Using PyTest fixture without passing them

Viewed 1757

I am using PyTest framework for writing and running my tests.
I have implemented a concrete logger:

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

...

and in the conftest.py file I wrote the function that will be invoked when this logger will be requested (creating a logger fixture)

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

Now, my question is how do I make this concrete logger global using pytest?
Meaning I don't want to pass it as argument to the test function like this:

def test_logger(other_fixture,logger):

but still be able to use it inside the test_logger test function (like global variable)

1 Answers

You could do

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