-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
fixture scope "package" wrong teardown order with nested packages #11480
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
Comments
Hello Dominik, I analyzed a little bit your case and here are my conclusions. The TheoryThe first step in executing a test suite with pytest implies the gathering of a list of each test method/function that must be called, and store them in a list named Test HooksFor each item in the items list, 3 hooks are executed:
The 1st hookDuring the
The fixture is initialized at the step 4 (Method's fixtures), not sure why. Note: In the The 2nd hookThe The 3rd hookFinally, in the
This time, the teardown of the fixture is not executed at the method's layer, instead it's executed at the package's layer. Note: In the ExamplesNow, let's use your examples to highlight what's going on. Example 1: test file in the same package as conftest
Apply the hooks on
Apply the hooks on
So everything was good, because each test was in the same package as the used conftest. Example 2: test file in a subpackage of conftest's package
Apply the hooks on
Apply the hooks on
As you can see, the conftest is in the package_1, not in nested_dir. Meaning that the teardown of SolutionThe most obvious solution would be to include all the parent packages in the stack, such that the nodes of the |
This is fixed by #11646, will will be included in pytest 8.0.0. The fix is as @robert-iancu-jr said -- now the Dir is nested under the Package. You can test the release candidate with |
I have a following tests structure

with such fixtures:
and
and tests
and
When I run the tests I would expect the package_1 teardown to happen before setup of package_2, but this is not what is happening. Instead, teardown happens at the end. See output:

But when I move the test file out of the nested_dir so the file structure looks like this:


the teardown order seems to be correct.
Anyways I would love to have a correct teardown order for nested tests file structure.
Pip list output:
Operating system:
Macos Ventura 13.0
The text was updated successfully, but these errors were encountered: