pytest標(biāo)記的具體使用
標(biāo)記通常搭配裝飾器使用
pytest
的標(biāo)記(Markers) 是一個非常強(qiáng)大和靈活的功能,允許你為測試用例“打標(biāo)簽”,從而實(shí)現(xiàn)分類、控制執(zhí)行、修改行為等目的。- 可以使用
@pytest.mark.標(biāo)記名
來裝飾測試函數(shù)或測試類。
1、內(nèi)置標(biāo)記(Built-in Marks)
pytest
提供了一些開箱即用的內(nèi)置標(biāo)記:
標(biāo)記 | 作用 | 示例 |
---|---|---|
@pytest.mark.skip(reason="...") | 無條件跳過該測試。 | @pytest.mark.skip(reason="臨時禁用") |
@pytest.mark.skipif(condition, reason="...") | 條件跳過測試。 | @pytest.mark.skipif(sys.version_info < (3,8), reason="需要 Python 3.8+") |
@pytest.mark.xfail(reason="...") | 預(yù)期該測試會失敗。如果失敗,結(jié)果為 XFAIL;如果意外通過,結(jié)果為 XPASS。 | @pytest.mark.xfail(reason="已知缺陷") |
@pytest.mark.parametrize | 參數(shù)化測試,用不同數(shù)據(jù)多次運(yùn)行同一測試。 | 見下文示例 |
@pytest.mark.usefixtures('fixture_name') | 在測試中使用 fixture,但不將其作為參數(shù)傳入。 | @pytest.mark.usefixtures("db_cleaner") |
@pytest.mark.filterwarnings | 為測試函數(shù)添加警告過濾器。 | @pytest.mark.filterwarnings("ignore::DeprecationWarning") |
@pytest.mark.parametrize詳解
這是最常用的標(biāo)記之一,用于數(shù)據(jù)驅(qū)動測試。數(shù)據(jù)驅(qū)動測試 = 參數(shù)化測試+數(shù)據(jù)文件
基本語法
@pytest.mark.parametrize("參數(shù)名", [值1, 值2, ...]) def test_function(參數(shù)名): ...
示例
import pytest # 測試平方函數(shù) @pytest.mark.parametrize("input,expected", [ (2, 4), (3, 9), (-2, 4), (0, 0), ]) def test_square(input, expected): assert input ** 2 == expected # 測試不同瀏覽器(字符串參數(shù)) @pytest.mark.parametrize("browser", ["chrome", "firefox", "safari"]) def test_login(browser): print(f"Testing login with {browser}") # 啟動對應(yīng)瀏覽器并測試登錄 -- 運(yùn)行結(jié)果會顯示為多個獨(dú)立的測試用例: test_square[2-4] PASSED test_square[3-9] PASSED test_square[-2-4] PASSED test_login[chrome] PASSED test_login[firefox] PASSED
2、自定義標(biāo)記(Custom Marks)
可以創(chuàng)建自己的標(biāo)記來分類測試,如 smoke
, integration
, e2e
, slow
等。
2.1 在配置文件中聲明自定義標(biāo)記
如果不聲明,pytest
會發(fā)出警告(PytestUnknownMarkWarning
)。
在 pyproject.toml
中聲明:
[tool.pytest.ini_options] markers = [ "smoke: 快速冒煙測試", "integration: 集成測試", "e2e: 端到端測試", "slow: 運(yùn)行時間較長的測試", "db: 涉及數(shù)據(jù)庫操作", "performance: 性能測試" ]
或在 pytest.ini
中:
[tool:pytest] markers = smoke: 快速冒煙測試 integration: 集成測試 slow: 運(yùn)行時間較長的測試
2.2 使用自定義標(biāo)記
import pytest @pytest.mark.smoke def test_login(): assert True @pytest.mark.integration @pytest.mark.db class TestUserAPI: def test_create_user(self): assert True @pytest.mark.performance def test_user_list_speed(self): assert True
2.3 使用標(biāo)記來控制測試執(zhí)行
標(biāo)記的強(qiáng)大之處在于你可以選擇性地運(yùn)行或跳過測試。
命令 | 作用 |
---|---|
pytest -m "smoke" | 只運(yùn)行標(biāo)記為 smoke 的測試。 |
pytest -m "not smoke" | 運(yùn)行未標(biāo)記為 smoke 的測試。 |
pytest -m "integration and db" | 運(yùn)行同時標(biāo)記為 integration 和 db 的測試。 |
pytest -m "smoke or integration" | 運(yùn)行標(biāo)記為 smoke 或 integration 的測試。 |
pytest -m "not slow" | 跳過所有標(biāo)記為 slow 的測試(常用于CI快速反饋)。 |
3、內(nèi)置標(biāo)記和自定義標(biāo)記的區(qū)別
對比維度 | 內(nèi)置標(biāo)記 (Built-in Marks) | 自定義標(biāo)記 (Custom Marks) |
---|---|---|
來源 | pytest 框架自帶的功能。 | 由用戶自己定義的標(biāo)簽。 |
功能 | 具有特定行為邏輯(如跳過、預(yù)期失敗、參數(shù)化)。 | 默認(rèn)無行為,僅用于分類和標(biāo)記,行為由執(zhí)行命令控制。 |
是否需要聲明 | ? 不需要在配置文件中聲明。 | ? 必須在 pyproject.toml 或 pytest.ini 中聲明,否則會警告。 |
用途 | 控制測試的執(zhí)行行為。 | 對測試進(jìn)行分類、分組,便于選擇性執(zhí)行。 |
示例 | @pytest.mark.skip, @pytest.mark.parametrize | @pytest.mark.smoke, @pytest.mark.e2e |
4、類標(biāo)記
- 類上的標(biāo)記會應(yīng)用到類中所有的測試方法。
- 可以在方法上添加額外的標(biāo)記。
@pytest.mark.db class TestDatabase: def test_read(self): # 有 db 標(biāo)記 pass @pytest.mark.write def test_write(self): # 有 db 和 write 兩個標(biāo)記 pass @pytest.mark.skip def test_delete(self): # 有 db 標(biāo)記,但會被跳過 pass
5、查看所有已注冊的標(biāo)記
pytest --markers
輸出示例:
@pytest.mark.smoke - 快速冒煙測試 @pytest.mark.integration - 集成測試 @pytest.mark.slow - 運(yùn)行時間較長的測試 @pytest.mark.skip - skip this test unconditionally @pytest.mark.skipif - skip the given test if condition is True ...
6、實(shí)踐
- 為自定義標(biāo)記在配置文件中聲明,避免警告。
- 使用有意義的標(biāo)記名,如
smoke
,e2e
,ui
,api
。 - 結(jié)合 CI/CD 使用:例如,在快速流水線中只運(yùn)行
smoke
測試,在完整流水線中運(yùn)行所有測試。 - 避免過度標(biāo)記:標(biāo)記太多會增加管理成本。
- 利用
parametrize
+mark
:可以為參數(shù)化測試的特定數(shù)據(jù)點(diǎn)打標(biāo)記。
總結(jié)
pytest
的標(biāo)記系統(tǒng)是其靈活性和可擴(kuò)展性的核心之一。通過標(biāo)記,可以:
- 組織和分類測試用例。
- 控制執(zhí)行流程(跳過、預(yù)期失?。?/li>
- 實(shí)現(xiàn)參數(shù)化測試。
- 集成到自動化流程中(如只運(yùn)行冒煙測試)。
到此這篇關(guān)于pytest標(biāo)記的具體使用的文章就介紹到這了,更多相關(guān)pytest標(biāo)記內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
從基礎(chǔ)到高級詳解Python Shell通配符匹配技術(shù)的完整指南
在文件處理和文本匹配領(lǐng)域,Shell通配符模式是每個開發(fā)者必備的核心技能,本文小編就來和大家簡單介紹一下Shell通配符匹配技術(shù)再Python語言中的具體應(yīng)用吧2025-08-08Django?Rest?Framework實(shí)現(xiàn)身份認(rèn)證源碼詳解
這篇文章主要為大家介紹了Django?Rest?Framework實(shí)現(xiàn)身份認(rèn)證源碼詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05Python bsddb模塊操作Berkeley DB數(shù)據(jù)庫介紹
這篇文章主要介紹了Python bsddb模塊操作Berkeley DB數(shù)據(jù)庫介紹,這里簡單介紹一些關(guān)于bsddb的使用方法,需要的朋友可以參考下2015-04-04