pytest注解使用小結(jié)
前言:在 pytest 測(cè)試框架中,注解(通常稱為裝飾器)用于為測(cè)試函數(shù)、類或方法提供額外的信息或元數(shù)據(jù)。這些裝飾器可以影響測(cè)試的執(zhí)行方式、報(bào)告方式以及測(cè)試的組織結(jié)構(gòu)。pytest 提供了多種內(nèi)置的裝飾器,以及通過(guò)插件擴(kuò)展的額外裝飾器
以下是一些常用的 pytest 裝飾器及其用途:
1、@pytest.mark.parametrize:
- 用于參數(shù)化測(cè)試,允許您為測(cè)試函數(shù)提供多個(gè)參數(shù)集,pytest 將為每個(gè)參數(shù)集運(yùn)行一次測(cè)試。
- 示例:
@pytest.mark.parametrize("input,expected", [(1, 2), (3, 4)])
import pytest @pytest.mark.parametrize("input,expected", [(1, 2), (3, 4), (5, 6)]) def test_addition(input, expected): assert input + 1 == expected
在這個(gè)例子中,test_addition
函數(shù)將使用三組不同的參數(shù)((1, 2)
,(3, 4)
,(5, 6)
)分別運(yùn)行三次。
2、@pytest.mark.skip 和 @pytest.mark.skipif:
- 用于跳過(guò)測(cè)試。
@pytest.mark.skip
無(wú)條件跳過(guò)測(cè)試,而@pytest.mark.skipif
根據(jù)條件跳過(guò)測(cè)試。 - 示例:
@pytest.mark.skip(reason="Not ready yet")
或@pytest.mark.skipif(sys.version_info < (3, 6), reason="Python 3.6+ required")
import pytest import sys # 無(wú)條件跳過(guò) @pytest.mark.skip(reason="This test is not ready yet") def test_not_ready(): assert True # 根據(jù)條件跳過(guò) @pytest.mark.skipif(sys.version_info < (3, 6), reason="Python 3.6+ required") def test_python_version(): assert True
在第一個(gè)例子中,test_not_ready
函數(shù)將被無(wú)條件跳過(guò)。在第二個(gè)例子中,如果 Python 版本低于 3.6,test_python_version
函數(shù)將被跳過(guò)。
3、@pytest.mark.xfail 和 @pytest.mark.xfailif:
- 用于標(biāo)記預(yù)期失敗的測(cè)試。這些測(cè)試將被執(zhí)行,但如果它們失敗了,則不會(huì)被視為錯(cuò)誤。
- 示例:
@pytest.mark.xfail(reason="Known issue")
或@pytest.mark.xfailif(some_condition, reason="Condition not met")
注意:@pytest.mark.xfailif 不是 pytest 內(nèi)置的,但可以通過(guò)類似邏輯實(shí)現(xiàn)條件性的 xfail
import pytest # 標(biāo)記預(yù)期失敗的測(cè)試 @pytest.mark.xfail(reason="This is a known issue") def test_xfail(): assert False # 可以通過(guò)編寫一個(gè)函數(shù)來(lái)模擬 @pytest.mark.xfailif 的行為 def pytest_xfail_if(condition, reason): def decorator(func): if condition: func = pytest.mark.xfail(reason=reason)(func) return func return decorator # 使用模擬的 @pytest.mark.xfailif @pytest_xfail_if(True, reason="Condition met, expect failure") def test_conditional_xfail(): assert False
4、@pytest.mark.tryfirst 和 @pytest.mark.trylast:
- 用于控制測(cè)試的執(zhí)行順序,尤其是在有多個(gè)鉤子函數(shù)(如 setup/teardown 方法)時(shí)。
- 這些裝飾器通常與 pytest 插件中的鉤子函數(shù)一起使用。
通常與 pytest 插件中的鉤子函數(shù)一起使用
# 假設(shè)有一個(gè) pytest 插件提供了 setup 和 teardown 鉤子函數(shù) # 并且我們想要某個(gè)測(cè)試在這些鉤子函數(shù)中首先或最后執(zhí)行 # 注意:這里的示例是假設(shè)性的,因?yàn)?@pytest.mark.tryfirst 和 @pytest.mark.trylast # 通常不直接用于測(cè)試函數(shù),而是用于鉤子函數(shù)或插件實(shí)現(xiàn) # 假設(shè)的 setup 和 teardown 鉤子函數(shù)(實(shí)際上需要由 pytest 插件提供) # @pytest.hookimpl(tryfirst=True) # def pytest_setup(): # pass # @pytest.hookimpl(trylast=True) # def pytest_teardown(): # pass # 假設(shè)的測(cè)試函數(shù)(實(shí)際上不會(huì)直接使用 @pytest.mark.tryfirst 或 @pytest.mark.trylast) # @pytest.mark.tryfirst # 這通常不會(huì)直接用于測(cè)試函數(shù) def test_tryfirst(): pass # @pytest.mark.trylast # 這通常也不會(huì)直接用于測(cè)試函數(shù) def test_trylast(): pass
5、@pytest.mark.usefixtures:
- 用于聲明測(cè)試將使用的 fixture。雖然這不是嚴(yán)格意義上的裝飾器(因?yàn)樗恢苯有揎椇瘮?shù)),但它用于指定測(cè)試依賴的 fixture。
- 示例:
@pytest.mark.usefixtures("my_fixture")
import pytest @pytest.fixture def my_fixture(): return "fixture value" @pytest.mark.usefixtures("my_fixture") def test_with_fixture(my_fixture_value): assert my_fixture_value == "fixture value" # 注意:在實(shí)際使用中,pytest 會(huì)自動(dòng)將 fixture 的值注入到測(cè)試函數(shù)中, # 因此測(cè)試函數(shù)的參數(shù)名應(yīng)與 fixture 的名稱相匹配(或使用 pytest.mark.parametrize 來(lái)指定參數(shù)名)。 # 上面的示例中,為了說(shuō)明 @pytest.mark.usefixtures 的用法, # 假設(shè)了一個(gè)名為 my_fixture_value 的參數(shù),但在實(shí)際代碼中應(yīng)直接使用 my_fixture。 # 正確的用法如下: @pytest.mark.usefixtures("my_fixture") def test_with_fixture_correct(my_fixture): assert my_fixture == "fixture value"
在這個(gè)例子中,test_with_fixture_correct
函數(shù)將使用名為 my_fixture
的 fixture。請(qǐng)注意,在實(shí)際代碼中,您不需要(也不應(yīng)該)在測(cè)試函數(shù)參數(shù)中顯式地指定 fixture 的值;pytest 會(huì)自動(dòng)將其注入
6、@pytest.mark.filterwarnings:
- 用于控制測(cè)試期間應(yīng)如何處理警告。
- 示例:
@pytest.mark.filterwarnings("ignore::DeprecationWarning")
import pytest import warnings @pytest.mark.filterwarnings("ignore::DeprecationWarning") def test_with_warnings(): warnings.warn("This is a deprecation warning", DeprecationWarning) assert True
在這個(gè)例子中,test_with_warnings
函數(shù)將忽略 DeprecationWarning
類型的警告。
7、@pytest.mark.timeout(通過(guò) pytest-timeout 插件提供):
- 用于設(shè)置測(cè)試的超時(shí)時(shí)間。如果測(cè)試在指定時(shí)間內(nèi)未完成,則將被標(biāo)記為失敗。
- 示例:
@pytest.mark.timeout(10)
(10秒超時(shí))
import pytest @pytest.mark.timeout(5) # 設(shè)置超時(shí)時(shí)間為5秒 def test_with_timeout(): import time time.sleep(10) # 這將觸發(fā)超時(shí)失敗 assert True
在這個(gè)例子中,test_with_timeout
函數(shù)將在5秒后超時(shí)失敗,因?yàn)?nbsp;time.sleep(10)
會(huì)使測(cè)試運(yùn)行超過(guò)指定的超時(shí)時(shí)間。
8、@pytest.mark.flaky(通過(guò) pytest-flaky 插件提供):
- 用于標(biāo)記可能間歇性失敗的測(cè)試,并允許它們?cè)谝欢〝?shù)量的重試后通過(guò)。
- 示例:
@pytest.mark.flaky(reruns=3, reruns_delay=2)
(重試3次,每次延遲2秒)
import pytest @pytest.mark.flaky(reruns=3, reruns_delay=1) # 設(shè)置重試3次,每次延遲1秒 def test_flaky(): import random assert random.choice([True, False]) # 這將隨機(jī)成功或失敗
在這個(gè)例子中,test_flaky
函數(shù)將隨機(jī)成功或失敗。如果它失敗了,pytest-flaky 插件將重試它最多3次,每次之間延遲1秒。
9、@pytest.mark.order(通過(guò) pytest-order 插件提供):
- 用于指定測(cè)試的執(zhí)行順序。
- 示例:
@pytest.mark.order(1)
(數(shù)字越小,執(zhí)行越早)
import pytest @pytest.mark.order(1) # 設(shè)置執(zhí)行順序?yàn)? def test_first(): assert True @pytest.mark.order(2) # 設(shè)置執(zhí)行順序?yàn)? def test_second(): assert True
在這個(gè)例子中,test_first
函數(shù)將先于 test_second
函數(shù)執(zhí)行,因?yàn)樗鼈兊膱?zhí)行順序被分別設(shè)置為1和2。
10、自定義標(biāo)記:
- 您可以使用
@pytest.mark.<name>
語(yǔ)法創(chuàng)建自定義的標(biāo)記,并在測(cè)試配置文件中定義它們的行為。 - 示例:
@pytest.mark.my_custom_mark
(然后在 pytest.ini 或 pytest.mark 文件中定義它)
import pytest # 在 pytest.ini 或 pytest.mark 文件中定義自定義標(biāo)記 # [pytest] # markers = # my_custom_mark: This is a custom marker @pytest.mark.my_custom_mark # 使用自定義標(biāo)記 def test_with_custom_mark(): assert True
我們定義了一個(gè)名為 my_custom_mark
的自定義標(biāo)記,并在 test_with_custom_mark
函數(shù)中使用了它。請(qǐng)注意,您需要在 pytest 的配置文件中(如 pytest.ini
或 pytest.mark
)定義這個(gè)自定義標(biāo)記,以便 pytest 能夠識(shí)別它。
請(qǐng)注意,上述列表中的一些裝飾器(如 @pytest.mark.timeout 和 @pytest.mark.flaky)是通過(guò) pytest 插件提供的,因此在使用它們之前需要確保已安裝相應(yīng)的插件。
在使用這些裝飾器時(shí),請(qǐng)確保您了解它們?nèi)绾斡绊憸y(cè)試的執(zhí)行和報(bào)告,以及它們是否適用于您的測(cè)試場(chǎng)景。
到此這篇關(guān)于pytest注解使用小結(jié)的文章就介紹到這了,更多相關(guān)pytest注解使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- python中pytest收集用例規(guī)則與運(yùn)行指定用例詳解
- python pytest進(jìn)階之fixture詳解
- 詳解用Pytest+Allure生成漂亮的HTML圖形化測(cè)試報(bào)告
- Pytest框架之fixture的詳細(xì)使用教程
- pycharm中使用request和Pytest進(jìn)行接口測(cè)試的方法
- 在pycharm中文件取消用 pytest模式打開(kāi)的操作
- Pytest測(cè)試框架基本使用方法詳解
- 詳解Pytest測(cè)試用例的執(zhí)行方法
- Python自動(dòng)化測(cè)試框架pytest的詳解安裝與運(yùn)行
相關(guān)文章
pandas與pyspark計(jì)算效率對(duì)比分析
這篇文章主要介紹了pandas與pyspark計(jì)算效率對(duì)比,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-06-06在python中以相同順序shuffle兩個(gè)list的方法
今天小編就為大家分享一篇在python中以相同順序shuffle兩個(gè)list的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-12-12python flask框架實(shí)現(xiàn)傳數(shù)據(jù)到j(luò)s的方法分析
這篇文章主要介紹了python flask框架實(shí)現(xiàn)傳數(shù)據(jù)到j(luò)s的方法,結(jié)合實(shí)例形式分析了前端數(shù)據(jù)序列化及后臺(tái)Flask交互數(shù)據(jù)返回相關(guān)操作技巧,需要的朋友可以參考下2019-06-06Python編程中NotImplementedError的使用方法
下面小編就為大家分享一篇Python編程中NotImplementedError的使用方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-04-04