When using @pytest.mark.parametrize('arg', param)
is there a way to find out if the last item in param
is being run? The reason I'm asking is I want to run a cleanup function unique to that test that should only run after the very last iteration of param
.
param = [(1, 'James'), (2, 'John')]
@pytest.mark.parametrize('id, user', param)
def test_myfunc(id, user):
# Do something
print(user)
# Run this only after the last param which would be after (2, 'John')
print('All done!')
I can run a conditional which checks for the value of param
but I was just wondering if pytest has a way for this.
You'll need to perform this logic within a pytest hook, specifically the
pytest_runtest_teardown
hook.Assuming your test looks like the following,
In the root of your test folder, create a
conftest.py
file and place the following,Then when we run it, it produces the following output,
As we can see, the teardown logic was called exactly once, after the final iteration of the test call.