小编Арт*_*ков的帖子

创建与 pytest.mark.parametrize 兼容的装饰器

我想为用 pytest 编写的测试创建一个装饰器。我的问题是,当调用装饰器时,pytest 会引发装饰器没有参数“test_params”的异常。

装饰器示例:

def decorator_example(fn):

    def create(*args, **kwargs):
        # any code here
        return fn(*args, **kwargs)

return create
Run Code Online (Sandbox Code Playgroud)

测试示例:

@pytest.mark.parametrize(
    "test_params",
    [
        pytest.param("own_parameters")
    ])
@decorator_example
def test_1(self, fixture1, fixture2, test_params):
    pass
Run Code Online (Sandbox Code Playgroud)

并捕获异常:

ValueError: <function create at address> uses no argument 'test_params'
Run Code Online (Sandbox Code Playgroud)

如何创建一个与 pytest 的参数化测试兼容的装饰器?

python decorator pytest

6
推荐指数
1
解决办法
988
查看次数

标签 统计

decorator ×1

pytest ×1

python ×1