Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/13254.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed misleading node IDs displayed for tests collected from paths outside ``rootdir``; pytest no longer synthesizes a phantom path by joining ``rootdir`` with the bare node ID in that case.
6 changes: 6 additions & 0 deletions src/_pytest/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1227,6 +1227,12 @@ def cwd_relative_nodeid(self, nodeid: str) -> str:
base_path_part, *nodeid_part = nodeid.split("::")
# Only process path part
fullpath = self.rootpath / base_path_part
if not fullpath.exists():
# nodeid was resolved relative to an initial path rather than
# to rootpath (see ``_check_initialpaths_for_relpath`` in
# ``nodes.py``), so joining rootpath with it gives a phantom
# path that doesn't exist. Leave the nodeid alone (#13254).
return nodeid
relative_path = bestrelpath(self.invocation_params.dir, fullpath)

nodeid = "::".join([relative_path, *nodeid_part])
Expand Down
39 changes: 39 additions & 0 deletions testing/test_terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -3408,6 +3408,45 @@ def test_x(a):
]
)

def test_nodeid_outside_rootpath_not_synthesized(self, pytester: Pytester) -> None:
"""Regression test for #13254.

When tests collected from a path outside ``rootpath`` are reported,
``Config.cwd_relative_nodeid`` should not synthesize a path by joining
``rootpath`` with the bare nodeid, since that produces a string that
does not point to a real file (e.g. ``aa/test_b.py`` when the test
is actually at ``../b/bb/test_b.py``).
"""
# Put pytest.ini under ``a/aa`` so ``rootpath`` is ``a/aa``.
(pytester.path / "a" / "aa").mkdir(parents=True)
(pytester.path / "a" / "aa" / "pytest.ini").write_text(
"[pytest]\n", encoding="utf-8"
)
(pytester.path / "a" / "aa" / "test_a.py").write_text(
"def test_a():\n pass\n", encoding="utf-8"
)
# And a test file living *outside* rootpath.
(pytester.path / "b" / "bb").mkdir(parents=True)
(pytester.path / "b" / "bb" / "test_b.py").write_text(
"def test_b():\n pass\n", encoding="utf-8"
)

# Invoke pytest from ``a`` so ``invocation_params.dir`` differs from
# ``rootpath`` and both invocation paths straddle the rootpath.
os.chdir(pytester.path / "a")
result = pytester.runpytest("./aa", "../b/bb", "-vv")

result.assert_outcomes(passed=2)
# The in-rootpath test keeps its cwd-relative nodeid.
result.stdout.re_match_lines([r".*aa[\\/]test_a\.py::test_a .*PASSED.*"])
# The out-of-rootpath test must be reported by its bare nodeid
# (plus the ``<-`` location hint), never as ``aa/test_b.py`` which
# would be a phantom path that does not exist.
result.stdout.re_match_lines(
[r"^test_b\.py::test_b <- .*b[\\/]bb[\\/]test_b\.py .*PASSED.*"]
)
result.stdout.no_re_match_line(r".*aa[\\/]test_b\.py::test_b.*")


class TestTerminalProgressPlugin:
"""Tests for the TerminalProgressPlugin."""
Expand Down