pytest參數(shù)化:@pytest.mark.parametrize詳解
pytest參數(shù)化:@pytest.mark.parametrize
內(nèi)置的pytest.mark.parametrize裝飾器可以用來(lái)對(duì)測(cè)試函數(shù)進(jìn)行參數(shù)化處理。
下面是一個(gè)典型的范例
檢查特定的輸入所期望的輸出是否匹配:
- test_expectation.py
import pytest
@pytest.mark.parametrize("test_input, expected", [("3+5", 8), ("2+4", 6), ("6*9", 42),])
def test_eval(test_input, expected):
assert eval(test_input) == expected
裝飾器@parametrize定義了三組不同的(test_input, expected)數(shù)據(jù),test_eval則會(huì)使用這三組數(shù)據(jù)
執(zhí)行三次:
$ pytest
=========================== test session starts ============================
platform linux ‐‐ Python 3.x.y, pytest‐4.x.y, py‐1.x.y, pluggy‐0.x.y
cachedir: $PYTHON_PREFIX/.pytest_cache
rootdir: $REGENDOC_TMPDIR, inifile:
collected 3 items
test_expectation.py ..F [100%]
================================= FAILURES =================================
____________________________ test_eval[6*9‐42] _____________________________
test_input = '6*9', expected = 42
@pytest.mark.parametrize("test_input,expected", [
("3+5", 8),
("2+4", 6),
("6*9", 42),
])
def test_eval(test_input, expected):
> assert eval(test_input) == expected
E AssertionError: assert 54 == 42
E + where 54 = eval('6*9')
test_expectation.py:8: AssertionError
==================== 1 failed, 2 passed in 0.12 seconds ====================
該示例中,只有一組數(shù)據(jù)是失敗的
通常情況下你可以在traceback中看到作為函數(shù)參數(shù)的input和output。
注意:
你也可以對(duì)模塊或者class使用參數(shù)化的marker來(lái)讓多個(gè)測(cè)試函數(shù)在不同的測(cè)試集下運(yùn)行。
你也可以對(duì)參數(shù)集中的某個(gè)參數(shù)使用mark,比如下面使用了內(nèi)置的mark.xfail:
- test_exception.py
import pytest
@pytest.mark.parametrize("test_input, expected", [("3+5", 8), ("2+4", 6), ("6*9", 42, marks=pytest.mark.xfail),])
def test_eval(test_input, expected):
assert eval(test_input) == expected
運(yùn)行結(jié)果如下:
$ pytest
=========================== test session starts ============================
platform linux ‐‐ Python 3.x.y, pytest‐4.x.y, py‐1.x.y, pluggy‐0.x.y
cachedir: $PYTHON_PREFIX/.pytest_cache
rootdir: $REGENDOC_TMPDIR, inifile:
collected 3 items
test_expectation.py ..x [100%]
=================== 2 passed, 1 xfailed in 0.12 seconds ====================
之前結(jié)果是失敗的用例在這里已經(jīng)被標(biāo)記為xfailed了。
如果參數(shù)化的列表是一個(gè)空列表,比如參數(shù)是某個(gè)函數(shù)動(dòng)態(tài)生成的,請(qǐng)參考empty_parameter_set_mark選項(xiàng)。
可以對(duì)一個(gè)函數(shù)使用多個(gè)parametrize的裝飾器,這樣多個(gè)裝飾器的參數(shù)會(huì)組合進(jìn)行調(diào)用:
import pytest
@pytest.mark.parametrize("x", [0, 1])
@pytest.mark.parametrize("y", [2, 3])
def test_foo(x, y):
pass
這會(huì)窮舉x和y的所有組合并進(jìn)行調(diào)用。
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- pytest實(shí)戰(zhàn)技巧之參數(shù)化基本用法和多種方式
- Python中pytest的參數(shù)化實(shí)例解析
- pytest使用@pytest.mark.parametrize()實(shí)現(xiàn)參數(shù)化的示例代碼
- pytest?fixtures函數(shù)及測(cè)試函數(shù)的參數(shù)化解讀
- pytest使用parametrize將參數(shù)化變量傳遞到fixture
- Python基礎(chǔ)教程之pytest參數(shù)化詳解
- pytest實(shí)現(xiàn)測(cè)試用例參數(shù)化
- Pytest單元測(cè)試框架如何實(shí)現(xiàn)參數(shù)化
- Pytest參數(shù)化parametrize使用代碼實(shí)例
相關(guān)文章
Python實(shí)現(xiàn)將JSON轉(zhuǎn)換為CSV格式
JSON(JavaScript??Object?Notation)和?CSV(Comma-Separated?Values)是數(shù)據(jù)交換和存儲(chǔ)中最常用的兩種格式,本文將詳細(xì)介紹如何使用Free?Spire.XLS?for?Python將JSON數(shù)據(jù)轉(zhuǎn)換為?CSV?文件,感興趣的小伙伴可以了解下2026-03-03
Python中try?except語(yǔ)句及實(shí)際應(yīng)用詳細(xì)解釋
在Python中try和except是用于異常處理的關(guān)鍵字,它們可以捕獲程序運(yùn)行時(shí)可能出現(xiàn)的錯(cuò)誤,從而避免程序崩潰,這篇文章主要介紹了Python中try?except語(yǔ)句及實(shí)際應(yīng)用的相關(guān)資料,需要的朋友可以參考下2025-04-04
python實(shí)現(xiàn)的陽(yáng)歷轉(zhuǎn)陰歷(農(nóng)歷)算法
這篇文章主要介紹了python實(shí)現(xiàn)的陽(yáng)歷轉(zhuǎn)陰歷(農(nóng)歷)算法,需要的朋友可以參考下2014-04-04
如何使用pyinstaller打包多個(gè)和單個(gè)python文件詳解
最近需要將python寫(xiě)的程序打包分發(fā)給其他同事使用,下面這篇文章主要給大家介紹了關(guān)于如何使用pyinstaller打包多個(gè)和單個(gè)python文件的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-06-06
python3 property裝飾器實(shí)現(xiàn)原理與用法示例
這篇文章主要介紹了python3 property裝飾器實(shí)現(xiàn)原理與用法,結(jié)合實(shí)例形式分析了Python3 property裝飾器功能、原理及實(shí)現(xiàn)方法,需要的朋友可以參考下2019-05-05

