Pytest:通过函数创建测试参数:使用MarkGenerator触发弃用警告



因此,为了在多个测试中重用测试参数,我有时要进行以下操作:

def parameters():
from _pytest.mark import MarkGenerator
generator = MarkGenerator()
return generator.parametrize("the_argument, the_result", [(1, 2), (2, 4)])
@parameters():
def test_multiplication("the_argument, the_result"):
pass

Pytest现在发出一个PytestDeprecationWarning: A private pytest class or function was used.,文档说这将成为一个硬错误

所以我的问题是,我如何做完全相同的事情,但在一个合法的方式?

像这样:

import pytest
parameters = pytest.mark.parametrize(
"the_argument, the_result",
[(1, 2), (2, 4)],
)
@parameters
def test_multiplication(the_argument, the_result):
pass
@parameters
def test_multiplication_2(the_argument, the_result):
pass

简单解释一下,装饰器和python中的其他东西一样都是对象,所以没有什么可以阻止我们将它们赋值给变量以供以后使用/重用。

最新更新