python - 如何使 pytest 固定装置与装饰功能一起使用?

标签 python unit-testing decorator pytest

当我装饰具有固定装置作为参数的测试函数时,py.test 似乎失败了。

def deco(func):

    @functools.wraps(func)
    def wrapper(*args, **kwargs):
        return func(*args, **kwargs)

    return wrapper


@pytest.fixture
def x():
    return 0

@deco
def test_something(x):
    assert x == 0

在这个简单的示例中,我收到以下错误:

TypeError: test_something() takes exactly 1 argument (0 given).

有没有办法解决这个问题,最好不要过多地修 retrofit 饰器? (因为装饰器也用于测试代码之外。)

最佳答案

看起来 functools.wraps 做得不够好,所以它破坏了 py.test 的自省(introspection)。

使用 decorator 创建装饰器包似乎可以解决问题。

import decorator

def deco(func):
    def wrapper(func, *args, **kwargs):
        return func(*args, **kwargs)
    return decorator.decorator(wrapper, func)

关于python - 如何使 pytest 固定装置与装饰功能一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19614658/

相关文章:

mysql - Django unittest - 无法创建表

class - 修改对象实例的 TypeScript 类装饰器

python - Python中从子对象访问父对象数据成员

python - 如何使用 Python 防止进出慢速 Cassandra 节点的流量

python - 整数 NxN 矩阵的精确行列式

python - 为什么在 python 中没有导入就失败了

unit-testing - 在 AOT/release 或配置文件模式下运行 flutter/dart 测试?

python - Python-将函数存储在变量中

angularjs - 单元测试 angular.service ,beforeEach 注入(inject)不执行

Python装饰器可选参数