參數化測試¶
pytest
允許輕鬆參數化測試函式。有關基本文件,請參閱 如何參數化固定裝置和測試函式。
以下我們提供一些使用內建機制的範例。
產生參數組合,取決於命令列¶
假設我們想使用不同的運算參數執行測試,且參數範圍應由命令列引數決定。我們先撰寫一個簡單的(無動作)運算測試
# 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
我們只執行兩個運算,所以我們看到兩個點。讓我們執行完整測試
$ 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-198>
<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 =============================
如果您只收集測試,您也會清楚地看到測試函式的變異為「進階」和「基礎」
$ 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-198>
<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
物件裝置
# 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-198>
<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
裝置函式已在設定階段實例化每個 DB 值,而 pytest_generate_tests
在收集階段產生兩個對應的 test_db_initialized
呼叫。
間接參數化¶
參數化測試時使用 indirect=True
參數,可以在將值傳遞給測試之前,使用裝置參數化測試
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
例如,這可以用於在裝置中執行更昂貴的設定,而不是在收集時間執行那些設定步驟。
對特定參數套用間接參數化¶
參數化通常會使用多個參數名稱。有機會對特定參數套用 indirect
參數。這可透過將參數名稱的清單或元組傳遞給 indirect
來完成。以下範例中有一個函式 test_indirect
,它使用兩個裝置:x
和 y
。這裡我們將包含裝置 x
名稱的清單提供給 indirect。indirect 參數只會套用於此參數,而值 a
會傳遞給各自的裝置函式
# 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 參數化 的參數化架構,但程式碼少很多
# 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
使用多個裝置參數化¶
以下是一個簡化的實際範例,說明如何使用參數化測試來測試不同 Python 詮釋器之間的物件序列化。我們定義一個 test_basic_objects
函式,它會針對三個參數使用不同的參數組執行
python1
:第一個 Python 詮釋器,執行將物件 pickle 儲存到檔案python2
:第二個詮釋器,執行從檔案 pickle 載入物件obj
:要儲存/載入的物件
"""Module containing a parametrized tests testing cross-python serialization
via the pickle module."""
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
ssssssssssss...ssssssssssss [100%]
========================= short test summary info ==========================
SKIPPED [12] multipython.py:65: 'python3.9' not found
SKIPPED [12] multipython.py:65: 'python3.11' not found
3 passed, 24 skipped in 0.12s
可選實作/匯入的參數化¶
如果您想比較特定 API 的多個實作結果,您可以撰寫測試函式,接收已匯入的實作,並在實作無法匯入/使用的情況下略過。假設我們有一個「基本」實作,而其他(可能是最佳化過的)實作需要提供類似的結果
# 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
檔案中的固定裝置函式是「與工作階段相關聯的」,因為我們不需要匯入超過一次如果您有多個測試函式和略過的匯入,您會看到報告中的
[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。
然後,以詳細模式執行 pytest
,並只使用 basic
標記
$ 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 預期的。