当我装饰具有固定装置作为参数的测试函数时,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/