Skip to content
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

Fixtures now register finalizers with all fixtures before them in the stack #6438

Merged
merged 1 commit into from Jan 16, 2020

Conversation

SalmonMode
Copy link
Contributor

Closes #6436

Turns out the root cause was because fixtures were only considering the fixtures requested in their argnames when trying to find out where to register their finalizers at execute time. They weren't considering autouse fixtures, so when this was combined with parameterization, this resulted in fixtures not being torn down when a parameterized, autouse fixture was being torn down to go from one param set to the next.

This also resulted in fixtures being torn down out of order in extreme cases. You can see an example of this in the test that I added (if you run it without this fix).

The solution ultimately required me to have fixtures register their finalizers with all fixtures that came before them in the stack. This list of fixtures came from using request.fixturenames in combination with the parent requests (when relevant) and the fixture's requested argnames to determine a good slicing point. Since this logic could be summed up on its own, I moved it into its own private method with a beefy docstring to explain it.

@SalmonMode
Copy link
Contributor Author

I'm not sure why it's failing. Could it be because I pushed a fix for the string formatting issue before the build was completely done?

@blueyed
Copy link
Contributor

blueyed commented Jan 10, 2020

@SalmonMode
pytest-CI is broken currently (#6426).

Copy link
Contributor

@blueyed blueyed left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome!

src/_pytest/fixtures.py Outdated Show resolved Hide resolved
changelog/6436.bugfix.rst Outdated Show resolved Hide resolved
@SalmonMode
Copy link
Contributor Author

Thanks for catching those! All fixed. Also thanks for the heads up about pytest-CI 😁

Copy link
Member

@nicoddemus nicoddemus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @SalmonMode, awesome contribution!

This is one of the hairest parts of the code base, congratulations in pulling this off.

I used the debugger and things look right to me; also the fact that you didn't need to change any other tests at all further suggests the patch is solid.

Could you please apply my last suggestion and squash/rebase on master (let me know if you would like me to do that for you)?

I would also appreciate if @RonnyPfannschmidt could take a look at this.

changelog/6436.bugfix.rst Outdated Show resolved Hide resolved
@SalmonMode
Copy link
Contributor Author

You got it! All set! 😁

@nicoddemus
Copy link
Member

Thanks @SalmonMode, but there's a bunch of commits which shouldn't be there... can you recheck how you rebased/squashed please?

@SalmonMode SalmonMode force-pushed the consistent-fixture-stack branch 2 times, most recently from fcf2770 to 34f58d9 Compare January 15, 2020 14:39
@SalmonMode
Copy link
Contributor Author

SalmonMode commented Jan 15, 2020

Ah, right you are. I pulled and merged my branch after rebasing without thinking haha. Looks like it's good now. I also made sure to get the very latest master and rebased on that again (since some stuff got committed there since yesterday).

@nicoddemus
Copy link
Member

The rebase now looks correct, thanks!

Could you please also squash the commits into one? The history in that case is not really relevant. 😁

(Let me know if you want me to do it, I will be glad to; our policy is to always ask before push-forcing into forks)

@SalmonMode
Copy link
Contributor Author

SalmonMode commented Jan 15, 2020

Sure thing! All set 😁

I forgot it could be done on the branch itself instead of squashing on merge

@nicoddemus
Copy link
Member

Thanks @SalmonMode appreciate the patience!

@SalmonMode
Copy link
Contributor Author

No worries, @nicoddemus! Sorry for the poke. I just downloaded the new GitHub app and was messing around :P

@nicoddemus
Copy link
Member

Not at all 😁

@nicoddemus nicoddemus merged commit 715f56d into pytest-dev:master Jan 16, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Autouse and parameterization results in fixtures not being torn down, or even being torn down out of order
4 participants