doctest: fix autouse fixtures possibly not getting picked up#11941
Merged
bluetech merged 2 commits intopytest-dev:mainfrom Feb 7, 2024
Merged
doctest: fix autouse fixtures possibly not getting picked up#11941bluetech merged 2 commits intopytest-dev:mainfrom
bluetech merged 2 commits intopytest-dev:mainfrom
Conversation
Currently, `DoctestModule` does `import_path` on its own. This changes it to use `importtestmodule` as `Module` does. The behavioral changes are: - Much better error messages on import errors. - Handles a few more error cases (see `importtestmodule`). This technically expands the cover of `--doctest-ignore-import-errors` but I think it makes sense. - Considers `pytest_plugins` in the module. - Populates `self.obj` as properly (without double-imports) as is expected from a `PyCollector`. This is also needed for the next commit.
nicoddemus
approved these changes
Feb 6, 2024
Member
nicoddemus
left a comment
There was a problem hiding this comment.
LGTM, appreciate the separate commits (as always).
I noticed this is touching the doctest import part mentioned in #11475...
@flying-sheep any chance of you testing this branch with your reproducer, and see how it affects it?
Fix pytest-dev#11929. Figured out what's going on. We have the following collection tree: ``` <Dir pyspacewar> <Dir src> <Package pyspacewar> <Package tests> <DoctestModule test_main.py> <DoctestItem pyspacewar.tests.test_main.doctest_main> ``` And the `test_main.py` contains an autouse fixture (`fake_game_ui`) that `doctest_main` needs in order to run properly. The fixture doesn't run! It doesn't run because nothing collects the fixtures from (calls `parsefactories()` on) the `test_main.py` `DoctestModule`. How come it only started happening with commit ab63ebb? Turns out it mostly only worked accidentally. Each `DoctestModule` is also collected as a normal `Module`, with the `Module` collected after the `DoctestModule`. For example, if we add a non-doctest test to `test_main.py`, the collection tree looks like this: ``` <Dir pyspacewar> <Dir src> <Package pyspacewar> <Package tests> <DoctestModule test_main.py> <DoctestItem pyspacewar.tests.test_main.doctest_main> <Module test_main.py> <Function test_it> ``` Now, `Module` *does* collect fixtures. When autouse fixtures are collected, they are added to the `_nodeid_autousenames` dict. Before ab63ebb, `DoctestItem` consults `_nodeid_autousenames` at *setup* time. At this point, the `Module` has collected and so it ended up picking the autouse fixture (this relies on another "accident", that the `DoctestModule` and `Module` have the same node ID). After ab63ebb, `DoctestItem` consults `_nodeid_autousenames` at *collection* time (= when it's created). At this point, the `Module` hasn't collected yet, so the autouse fixture is not picked out. The fix is simple -- have `DoctestModule.collect()` call `parsefactories`. From some testing I've done it shouldn't have negative consequences (I hope).
f81c216 to
9cd14b4
Compare
Member
Author
|
@nicoddemus I'm pretty sure #11475 is a different issue from this one. |
bluetech
added a commit
to bluetech/pytest
that referenced
this pull request
Feb 7, 2024
…sefactories doctest: fix autouse fixtures possibly not getting picked up (cherry picked from commit 6c0b6c2)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix #11929. There is also a prerequisite commit, please see the commit message.