使用PyTest固定装置而不通过它们

时间:2019-06-12 07:34:06

标签: 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
        """
    ....

...

在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

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

def test_logger(other_fixture,logger):

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

1 个答案:

答案 0 :(得分:1)

你可以做

@pytest.mark.usefixtures("logger")
def test_logger(other_fixture):