自动包装/装饰所有pytest单元测试

2024-09-28 18:59:38 发布

您现在位置:Python中文网/ 问答频道 /正文

假设我有一个非常简单的日志装饰器:

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添加到每个测试中。此外,可能有一两个模块我不想配置,所以能够选择加入或退出整个文件或模块会很有帮助


Tags: 模块testreturnpytestmydefargs装饰
1条回答
网友
1楼 · 发布于 2024-09-28 18:59:38

如果您可以将逻辑移动到fixture中,如问题中所述,您可以只使用在顶级conftest.py中定义的自动使用fixture

要增加选择退出某些测试的可能性,您可以定义一个标记,该标记将添加到不应使用夹具的测试中,并在夹具中检查该标记,例如,类似以下内容:

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

test.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

在模块中。这将把标记添加到所有包含的测试中

相关问题 更多 >