我目前正试图在以下上下文中使用pytest的参数化功能:
我有多个功能,应该使用一组通用的测试用例进行测试。根据测试的函数,相同的测试用例应该通过或xfail。我想出了一个愚蠢的例子来说明这一点:
import pytest
# Functions to test
def sum_int(a, b):
assert isinstance(a, int)
assert isinstance(b, int)
return a + b
def sum_any(a, b):
return a + b
# Universal test cases
TESTCASES = [
"a,b,result", [
(1, 1, 2),
("a", "a", "aa")
]
]
class Tests:
@pytest.mark.parametrize(*TESTCASES, ids=["int_pass", "str_fail"])
def test_sum_int(self, a, b, result):
assert sum_int(a, b) == result
@pytest.mark.parametrize(*TESTCASES, ids=["int_pass", "str_pass"])
def test_sum_any(self, a, b, result):
assert sum_any(a, b) == result
不幸的是,似乎不可能只通过额外的标记(如pytest.mark.xfail(reason=AssertionError)
到parametrize()
,就像可以用ID一样(。
# Does not work
@pytest.mark.parametrize(*TESTCASES,
ids=["int_pass", "str_fail"],
marks=[None, pytest.mark.xfail(reason=AssertionError)])
def test_sum_int(self, a, b, result):
assert sum_int(a, b) == result
实现这一目标的好方法是什么?
我不得不发现问题的答案相对简单。pytest.param
机制允许为特定测试用例指定标记:
@pytest.mark.parametrize(
TESTCASES[0],
[
pytest.param(*args, marks=marks)
for args, marks
in zip(TESTCASES[1], [[], [pytest.mark.xfail(reason=AssertionError)]])
],
ids=["int_pass", "str_fail"],
)
def test_sum_int(self, a, b, result):
assert sum_int(a, b) == result
如果你试图只标记字符串测试的总和,你可以这样做:
# Universal test cases
TESTCASES = [
"a,b,result", [
(1, 1, 2),
pytest.mark.xfail(("a", "a", "aa"))
]
]
在这里阅读https://docs.pytest.org/en/2.8.7/parametrize.html#:~:text=#%20content%20of%20test_expectation.py