Python 在不通过测试的情况下使用PyTest夹具

Python 在不通过测试的情况下使用PyTest夹具,python,pytest,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 t

我正在使用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
        """
    ....

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

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

def test_logger(other_fixture,logger):
但是仍然能够在
test\u记录器中使用它
test函数(如全局变量)

您可以这样做

@pytest.mark.usefixtures(“记录器”)
def测试记录仪(其他固定装置):

使其成为模块级的全局变量,例如conftest中的
pytest.logger=logger
。谁说pytest的字段名为logger?我不明白你在暗示什么,你将通过上述评论中的行自行分配该字段。变量不能神奇地在任何函数作用域中可用,它必须在某处声明。建议将
记录器
设置为全局变量,方法是在模块级别分配它;当然,您可以选择任何模块,但在我看来,
pytest
最有意义。我想我已经找到了!非常感谢@斯图德女士,你是怎么解决的?
def test_logger(other_fixture,logger):