Access an autouse device without having to add it to a method argument

I have session binding in conftest.py

@pytest.fixture(scope="session",autouse=True)
def log(request):
    testlog = LogUtil(testconf.LOG_NAME).get()
    return testlog

This loads and works as expected when the test method is defined in mytest.py as follows:

def test_hello_world(self, log):
        log.info("hello world test")

Is there a way to use the device (with its authorization turned on) without the need to add an additional parameter "log" for testing methods?

def test_hello_world(self):
        log.info("hello world test") #log is not found

@pytest.mark.usefixtures("log")
def test_hello_world2(self):
        log.info("hello world test") #log is not found

def test_hello_world3(self,log):
        log.info("hello world test") #log object is accessible here

Error - NameError: name 'log' not defined

+4
source share
1 answer

Usually you use stand-alone devices if you want to use them only for configuration / debugging.

If you need access to the object that the device returns, you need to specify it as an argument, as in the first example.

0
source

All Articles