-
Notifications
You must be signed in to change notification settings - Fork 14
Move script_runner fixture to session scope #90
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
base: master
Are you sure you want to change the base?
Conversation
Requires its dependencies to also me moved to the session scope, `tmp_path` was function-scoped and must be replaced with `tmp_path_factory`. Needed to work well with custom fixtures, Fixes kvas-it#89 ScriptRunner attributes marked as Final to document and enforce them as read-only
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #90 +/- ##
==========================================
- Coverage 99.54% 98.15% -1.39%
==========================================
Files 1 1
Lines 219 217 -2
==========================================
- Hits 218 213 -5
- Misses 1 4 +3 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
Hey, @HexDecimal! Thanks for the fast implementation. I was thinking that what you did wouldn't work, because when we make It turns out that pytest is smarter than I thought, or perhaps I should say "sneakier". I saw that all the tests pass and my first thought was "this can't be". But they do pass, and all the launch modes are correctly set. So what's going on? I did some testing, running PreparationI changed the line before I also passed Finally, I changed the embedded "test file" to have a session scoped initialization fixture that simulates the heavy setup that we want to only happen once: Test run 1I ran Here we can see that the Test run 2Then I removed the marks from the tests so that everything would run with the default launch mode ( I ran the same command and got this: This is expected behavior with Test run 3I tried one more variation, where I only had one test with a mark: The output was like this: This is smart, the runner and our heavy session-scoped fixture are created once for each launch mode, and then the tests are grouped by launch mode. Perhaps they are not really grouped though, pytest just notices when it doesn't need to re-create the fixtures because the one from the previous test will do. Conclusions
Overall, I think that this PR should not break anything for anyone or make anything worse than before. Now it's possible to use Would be good to document how this works. Also, if you have ideas for point 4 above, maybe we can improve this further. Cheers, |
|
It seems the It was enlightening to have a clear confirmation on how this works. You've already put more work into this PR than I did.
This seems like the desired behavior even when the fixture is a heavy one.
That's unfortunate. Though it seems to be an issue with pytest itself and I'd rather report the behavior to pytest than try to make a local workaround. Maybe there's something simple that's being missed. |
Yeah, you're right, it's logical to run the session-scoped fixture with the right launch mode.
My impression is that pytest doesn't reorder the tests to minimise fixture creation. It would be fairly hard to do it well, given that there could be multiple session-but-not-session-scoped fixtures with different rules and unknown execution times. Also I imagine that session-scoped fixtures that need to be created more than once are themselves a bit of a corner case, so pytest tries to make things work correctly but asking it to also be max efficient might be a bit too much. Perhaps we could report and discuss with pytest developers, but for this PR I would add a bit of explanation to the README and then merge it -- it should solve #89 and perhaps help some other people too. |
|
Thank you folks! |
|
I suspect that adding this will give pytest enough context to fix the grouping issues. @kvas-it can you verify that my last commit works correctly? |
Unfortunately there's no change. Pytest doesn't seem to reorder the tests to optimise the use of our fixture. However, I learned today that we can do it ourselves -- there's pytest_collection_modifyitems hook that we can implement to group the tests by launch mode. Reordering the tests seems quite invasive for a plugin like ours, but we could also make it optional (controlled by a command line option) and by default only notice that fixtures are re-created, report any session-scoped fixtures dependent on the runner and how many times they were created and what would be the optimal number of times (maybe print a warning, pytest has mechanisms for that). All of this is great, but also with regard to this PR, my preference is probably to just document the current behavior and merge it. We could recommend something like pytest-order as a workaround for now. Then maybe create the ticket about re-ordering feature and see if we get to implementing it. What do you think? |
Requires its dependencies to also me moved to the session scope,
tmp_pathwas function-scoped and must be replaced withtmp_path_factory.Needed to work well with custom fixtures, Fixes #89
ScriptRunnerattributes marked asFinalto document and enforce them as read-onlyI've also noticed that
rootdirdoes not seem to be used anywhere, but I didn't do anything with this knowledge.script_cwdandrootdircan probably be removed.