Fix phantom nodeid for tests collected outside rootpath (#13254)#14417
Open
mnajaf wants to merge 1 commit intopytest-dev:mainfrom
Open
Fix phantom nodeid for tests collected outside rootpath (#13254)#14417mnajaf wants to merge 1 commit intopytest-dev:mainfrom
mnajaf wants to merge 1 commit intopytest-dev:mainfrom
Conversation
…3254) When a test file lives outside rootpath, pytest computes its nodeid by stripping one of the initial paths rather than rootpath (via _check_initialpaths_for_relpath in nodes.py). Config.cwd_relative_nodeid then assumed the nodeid was rootpath-relative and rebuilt the display path as rootpath / nodeid, producing a string that pointed to a file that does not exist on disk (e.g. aa/test_b.py::test_b when the test is actually at ../../b/bb/test_b.py). This change checks whether the synthesized fullpath exists; if not, cwd_relative_nodeid returns the nodeid unchanged. The "<- ..." location hint emitted by the terminal reporter still points to the real file, so users retain the correct location information without the misleading prefix. Adds a regression test under TestNodeIDHandling in test_terminal.py and a 13254.bugfix.rst changelog fragment.
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.
Fixes #13254.
Problem
When pytest collects tests from a path that lives outside
rootpath(for example a
pytest.iniinsidetests/a/aa/while an additionaltest file sits at
tests/b/bb/test_b.py), the displayed node ID forthe out-of-rootpath test is a synthesized path that does not exist on
disk.
Minimal reproducer:
Run from
tests/a/:Before this change:
aa\test_b.pydoes not exist; the file is actually at../../b/bb/test_b.py.Cause
Node.__init__insrc/_pytest/nodes.pyfalls back to_check_initialpaths_for_relpathwhen the collected path cannot beexpressed relative to
rootpath. That yields a bare node ID liketest_b.py::test_b.Config.cwd_relative_nodeidthen assumed everynode ID was rootpath-relative and rebuilt the display string as
rootpath / base_path_part, which in this case points nowhere.Fix
Check whether the synthesized
fullpathactually exists on disk. Ifit does not, the node ID was resolved via the initial paths (not
rootpath), so return it unchanged. The
<- ...location hint renderedby the terminal reporter continues to show the real file location, so
the correct path information is still presented without the misleading
prefix.
After this change the same invocation produces:
Tests
Added
TestNodeIDHandling::test_nodeid_outside_rootpath_not_synthesizedin
testing/test_terminal.py. The test reproduces the issue layoutand asserts that the displayed node ID for the out-of-rootpath test
does not gain a phantom
aa/prefix. The fulltesting/test_config.pyand
testing/test_terminal.pysuites pass locally on Windows(Python 3.13) alongside
testing/test_collection.py,testing/test_session.py,testing/test_nodes.py,testing/test_reports.py, andtesting/test_skipping.py(841 passed total).
Notes
This is my first contribution to pytest; happy to adjust the comment
wording, changelog phrasing, or test placement if preferred.