我想为用pytest编写的测试创建一个装饰器。我的问题是,在调用decorator时,pytest引发了一个异常,decorator没有参数“test_params”。你知道吗
装饰器示例:
def decorator_example(fn):
def create(*args, **kwargs):
# any code here
return fn(*args, **kwargs)
return create
测试示例:
@pytest.mark.parametrize(
"test_params",
[
pytest.param("own_parameters")
])
@decorator_example
def test_1(self, fixture1, fixture2, test_params):
pass
并捕获异常:
ValueError: <function create at address> uses no argument 'test_params'
如何创建与pytest的参数化测试兼容的decorator?你知道吗
这是因为} 来模拟包装函数的签名:
decorator_example
用具有完全不同签名的包装函数create
替换了test_1
函数,打破了pytest
自省(例如,检查create
是否有参数test_params
失败,因为只有*args
和**kwargs
可用)。您需要使用^{Python 2.7兼容性
您可以使用^{} 包。按常规安装
上面的例子是:
或使用^{} :
相关问题 更多 >
编程相关推荐