參數化測試¶
pytest
允許輕鬆參數化測試函數。有關基本文件,請參閱如何參數化 fixtures 和測試函數。
在下面,我們提供一些使用內建機制的範例。
產生參數組合,取決於命令列¶
假設我們要使用不同的計算參數執行測試,並且參數範圍應由命令列參數確定。讓我們先編寫一個簡單的(無所事事)計算測試
# content of test_compute.py
def test_compute(param1):
assert param1 < 4
現在我們添加如下的測試配置
# content of conftest.py
def pytest_addoption(parser):
parser.addoption("--all", action="store_true", help="run all combinations")
def pytest_generate_tests(metafunc):
if "param1" in metafunc.fixturenames:
if metafunc.config.getoption("all"):
end = 5
else:
end = 2
metafunc.parametrize("param1", range(end))
這表示如果我們不傳遞 --all
,我們只運行 2 個測試
$ pytest -q test_compute.py
.. [100%]
2 passed in 0.12s
我們只運行兩個計算,所以我們看到兩個點。讓我們運行完整的 monty
$ pytest -q --all
....F [100%]
================================= FAILURES =================================
_____________________________ test_compute[4] ______________________________
param1 = 4
def test_compute(param1):
> assert param1 < 4
E assert 4 < 4
test_compute.py:4: AssertionError
========================= short test summary info ==========================
FAILED test_compute.py::test_compute[4] - assert 4 < 4
1 failed, 4 passed in 0.12s
正如預期的那樣,當運行完整的 param1
值範圍時,我們將在最後一個值上收到錯誤。
測試 ID 的不同選項¶
pytest 將建構一個字串,該字串是參數化測試中每組值的測試 ID。這些 ID 可以與 -k
一起使用,以選擇要運行的特定案例,並且它們還將在案例失敗時識別特定案例。使用 --collect-only
運行 pytest 將顯示產生的 ID。
數字、字串、布林值和 None 將在其測試 ID 中使用其常用的字串表示形式。對於其他物件,pytest 將根據參數名稱建立字串
# content of test_time.py
from datetime import datetime, timedelta
import pytest
testdata = [
(datetime(2001, 12, 12), datetime(2001, 12, 11), timedelta(1)),
(datetime(2001, 12, 11), datetime(2001, 12, 12), timedelta(-1)),
]
@pytest.mark.parametrize("a,b,expected", testdata)
def test_timedistance_v0(a, b, expected):
diff = a - b
assert diff == expected
@pytest.mark.parametrize("a,b,expected", testdata, ids=["forward", "backward"])
def test_timedistance_v1(a, b, expected):
diff = a - b
assert diff == expected
def idfn(val):
if isinstance(val, (datetime,)):
# note this wouldn't show any hours/minutes/seconds
return val.strftime("%Y%m%d")
@pytest.mark.parametrize("a,b,expected", testdata, ids=idfn)
def test_timedistance_v2(a, b, expected):
diff = a - b
assert diff == expected
@pytest.mark.parametrize(
"a,b,expected",
[
pytest.param(
datetime(2001, 12, 12), datetime(2001, 12, 11), timedelta(1), id="forward"
),
pytest.param(
datetime(2001, 12, 11), datetime(2001, 12, 12), timedelta(-1), id="backward"
),
],
)
def test_timedistance_v3(a, b, expected):
diff = a - b
assert diff == expected
在 test_timedistance_v0
中,我們讓 pytest 產生測試 ID。
在 test_timedistance_v1
中,我們將 ids
指定為字串列表,這些字串用作測試 ID。這些字串簡潔明瞭,但維護起來可能很麻煩。
在 test_timedistance_v2
中,我們將 ids
指定為一個函數,該函數可以產生字串表示形式以構成測試 ID 的一部分。因此,我們的 datetime
值使用由 idfn
產生的標籤,但由於我們沒有為 timedelta
物件產生標籤,因此它們仍然使用預設的 pytest 表示形式
$ pytest test_time.py --collect-only
=========================== test session starts ============================
platform linux -- Python 3.x.y, pytest-8.x.y, pluggy-1.x.y
rootdir: /home/sweet/project
collected 8 items
<Dir parametrize.rst-206>
<Module test_time.py>
<Function test_timedistance_v0[a0-b0-expected0]>
<Function test_timedistance_v0[a1-b1-expected1]>
<Function test_timedistance_v1[forward]>
<Function test_timedistance_v1[backward]>
<Function test_timedistance_v2[20011212-20011211-expected0]>
<Function test_timedistance_v2[20011211-20011212-expected1]>
<Function test_timedistance_v3[forward]>
<Function test_timedistance_v3[backward]>
======================== 8 tests collected in 0.12s ========================
在 test_timedistance_v3
中,我們使用 pytest.param
來指定測試 ID 以及實際資料,而不是將它們分開列出。
“testscenarios” 的快速移植¶
這是一個快速移植,用於運行使用 testscenarios 配置的測試,這是 Robert Collins 為標準 unittest 框架提供的附加元件。我們只需要做一些工作來為 pytest 的 Metafunc.parametrize
建構正確的參數
# content of test_scenarios.py
def pytest_generate_tests(metafunc):
idlist = []
argvalues = []
for scenario in metafunc.cls.scenarios:
idlist.append(scenario[0])
items = scenario[1].items()
argnames = [x[0] for x in items]
argvalues.append([x[1] for x in items])
metafunc.parametrize(argnames, argvalues, ids=idlist, scope="class")
scenario1 = ("basic", {"attribute": "value"})
scenario2 = ("advanced", {"attribute": "value2"})
class TestSampleWithScenarios:
scenarios = [scenario1, scenario2]
def test_demo1(self, attribute):
assert isinstance(attribute, str)
def test_demo2(self, attribute):
assert isinstance(attribute, str)
這是一個完全獨立的範例,您可以使用以下命令運行
$ pytest test_scenarios.py
=========================== test session starts ============================
platform linux -- Python 3.x.y, pytest-8.x.y, pluggy-1.x.y
rootdir: /home/sweet/project
collected 4 items
test_scenarios.py .... [100%]
============================ 4 passed in 0.12s =============================
如果您只是收集測試,您也會很好地看到 ‘advanced’ 和 ‘basic’ 作為測試函數的變體
$ pytest --collect-only test_scenarios.py
=========================== test session starts ============================
platform linux -- Python 3.x.y, pytest-8.x.y, pluggy-1.x.y
rootdir: /home/sweet/project
collected 4 items
<Dir parametrize.rst-206>
<Module test_scenarios.py>
<Class TestSampleWithScenarios>
<Function test_demo1[basic]>
<Function test_demo2[basic]>
<Function test_demo1[advanced]>
<Function test_demo2[advanced]>
======================== 4 tests collected in 0.12s ========================
請注意,我們告訴 metafunc.parametrize()
您的情境值應被視為類別範圍。使用 pytest-2.3,這會導致基於資源的排序。
延遲參數化資源的設定¶
測試函數的參數化發生在收集時。最好僅在實際測試運行時才設定昂貴的資源,例如資料庫連線或子程序。這是一個簡單的範例,說明您可以如何實現這一點。此測試需要 db
物件 fixture
# content of test_backends.py
import pytest
def test_db_initialized(db):
# a dummy test
if db.__class__.__name__ == "DB2":
pytest.fail("deliberately failing for demo purposes")
我們現在可以添加一個測試配置,該配置產生 test_db_initialized
函數的兩個調用,並且還實作一個工廠,該工廠為實際的測試調用建立資料庫物件
# content of conftest.py
import pytest
def pytest_generate_tests(metafunc):
if "db" in metafunc.fixturenames:
metafunc.parametrize("db", ["d1", "d2"], indirect=True)
class DB1:
"one database object"
class DB2:
"alternative database object"
@pytest.fixture
def db(request):
if request.param == "d1":
return DB1()
elif request.param == "d2":
return DB2()
else:
raise ValueError("invalid internal test config")
讓我們先看看它在收集時的樣子
$ pytest test_backends.py --collect-only
=========================== test session starts ============================
platform linux -- Python 3.x.y, pytest-8.x.y, pluggy-1.x.y
rootdir: /home/sweet/project
collected 2 items
<Dir parametrize.rst-206>
<Module test_backends.py>
<Function test_db_initialized[d1]>
<Function test_db_initialized[d2]>
======================== 2 tests collected in 0.12s ========================
然後當我們運行測試時
$ pytest -q test_backends.py
.F [100%]
================================= FAILURES =================================
_________________________ test_db_initialized[d2] __________________________
db = <conftest.DB2 object at 0xdeadbeef0001>
def test_db_initialized(db):
# a dummy test
if db.__class__.__name__ == "DB2":
> pytest.fail("deliberately failing for demo purposes")
E Failed: deliberately failing for demo purposes
test_backends.py:8: Failed
========================= short test summary info ==========================
FAILED test_backends.py::test_db_initialized[d2] - Failed: deliberately f...
1 failed, 1 passed in 0.12s
第一個調用 db == "DB1"
通過,而第二個調用 db == "DB2"
失敗。我們的 db
fixture 函數在設定階段實例化了每個 DB 值,而 pytest_generate_tests
在收集階段產生了對 test_db_initialized
的兩個相應調用。
間接參數化¶
在參數化測試時使用 indirect=True
參數允許使用 fixture 參數化測試,該 fixture 在將值傳遞給測試之前接收這些值
import pytest
@pytest.fixture
def fixt(request):
return request.param * 3
@pytest.mark.parametrize("fixt", ["a", "b"], indirect=True)
def test_indirect(fixt):
assert len(fixt) == 3
例如,這可以用於在 fixture 中在測試運行時執行更昂貴的設定,而不是必須在收集時運行這些設定步驟。
對特定參數應用間接¶
參數化通常使用多個參數名稱。有機會在特定參數上應用 indirect
參數。可以通過將參數名稱的列表或元組傳遞給 indirect
來完成。在下面的範例中,有一個函數 test_indirect
,它使用兩個 fixtures:x
和 y
。在這裡,我們將列表給予 indirect,其中包含 fixture x
的名稱。間接參數將僅應用於此參數,並且值 a
將傳遞給相應的 fixture 函數
# content of test_indirect_list.py
import pytest
@pytest.fixture(scope="function")
def x(request):
return request.param * 3
@pytest.fixture(scope="function")
def y(request):
return request.param * 2
@pytest.mark.parametrize("x, y", [("a", "b")], indirect=["x"])
def test_indirect(x, y):
assert x == "aaa"
assert y == "b"
此測試的結果將是成功的
$ pytest -v test_indirect_list.py
=========================== test session starts ============================
platform linux -- Python 3.x.y, pytest-8.x.y, pluggy-1.x.y -- $PYTHON_PREFIX/bin/python
cachedir: .pytest_cache
rootdir: /home/sweet/project
collecting ... collected 1 item
test_indirect_list.py::test_indirect[a-b] PASSED [100%]
============================ 1 passed in 0.12s =============================
通過每個類別配置參數化測試方法¶
這是一個範例 pytest_generate_tests
函數,它實作了類似於 Michael Foord 的 unittest parametrizer 的參數化方案,但程式碼少得多
# content of ./test_parametrize.py
import pytest
def pytest_generate_tests(metafunc):
# called once per each test function
funcarglist = metafunc.cls.params[metafunc.function.__name__]
argnames = sorted(funcarglist[0])
metafunc.parametrize(
argnames, [[funcargs[name] for name in argnames] for funcargs in funcarglist]
)
class TestClass:
# a map specifying multiple argument sets for a test method
params = {
"test_equals": [dict(a=1, b=2), dict(a=3, b=3)],
"test_zerodivision": [dict(a=1, b=0)],
}
def test_equals(self, a, b):
assert a == b
def test_zerodivision(self, a, b):
with pytest.raises(ZeroDivisionError):
a / b
我們的測試產生器查找類別級別的定義,該定義指定要為每個測試函數使用哪些參數集。讓我們運行它
$ pytest -q
F.. [100%]
================================= FAILURES =================================
________________________ TestClass.test_equals[1-2] ________________________
self = <test_parametrize.TestClass object at 0xdeadbeef0002>, a = 1, b = 2
def test_equals(self, a, b):
> assert a == b
E assert 1 == 2
test_parametrize.py:21: AssertionError
========================= short test summary info ==========================
FAILED test_parametrize.py::TestClass::test_equals[1-2] - assert 1 == 2
1 failed, 2 passed in 0.12s
使用多個 fixtures 進行參數化¶
這是一個簡化的真實範例,說明如何使用參數化測試來測試不同 Python 直譯器之間物件的序列化。我們定義了一個 test_basic_objects
函數,該函數將使用其三個參數的不同參數集運行
python1
:第一個 Python 直譯器,運行以將物件 pickle-dump 到檔案python2
:第二個直譯器,運行以從檔案 pickle-load 物件obj
:要 dump/load 的物件
"""Module containing a parametrized tests testing cross-python serialization
via the pickle module."""
from __future__ import annotations
import shutil
import subprocess
import textwrap
import pytest
pythonlist = ["python3.9", "python3.10", "python3.11"]
@pytest.fixture(params=pythonlist)
def python1(request, tmp_path):
picklefile = tmp_path / "data.pickle"
return Python(request.param, picklefile)
@pytest.fixture(params=pythonlist)
def python2(request, python1):
return Python(request.param, python1.picklefile)
class Python:
def __init__(self, version, picklefile):
self.pythonpath = shutil.which(version)
if not self.pythonpath:
pytest.skip(f"{version!r} not found")
self.picklefile = picklefile
def dumps(self, obj):
dumpfile = self.picklefile.with_name("dump.py")
dumpfile.write_text(
textwrap.dedent(
rf"""
import pickle
f = open({str(self.picklefile)!r}, 'wb')
s = pickle.dump({obj!r}, f, protocol=2)
f.close()
"""
)
)
subprocess.run((self.pythonpath, str(dumpfile)), check=True)
def load_and_is_true(self, expression):
loadfile = self.picklefile.with_name("load.py")
loadfile.write_text(
textwrap.dedent(
rf"""
import pickle
f = open({str(self.picklefile)!r}, 'rb')
obj = pickle.load(f)
f.close()
res = eval({expression!r})
if not res:
raise SystemExit(1)
"""
)
)
print(loadfile)
subprocess.run((self.pythonpath, str(loadfile)), check=True)
@pytest.mark.parametrize("obj", [42, {}, {1: 3}])
def test_basic_objects(python1, python2, obj):
python1.dumps(obj)
python2.load_and_is_true(f"obj == {obj}")
如果我們沒有安裝所有 Python 直譯器,則運行它會導致一些跳過,否則會運行所有組合(3 個直譯器乘以 3 個直譯器乘以 3 個要序列化/反序列化的物件)
. $ pytest -rs -q multipython.py
sssssssssssssssssssssssssss [100%]
========================= short test summary info ==========================
SKIPPED [9] multipython.py:67: 'python3.9' not found
SKIPPED [9] multipython.py:67: 'python3.10' not found
SKIPPED [9] multipython.py:67: 'python3.11' not found
27 skipped in 0.12s
可選實作/導入的參數化¶
如果您想比較給定 API 的多個實作的結果,您可以編寫測試函數,這些函數接收已導入的實作,並且在實作不可導入/可用時被跳過。假設我們有一個“base”實作,而另一個(可能是最佳化的實作)需要提供相似的結果
# content of conftest.py
import pytest
@pytest.fixture(scope="session")
def basemod(request):
return pytest.importorskip("base")
@pytest.fixture(scope="session", params=["opt1", "opt2"])
def optmod(request):
return pytest.importorskip(request.param)
然後是簡單函數的基本實作
# content of base.py
def func1():
return 1
以及最佳化版本
# content of opt1.py
def func1():
return 1.0001
最後是一個小的測試模組
# content of test_module.py
def test_func1(basemod, optmod):
assert round(basemod.func1(), 3) == round(optmod.func1(), 3)
如果您在啟用跳過報告的情況下運行此程式碼
$ pytest -rs test_module.py
=========================== test session starts ============================
platform linux -- Python 3.x.y, pytest-8.x.y, pluggy-1.x.y
rootdir: /home/sweet/project
collected 2 items
test_module.py .s [100%]
========================= short test summary info ==========================
SKIPPED [1] test_module.py:3: could not import 'opt2': No module named 'opt2'
======================= 1 passed, 1 skipped in 0.12s =======================
您會看到我們沒有 opt2
模組,因此我們的 test_func1
的第二次測試運行被跳過。一些注意事項
conftest.py
檔案中的 fixture 函數是“session-scoped”,因為我們不需要導入多次如果您有多個測試函數和跳過的導入,您將在報告中看到
[1]
計數增加您可以將 @pytest.mark.parametrize 樣式的參數化放在測試函數上,以參數化輸入/輸出值。
為個別參數化測試設定標記或測試 ID¶
使用 pytest.param
將標記應用於或設定個別參數化測試的測試 ID。例如
# content of test_pytest_param_example.py
import pytest
@pytest.mark.parametrize(
"test_input,expected",
[
("3+5", 8),
pytest.param("1+7", 8, marks=pytest.mark.basic),
pytest.param("2+4", 6, marks=pytest.mark.basic, id="basic_2+4"),
pytest.param(
"6*9", 42, marks=[pytest.mark.basic, pytest.mark.xfail], id="basic_6*9"
),
],
)
def test_eval(test_input, expected):
assert eval(test_input) == expected
在此範例中,我們有 4 個參數化測試。除了第一個測試之外,我們使用自訂標記 basic
標記其餘三個參數化測試,對於第四個測試,我們還使用內建標記 xfail
來指示此測試預期會失敗。為了明確起見,我們為某些測試設定了測試 ID。
然後使用詳細模式和僅使用 basic
標記運行 pytest
$ pytest -v -m basic
=========================== test session starts ============================
platform linux -- Python 3.x.y, pytest-8.x.y, pluggy-1.x.y -- $PYTHON_PREFIX/bin/python
cachedir: .pytest_cache
rootdir: /home/sweet/project
collecting ... collected 24 items / 21 deselected / 3 selected
test_pytest_param_example.py::test_eval[1+7-8] PASSED [ 33%]
test_pytest_param_example.py::test_eval[basic_2+4] PASSED [ 66%]
test_pytest_param_example.py::test_eval[basic_6*9] XFAIL [100%]
=============== 2 passed, 21 deselected, 1 xfailed in 0.12s ================
結果如下
收集了四個測試
由於一個測試沒有
basic
標記,因此取消選擇了一個測試。選擇了三個帶有
basic
標記的測試。測試
test_eval[1+7-8]
通過,但名稱是自動產生的且令人困惑。測試
test_eval[basic_2+4]
通過。測試
test_eval[basic_6*9]
預期會失敗並且確實失敗了。
參數化條件式拋出例外¶
將 pytest.raises()
與 pytest.mark.parametrize 裝飾器一起使用,以編寫參數化測試,其中某些測試引發例外,而另一些測試則不引發例外。
contextlib.nullcontext
可用於測試預期不會引發例外但應產生某些值的案例。該值作為 enter_result
參數給出,該參數將作為 with
語句的目標(以下範例中的 e
)提供。
例如
from contextlib import nullcontext
import pytest
@pytest.mark.parametrize(
"example_input,expectation",
[
(3, nullcontext(2)),
(2, nullcontext(3)),
(1, nullcontext(6)),
(0, pytest.raises(ZeroDivisionError)),
],
)
def test_division(example_input, expectation):
"""Test how much I know division."""
with expectation as e:
assert (6 / example_input) == e
在上面的範例中,前三個測試案例應在沒有任何例外的情況下運行,而第四個測試案例應引發 ZeroDivisionError
例外,這是 pytest 預期的。