Description
Originally reported by: Thomas Tanner (BitBucket: ttanner, GitHub: ttanner)
According to the documentation http://pytest.org/latest/fixture.html#fixture-finalization-executing-teardown-code
"The fin function will execute when the last test using the fixture in the module has finished execution."
Either the documentation is wrong, misleading or there is a bug in pytest.
Example:
#!python
from pytest import fixture
@fixture(scope='session')
def fix1(request):
print 'fix1'
def fin(): print '-fix1'
request.addfinalizer(fin)
return 'fix1'
@fixture(scope='session')
def fix2(request):
print 'fix2'
def fin(): print '-fix2'
request.addfinalizer(fin)
return 'fix2'
def test_f1(fix1):
print 'f1',fix1
def test_f2(fix2):
print 'f2',fix2
results in
#!
test.py::test_f1 fix1
f1 fix1
PASSED
test.py::test_f2 fix2
f2 fix2
PASSED-fix2
-fix1
as you can see, fin of fix1 is called not immediately after the last test depending on it (f1), but after all other tests (f2) and fix2's fin.
Just in case pytest won't guarantee finalization of a fixture after all its dependencies have been executed: what would be the best way to ensure mutually-exclusive (session) fixtures are finalized before the next one is setup?