pytest注解使用小結
前言:在 pytest 測試框架中,注解(通常稱為裝飾器)用于為測試函數(shù)、類或方法提供額外的信息或元數(shù)據(jù)。這些裝飾器可以影響測試的執(zhí)行方式、報告方式以及測試的組織結構。pytest 提供了多種內(nèi)置的裝飾器,以及通過插件擴展的額外裝飾器

以下是一些常用的 pytest 裝飾器及其用途:
1、@pytest.mark.parametrize:
- 用于參數(shù)化測試,允許您為測試函數(shù)提供多個參數(shù)集,pytest 將為每個參數(shù)集運行一次測試。
- 示例:
@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在這個例子中,test_addition 函數(shù)將使用三組不同的參數(shù)((1, 2),(3, 4),(5, 6))分別運行三次。
2、@pytest.mark.skip 和 @pytest.mark.skipif:
- 用于跳過測試。
@pytest.mark.skip無條件跳過測試,而@pytest.mark.skipif根據(jù)條件跳過測試。 - 示例:
@pytest.mark.skip(reason="Not ready yet")或@pytest.mark.skipif(sys.version_info < (3, 6), reason="Python 3.6+ required")
import pytest
import sys
# 無條件跳過
@pytest.mark.skip(reason="This test is not ready yet")
def test_not_ready():
assert True
# 根據(jù)條件跳過
@pytest.mark.skipif(sys.version_info < (3, 6), reason="Python 3.6+ required")
def test_python_version():
assert True 在第一個例子中,test_not_ready 函數(shù)將被無條件跳過。在第二個例子中,如果 Python 版本低于 3.6,test_python_version 函數(shù)將被跳過。
3、@pytest.mark.xfail 和 @pytest.mark.xfailif:
- 用于標記預期失敗的測試。這些測試將被執(zhí)行,但如果它們失敗了,則不會被視為錯誤。
- 示例:
@pytest.mark.xfail(reason="Known issue")或@pytest.mark.xfailif(some_condition, reason="Condition not met")
注意:@pytest.mark.xfailif 不是 pytest 內(nèi)置的,但可以通過類似邏輯實現(xiàn)條件性的 xfail
import pytest
# 標記預期失敗的測試
@pytest.mark.xfail(reason="This is a known issue")
def test_xfail():
assert False
# 可以通過編寫一個函數(shù)來模擬 @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 False4、@pytest.mark.tryfirst 和 @pytest.mark.trylast:
- 用于控制測試的執(zhí)行順序,尤其是在有多個鉤子函數(shù)(如 setup/teardown 方法)時。
- 這些裝飾器通常與 pytest 插件中的鉤子函數(shù)一起使用。
通常與 pytest 插件中的鉤子函數(shù)一起使用
# 假設有一個 pytest 插件提供了 setup 和 teardown 鉤子函數(shù)
# 并且我們想要某個測試在這些鉤子函數(shù)中首先或最后執(zhí)行
# 注意:這里的示例是假設性的,因為 @pytest.mark.tryfirst 和 @pytest.mark.trylast
# 通常不直接用于測試函數(shù),而是用于鉤子函數(shù)或插件實現(xiàn)
# 假設的 setup 和 teardown 鉤子函數(shù)(實際上需要由 pytest 插件提供)
# @pytest.hookimpl(tryfirst=True)
# def pytest_setup():
# pass
# @pytest.hookimpl(trylast=True)
# def pytest_teardown():
# pass
# 假設的測試函數(shù)(實際上不會直接使用 @pytest.mark.tryfirst 或 @pytest.mark.trylast)
# @pytest.mark.tryfirst # 這通常不會直接用于測試函數(shù)
def test_tryfirst():
pass
# @pytest.mark.trylast # 這通常也不會直接用于測試函數(shù)
def test_trylast():
pass5、@pytest.mark.usefixtures:
- 用于聲明測試將使用的 fixture。雖然這不是嚴格意義上的裝飾器(因為它不直接修飾函數(shù)),但它用于指定測試依賴的 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"
# 注意:在實際使用中,pytest 會自動將 fixture 的值注入到測試函數(shù)中,
# 因此測試函數(shù)的參數(shù)名應與 fixture 的名稱相匹配(或使用 pytest.mark.parametrize 來指定參數(shù)名)。
# 上面的示例中,為了說明 @pytest.mark.usefixtures 的用法,
# 假設了一個名為 my_fixture_value 的參數(shù),但在實際代碼中應直接使用 my_fixture。
# 正確的用法如下:
@pytest.mark.usefixtures("my_fixture")
def test_with_fixture_correct(my_fixture):
assert my_fixture == "fixture value"在這個例子中,test_with_fixture_correct 函數(shù)將使用名為 my_fixture 的 fixture。請注意,在實際代碼中,您不需要(也不應該)在測試函數(shù)參數(shù)中顯式地指定 fixture 的值;pytest 會自動將其注入
6、@pytest.mark.filterwarnings:
- 用于控制測試期間應如何處理警告。
- 示例:
@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在這個例子中,test_with_warnings 函數(shù)將忽略 DeprecationWarning 類型的警告。
7、@pytest.mark.timeout(通過 pytest-timeout 插件提供):
- 用于設置測試的超時時間。如果測試在指定時間內(nèi)未完成,則將被標記為失敗。
- 示例:
@pytest.mark.timeout(10)(10秒超時)
import pytest
@pytest.mark.timeout(5) # 設置超時時間為5秒
def test_with_timeout():
import time
time.sleep(10) # 這將觸發(fā)超時失敗
assert True 在這個例子中,test_with_timeout 函數(shù)將在5秒后超時失敗,因為 time.sleep(10) 會使測試運行超過指定的超時時間。
8、@pytest.mark.flaky(通過 pytest-flaky 插件提供):
- 用于標記可能間歇性失敗的測試,并允許它們在一定數(shù)量的重試后通過。
- 示例:
@pytest.mark.flaky(reruns=3, reruns_delay=2)(重試3次,每次延遲2秒)
import pytest
@pytest.mark.flaky(reruns=3, reruns_delay=1) # 設置重試3次,每次延遲1秒
def test_flaky():
import random
assert random.choice([True, False]) # 這將隨機成功或失敗在這個例子中,test_flaky 函數(shù)將隨機成功或失敗。如果它失敗了,pytest-flaky 插件將重試它最多3次,每次之間延遲1秒。
9、@pytest.mark.order(通過 pytest-order 插件提供):
- 用于指定測試的執(zhí)行順序。
- 示例:
@pytest.mark.order(1)(數(shù)字越小,執(zhí)行越早)
import pytest
@pytest.mark.order(1) # 設置執(zhí)行順序為1
def test_first():
assert True
@pytest.mark.order(2) # 設置執(zhí)行順序為2
def test_second():
assert True在這個例子中,test_first 函數(shù)將先于 test_second 函數(shù)執(zhí)行,因為它們的執(zhí)行順序被分別設置為1和2。
10、自定義標記:
- 您可以使用
@pytest.mark.<name>語法創(chuàng)建自定義的標記,并在測試配置文件中定義它們的行為。 - 示例:
@pytest.mark.my_custom_mark(然后在 pytest.ini 或 pytest.mark 文件中定義它)
import pytest
# 在 pytest.ini 或 pytest.mark 文件中定義自定義標記
# [pytest]
# markers =
# my_custom_mark: This is a custom marker
@pytest.mark.my_custom_mark # 使用自定義標記
def test_with_custom_mark():
assert True 我們定義了一個名為 my_custom_mark 的自定義標記,并在 test_with_custom_mark 函數(shù)中使用了它。請注意,您需要在 pytest 的配置文件中(如 pytest.ini 或 pytest.mark)定義這個自定義標記,以便 pytest 能夠識別它。
請注意,上述列表中的一些裝飾器(如 @pytest.mark.timeout 和 @pytest.mark.flaky)是通過 pytest 插件提供的,因此在使用它們之前需要確保已安裝相應的插件。
在使用這些裝飾器時,請確保您了解它們?nèi)绾斡绊憸y試的執(zhí)行和報告,以及它們是否適用于您的測試場景。
到此這篇關于pytest注解使用小結的文章就介紹到這了,更多相關pytest注解使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
解決Django中調(diào)用keras的模型出現(xiàn)的問題
今天小編就為大家分享一篇解決Django中調(diào)用keras的模型出現(xiàn)的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-08-08
python pandas 解析(讀取、寫入)CSV 文件的操作方法
這篇文章主要介紹了python pandas 解析(讀取、寫入) CSV 文件,本文通過示例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-12-12
matplotlib共享坐標軸的實現(xiàn)(X或Y坐標軸)
在作圖的過程中,我們經(jīng)常會遇到子圖共用坐標軸的情況,或是共用橫軸標軸,也可能是縱坐標軸。本文就介紹了matplotlib共享坐標軸,感興趣的可以了解一下2021-05-05
python tools實現(xiàn)視頻的每一幀提取并保存
這篇文章主要為大家詳細介紹了python tools實現(xiàn)視頻的每一幀提取并保存,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-05-05
OpenCV每日函數(shù)之BarcodeDetector類條碼檢測器
OpenCV在V4.5.3版本的contrib包中提供了一個barcode::BarcodeDetector類,用于條形碼的識別,這篇文章主要介紹了OpenCV每日函數(shù)?BarcodeDetector條碼檢測器,需要的朋友可以參考下2022-06-06

