Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access autouse fixture without having to add it to the method argument

I have a session scoped fixture in conftest.py

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

This is loaded and works as expected when a 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 fixture (since its autouse enabled) without having to add the extra "log" parameter to test 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' is not defined

like image 535
tjmn Avatar asked May 03 '16 09:05

tjmn


1 Answers

You typically use autouse fixtures when you want to use them for setup/teardown only.

If you need access to the object the fixture returns, you'll need to specify it as an argument like in your first example.

like image 116
The Compiler Avatar answered Oct 06 '22 01:10

The Compiler