I have a fixture in conftest.py
with a function scope.
@pytest.fixture()
def registration_setup(
test_data, # fixture 1
credentials, # fixture 2
deployment # fixture 3
deployment_object # fixture 4
):
# pre-test cleanup
do_cleanup()
yield
# post-test cleanup
do_cleanup()
I use it in a test class like this:
class TestClass:
@pytest.fixture(autouse=True)
def _inventory_cleanup(self, registration_setup):
log('Cleanup Done!')
def test_1():
...
def test_2():
...
def test_3():
...
Now I want to create a new test class where I run the registartion_setup
fixture once for the entire class. The desired behaviour here is, First the pre-test cleanup executes and then all the tests in the new test class execute, followed by the post-test cleanup.
How can I achieve this, thanks for the help.
Huge thanks to @Marco.S for his answer. Just a small additional check to get it to work
def reg_setup(request):
if 'TestClassWhereFixtureShouldRunOnlyOnce' in request.node.nodeid:
if not request.config.first_test_executed:
# pre-test cleanup
do_cleanup()
request.config.first_test_executed = True
yield
if 'test_last' in request.node.nodeid: # Check for the last test case.
# post-test cleanup
do_cleanup()
else:
# pre-test cleanup
do_cleanup()
yield
# post-test cleanup
do_cleanup()