假设我有一个非常简单的日志装饰器:
from functools import wraps
def my_decorator(func):
@wraps(func)
def wrapper(*args, **kwargs):
print(f"{func.__name__} ran with args: {args}, and kwargs: {kwargs}")
result = func(*args, **kwargs)
return result
return wrapper
我可以将此装饰器单独添加到每个pytest单元测试中:
@my_decorator
def test_one():
assert True
@my_decorator
def test_two():
assert 1
如何将此装饰器自动添加到每个pytest单元测试中,从而不必手动添加它?如果我想将它添加到文件中的每个单元测试中,该怎么办?还是在模块中
我的用例是用SQL探查器包装每个测试函数,因此低效的ORM代码会引发错误。使用pytest fixture应该可以工作,但是我有数千个测试,所以最好自动应用包装器,而不是将fixture添加到每个测试中。此外,可能有一两个模块我不想配置,所以能够选择加入或退出整个文件或模块会很有帮助
如果您可以将逻辑移动到fixture中,如问题中所述,您可以只使用在顶级
conftest.py
中定义的自动使用fixture要增加选择退出某些测试的可能性,您可以定义一个标记,该标记将添加到不应使用夹具的测试中,并在夹具中检查该标记,例如,类似以下内容:
conftest.py
test.py
正如@hoefling所指出的,您还可以通过添加以下内容来禁用整个模块的夹具:
在模块中。这将把标记添加到所有包含的测试中
相关问题 更多 >
编程相关推荐