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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With