我进行了以下测试:
@pytest.mark.parametrize(
"nums",
[[3, 1, 5, 4, 2], [2, 6, 4, 3, 1, 5], [1, 5, 6, 4, 3, 2]]
)
def test_cyclic_sort(nums):
pass
@pytest.mark.parametrize(
"nums, missing",
[([4, 0, 3, 1], 2)]
)
def test_find_missing_number(nums, missing):
pass
我想自定义测试名称以包含输入数组。我已阅读 pytest docs、this question 和 this question,但没有人回答以下问题:
@staticmethod
。尝试从 TestClass.static_method
内部使用 TestClass
引用静态方法会在 PyCharm 中出现错误;这样做的正确语法是什么?答案 0 :(得分:1)
当对 ids
关键字使用可调用对象时,它将使用单个参数调用:被参数化的测试参数的值。可调用的 ids
返回一个字符串,该字符串将在方括号中用作测试名称后缀。
如果测试参数化多个值,该函数仍将使用单个参数调用,但每个测试将调用多次。生成的名称将与破折号连接,类似于
"-".join([idfunc(val) for val in parameters])
例如:
test_something[val1-val2-val3]
Here is the join in the pytest source。
要使用静态方法,此语法有效:
class TestExample:
@staticmethod
def idfunc(val):
return f"foo{val}"
@pytest.mark.parametrize(
"x, y",
[
[1, 2],
["a", "b"],
],
ids=idfunc.__func__,
)
def test_vals(self, x, y):
assert x
assert y
这将生成两个测试,如上所述调用 idfunc
四次。
TestExample::test_vals[foo1-foo2]
TestExample::test_vals[fooa-foob]