[英]Automatically wrap / decorate all pytest unit tests
假设我有一个非常简单的日志装饰器:
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 分析器包装每个测试 function,因此效率低下的 ORM 代码会引发错误。 使用 pytest 夹具应该可以工作,但我有数千个测试,所以自动应用包装器而不是将夹具添加到每个测试中会很好。 此外,可能有一两个模块我不想分析,因此能够选择加入或退出整个文件或模块会很有帮助。
如果您可以将逻辑移动到夹具中,如问题中所述,您只需使用顶级conftest.py
中定义的自动使用夹具。
为了增加选择退出某些测试的可能性,您可以定义一个标记,该标记将添加到不应使用夹具的测试中,并检查夹具中的该标记,例如:
conftest.py
import pytest
def pytest_configure(config):
config.addinivalue_line(
"markers",
"no_profiling: mark test to not use sql profiling"
)
@pytest.fixture(autouse=True)
def sql_profiling(request):
if not request.node.get_closest_marker("no_profiling"):
# do the profiling
yield
测试.py
import pytest
def test1():
pass # will use profiling
@pytest.mark.no_profiling
def test2():
pass # will not use profiling
正如@hoefling 所指出的,您还可以通过添加以下内容来禁用整个模块的固定装置:
pytestmark = pytest.mark.no_profiling
在模块中。 这会将标记添加到所有包含的测试中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.