-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Teardown order mismatch with parametrized parent fixtures and scopes #12134
Comments
i just ran into this issue after checking out the fixes from #11833 (specifically i am on commit 2607fe8) import pytest
@pytest.fixture(scope='module', autouse=True, params=[7,8,9])
def fixture_autouse(request):
print(f"SETUP FIXTURE AUTOUSE {request.param}")
yield
print(f"TEARDOWN FIXTURE AUTOUSE {request.param}")
@pytest.fixture(scope='module', params=[1,2,3])
def fixture_test(request):
print(f"SETUP FIXTURE TEST {request.param}")
yield
print(f"TEARDOWN FIXTURE TEST {request.param}")
def test_1(fixture_test):
pass
def test_2():
pass results in
i think it's extremely useful to have guaranteed fifo order to teardown wrt setup. for example, these context managers must respect stack in/out ordering: https://github.com/vyperlang/titanoboa/blob/f783a32fbed7a98112acfc494a78e27d1388fa66/boa/test/plugin.py#L54-L63, and running the finalizers out of order is a bug. |
This reverts commit 9102242. the finalizer order is still not working as of pytest (@2607fe8b4706f). cf. pytest-dev/pytest#12134
in this case i think the issue i am having has more to do with the fact that |
Yeah I don't think there's actually any teardown order mismatch in your example? And (running with
So maybe what's surprising you is pytest reordering the parametrization to minimize the number of required setup & teardowns, which I feel like I've read about somewhere but I can't find it now. Regardless, I think discussion about that should be continued in a different issue. |
fixtures that dont share dependencies are not considered stacked in fact they are considered independent to minimize the amount of setups/teardowns and the goal is to minimize setup/teardown of those fixtures with a larger set of fixtures the set of minimal setups/teardowns can be slightly confusing because the reordering makes it slightly more effective than just stacked |
i think considering them as stacked may be useful, as there are cases where setup/teardown needs to be stacked. or at least there could be some way to "request" stacking. |
stacking is requested by dependency |
Sorry, re-reading the comments now I do think this is the same "problem" - which as Ronny says is more of a feature than a bug. So we probably should just update the docs to mention that teardown ordering is not guaranteed to be opposite of setup order (due to minimization of setup/teardowns); and to suggest adding dependencies if that is a hard requirement in parametrization. (and indeed, adding |
This reverts commit 91022425d40076f7cd78323467f4e056c7b9d716. the finalizer order is still not working as of pytest (@2607fe8b4706f). cf. pytest-dev/pytest#12134
Found during discussion of #11833 (I have reduced the size of the repro since I posted it in #11833 (comment))
output:
If we reverse the order of the fixture parameters to
test_1
we get an ordering where the stack is fully respected:What's happening in the first example is relatively straightforward -
test_1
requestsfixture_2
, which is set up and its finalizer is queued.test_1
requestsfixture_1[a]
, which is set up and its finalizer is queued.fixture_1
is parametrized, we run the test again.test_1
requestsfixture_2
- and since it has module scope the value is cached, so no setup is run (and since Don't add fixture finalizer if the value is cached #11833 no finalizer is queued, not that it would make any difference here)test_1
requestsfixture_1[b]
, it has a different cache key, sofixture_1[a]
is torn down, andfixture_1[b]
is setup with its finalizer queued.fixture_1[b]
first, thenfixture_2
, (and finallyfixture_1[a]
is attempted as its still on the stack, but thats also irrelevant here).I'm not even sure this is a bug per se, but I could at least see it being surprising to users as the documentation does not imply that the stack order is ever disrespected:
https://docs.pytest.org/en/8.0.x/how-to/fixtures.html#note-on-finalizer-order
https://docs.pytest.org/en/8.0.x/how-to/fixtures.html#yield-fixtures-recommended
Possible fixes:
fixture_2
whenfixture_1[a]
is torn down.I don't think this is a huge problem though, as I think in most practical cases an end user can work around this issue by changing fixture order, make
fixture_2
depend onfixture_1
, change scoping, etc, which makes me favor option 3 and possibly also 2.Related to #4871
The text was updated successfully, but these errors were encountered: