参数化测试¶
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))
这意味着如果我们不通过,我们只运行2个测试 --all
:
$ 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
选择要运行的特定案例,当某个案例失败时,它们还将识别该特定案例。使用运行pytest --collect-only
将显示生成的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-6.x.y, py-1.x.y, pluggy-0.x.y
cachedir: $PYTHON_PREFIX/.pytest_cache
rootdir: $REGENDOC_TMPDIR
collected 8 items
<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和实际数据,而不是单独列出它们。
“测试方案”的快速端口¶
这里有一个运行测试的快速端口 test scenarios 是标准UnitTest框架的Robert Collins的附加组件。我们只需要稍微工作一下,就可以为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-6.x.y, py-1.x.y, pluggy-0.x.y
cachedir: $PYTHON_PREFIX/.pytest_cache
rootdir: $REGENDOC_TMPDIR
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-6.x.y, py-1.x.y, pluggy-0.x.y
cachedir: $PYTHON_PREFIX/.pytest_cache
rootdir: $REGENDOC_TMPDIR
collected 4 items
<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-6.x.y, py-1.x.y, pluggy-0.x.y
cachedir: $PYTHON_PREFIX/.pytest_cache
rootdir: $REGENDOC_TMPDIR
collected 2 items
<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 0xdeadbeef>
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
参数当参数化测试时,允许在将值传递给测试之前使用接收值的夹具对测试进行参数化:
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
它使用两个固定装置: x
和 y
. 这里我们给间接的列表,它包含了设备的名称 x
. 间接参数将仅应用于此参数,并且 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-6.x.y, py-1.x.y, pluggy-0.x.y -- $PYTHON_PREFIX/bin/python
cachedir: $PYTHON_PREFIX/.pytest_cache
rootdir: $REGENDOC_TMPDIR
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 0xdeadbeef>, 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.5", "python3.6", "python3.7"]
@pytest.fixture(params=pythonlist)
def python1(request, tmpdir):
picklefile = tmpdir.join("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.dirpath("dump.py")
dumpfile.write(
textwrap.dedent(
r"""
import pickle
f = open({!r}, 'wb')
s = pickle.dump({!r}, f, protocol=2)
f.close()
""".format(
str(self.picklefile), obj
)
)
)
subprocess.check_call((self.pythonpath, str(dumpfile)))
def load_and_is_true(self, expression):
loadfile = self.picklefile.dirpath("load.py")
loadfile.write(
textwrap.dedent(
r"""
import pickle
f = open({!r}, 'rb')
obj = pickle.load(f)
f.close()
res = eval({!r})
if not res:
raise SystemExit(1)
""".format(
str(self.picklefile), expression
)
)
)
print(loadfile)
subprocess.check_call((self.pythonpath, str(loadfile)))
@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:29: 'python3.5' not found
SKIPPED [12] multipython.py:29: 'python3.7' 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-6.x.y, py-1.x.y, pluggy-0.x.y
cachedir: $PYTHON_PREFIX/.pytest_cache
rootdir: $REGENDOC_TMPDIR
collected 2 items
test_module.py .s [100%]
========================= short test summary info ==========================
SKIPPED [1] conftest.py:12: 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-6.x.y, py-1.x.y, pluggy-0.x.y -- $PYTHON_PREFIX/bin/python
cachedir: $PYTHON_PREFIX/.pytest_cache
rootdir: $REGENDOC_TMPDIR
collecting ... collected 14 items / 11 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, 11 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 decorator编写参数化测试,其中一些测试引发异常,而另一些则不引发异常。
定义无操作上下文管理器很有帮助 does_not_raise
作为对…的补充 raises
. 例如:
from contextlib import contextmanager
import pytest
@contextmanager
def does_not_raise():
yield
@pytest.mark.parametrize(
"example_input,expectation",
[
(3, does_not_raise()),
(2, does_not_raise()),
(1, does_not_raise()),
(0, pytest.raises(ZeroDivisionError)),
],
)
def test_division(example_input, expectation):
"""Test how much I know division."""
with expectation:
assert (6 / example_input) is not None
在上面的示例中,前三个测试用例应该正常运行,而第四个测试用例应该提高 ZeroDivisionError
.
如果您只支持python 3.7+,那么只需使用 nullcontext
定义 does_not_raise
:
from contextlib import nullcontext as does_not_raise
或者,如果您支持Python 3.3+,您可以使用:
from contextlib import ExitStack as does_not_raise
或者,如果需要,您可以 pip install contextlib2
并使用:
from contextlib2 import nullcontext as does_not_raise