使用 PyTest 夹具而不通过它们

ms_*_*tud 6 python pytest

我正在使用 PyTest 框架来编写和运行我的测试。
我已经实现了一个具体的记录器:

class Logger(object):

    class LogFormats:
        ...

    def __init__(self, testname ,setup ,silent=True):
        """
        creating concrete logger for pytest.
        the logger will create a file for the test in specific test directory in quali FS and will
        write to this file all test log output (colored).
        :param: testname: test name - recieved from pytest fixtures (command line parameters)
        :param: setup: test setup - recieved from pytest fixtures (command line parameters)
        :param: silent: log test in silent mode (info only) or not silent mode (everything is logged)
        :param: root_password: password for root user
        """
    ....

...
Run Code Online (Sandbox Code Playgroud)

在 conftest.py 文件中,我编写了在请求此记录器时将调用的函数(创建记录器固定装置)

@pytest.fixture(scope="module",autouse=True)
def logger(request):
    setup = request.config.getoption('--setupname')
    logger = Logger(testname=request.node.name, setup=setup)
    return logger
Run Code Online (Sandbox Code Playgroud)

现在,我的问题是如何使用 pytest 使这个具体记录器全局化?
这意味着我不想将它作为参数传递给测试函数,如下所示:

def test_logger(other_fixture,logger):
Run Code Online (Sandbox Code Playgroud)

但仍然可以在test_logger测试函数中使用它(如全局变量)

mar*_*lli 9

你可以做

@pytest.mark.usefixtures("logger")
def test_logger(other_fixture):
Run Code Online (Sandbox Code Playgroud)