-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fixture finalization is delayed #687
Comments
@ttanner that session scoped fixtures stay around mostly is kinda intentional, there is no direct concept of mutally exclusive fixtres yet while parametrization of certain fixtures can solve it, its not yet possible in a general way spanning multiple fixtures |
Hi, Old issue here, but I see the same behaviour today with module soped fixtures. Actually, the documentation referenced to above uses the scope='module' as examples it claims that "The fin function will execute when the last test using the fixture in the module has finished execution." The yield type fixtures has similar documentation. I't would be of great use if I could get my module scoped fixtures to be finalized as soon as they aren't needed any more. from time import sleep
from pytest import fixture
@fixture(scope='module')
def fixture1():
print('Setup of fixture1')
yield 'fixture1'
print('Teardown of fixture1')
@fixture(scope='module')
def fixture2():
print('Setup of fixture2')
yield 'fixture2'
print('Teardown of fixture2')
def test_1(fixture1):
print('Running test with {}'.format(fixture1))
sleep(.25)
def test_2(fixture1, fixture2):
print('Running test with {} and {}'.format(fixture1, fixture2))
sleep(0.25)
def test_3(fixture2):
print('Running test with {}'.format(fixture2))
sleep(0.25)
|
I agree we should fix this. I thought the "setupstate" mechanism (which I don't know intimately) was suppose to take care of that. @RonnyPfannschmidt, do you have a comment about that? Is it a bug or a feature not implemented? |
@nicoddemus setupstate cannot fix this, since it has no idea about mutual exclusion in fact, the current fixture mechanism is completely under-powered for such cases |
@RonnyPfannschmidt oh my bad, I completely misunderstood the issue! 😅 AFAIU the main problem with implementing this is that the run-test protocol (which handles fixture setup/run/teardown) currently only knows about the "next" test item. This means that when current item's module and the next-item's module are different the mechanism knows that it can safely tear down module-scoped fixtures at that point. To change the mechanism so that module-scoped fixtures would be tore down when the last test that uses it in a module finishes, it would have to look at all items ahead of current item in the same module to be able to determine if a module-scoped fixture could be torn down at that point. As this requires a non-trivial change in the run-test protocol, I would be happy to at least update the documentation for now to avoid confusion. @RonnyPfannschmidt what do you think? |
To me its not so much about mutual exclusion, more about getting the Ronny Pfannschmidt [email protected] schrieb am Mi., 9. Nov. 2016,
|
@oscarh one of the problems is, that session scope cannot be "left" for example, when iterating over mutually exclusive parameters tear-down is done, however distinct fixtures are not "mutually exclusive" |
@nicoddemus A documentation update would be good for now, as I don't seem to be the only interpretting the documentation as if this is supported. Then I guess that this is more of a feature request than a bug. Would such a feature request be considered? |
If you would like to contribute a PR, we would really appreciate it. 😅
Sure, but TBH I don't think that would happen soon, we have other priorities right now. |
With regards to changing the documentation, in the interests of it describing pytest's behaviour, I could change it to:
This is my understanding of what currently occurs. |
@DuncanBetts, Yes, when the last test in the module finishes execution. |
Improved description of functionality for Issue #687
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:
results in
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?
The text was updated successfully, but these errors were encountered: