Skip to content

Commit b84f8f1

Browse files
rcbevansrich
andauthored
test: the interleaved-args probe runs unconditionally instead of skipping (#447)
* test: the interleaved-args probe runs unconditionally instead of skipping pytest-dev/pytest#14968 (the 9.1.x backport of the pytest-dev/pytest#14971 fix) is still unreleased: PyPI's newest pytest is 9.1.1, so there is no version to adopt and the upstream-behavior probe version-gated on (9, 2, 0) skipped on every board, verifying nothing. The upstream bug is specific to fixtures defined in a nested conftest.py (re-collection of the revisited directory builds a second Directory node and fixture registration matches by node identity). Fixtures that do not ride a re-collected Directory node - the root conftest.py and each test module's own fixtures - resolve under the same interleaving; verified against 9.1.1 and 8.4.2 scratch projects, with the nested-conftest negative control erroring on 9.1.1 exactly as upstream reports. Replace the skip with a probe of that scenario: root conftest fixture plus per-module fixtures, asserted under both argument orderings. The nested conftest shape stays covered at the repo level by the two guards (web_admin fixtures registered from tests/conftest.py). A nested-conftest variant of the probe can be added once the pin moves past the release that ships the backport. * ci: board the queue * ci: re-board --------- Co-authored-by: rich <rich@local>
1 parent 60b9f8f commit b84f8f1

1 file changed

Lines changed: 50 additions & 59 deletions

File tree

‎tests/test_web_admin_fixture_resolution.py‎

Lines changed: 50 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,16 @@
1414
Fix: the web_admin fixtures live in :mod:`tests.web_admin._fixtures` and are
1515
registered from ``tests/conftest.py`` (loaded for every test regardless of
1616
argument order), so resolution no longer depends on conftest adjacency. The
17-
guards here pin BOTH orderings, plus a probe of the upstream pytest behavior
18-
that activates once the installed pytest carries the upstream fix.
17+
guards here pin BOTH orderings, plus a probe of pytest's own fixture
18+
resolution under interleaving that avoids the upstream bug's trigger (a
19+
nested conftest) and therefore runs unconditionally.
1920
"""
2021

2122
import os
2223
import subprocess
2324
import sys
2425
from pathlib import Path
2526

26-
import pytest
27-
2827
_REPO_ROOT = Path(__file__).resolve().parents[1]
2928

3029
#: The exact mixed argument list from the issue's verified repro: a web_admin
@@ -45,11 +44,11 @@
4544
)
4645

4746

48-
def _run_pytest(*args: str) -> subprocess.CompletedProcess[str]:
49-
"""Run the repo's own pytest on *args* from the repo root."""
47+
def _run_pytest(cwd: Path, *args: str) -> subprocess.CompletedProcess[str]:
48+
"""Run this interpreter's pytest on *args* from *cwd*."""
5049
return subprocess.run( # noqa: S603 # Why: fixed argv, no shell; the subprocess IS the system under test.
5150
[sys.executable, "-m", "pytest", "-q", "-p", "no:cacheprovider", *args],
52-
cwd=_REPO_ROOT,
51+
cwd=cwd,
5352
env=os.environ.copy(),
5453
capture_output=True,
5554
text=True,
@@ -74,7 +73,7 @@ def test_web_admin_fixtures_survive_interleaved_arguments() -> None:
7473
found". Registration from tests/conftest.py makes resolution independent
7574
of argument order.
7675
"""
77-
_assert_no_fixture_errors(_run_pytest(*_INTERLEAVED_ARGS))
76+
_assert_no_fixture_errors(_run_pytest(_REPO_ROOT, *_INTERLEAVED_ARGS))
7877

7978

8079
def test_web_admin_fixtures_root_file_first_control() -> None:
@@ -83,71 +82,63 @@ def test_web_admin_fixtures_root_file_first_control() -> None:
8382
Passed before the fix too; pinned so the guard covers both shapes and an
8483
over-eager "fix" cannot trade one ordering for the other.
8584
"""
86-
_assert_no_fixture_errors(_run_pytest(*_CONTROL_ARGS))
87-
88-
89-
# Upstream behavior (pytest-dev/pytest#14971): the fix landed on pytest main
90-
# after 9.1.1 and was backported to the 9.1.x branch (pytest-dev/pytest#14968,
91-
# merged 2026-09-06), but no released pytest carried it when this guard was
92-
# written. Bump this once the repo's pin moves to a release that ships the
93-
# backport (the next 9.1.x patch or 9.2.0), and the probe below starts
94-
# asserting instead of skipping.
95-
_UPSTREAM_FIXED = (9, 2, 0)
96-
97-
98-
def _pytest_version_tuple() -> tuple[int, ...]:
99-
return tuple(int(part) for part in pytest.__version__.split(".")[:2])
100-
101-
102-
def test_upstream_pytest_interleaved_conftest_behavior(tmp_path: Path) -> None:
103-
"""Probe the minimal upstream scenario against the installed pytest.
104-
105-
Once pytest carries the upstream fix the probe asserts collection is
106-
order-independent WITHOUT our restructuring; while the installed pytest is
107-
known-affected it skips (the repo-level guards above prove our suite is
108-
order-independent anyway).
85+
_assert_no_fixture_errors(_run_pytest(_REPO_ROOT, *_CONTROL_ARGS))
86+
87+
88+
# The upstream bug is specific to fixtures defined in a NESTED conftest.py:
89+
# re-collection of the revisited directory builds a second ``Directory`` node
90+
# and fixture registration matches by node identity, so the nested conftest's
91+
# registrations vanish. Fixtures that do not ride a re-collected Directory
92+
# node - the root conftest.py and each test module's own fixtures - resolve
93+
# under the same interleaving on every pytest this suite pins (verified
94+
# against 9.1.1 and 8.4.2). The probe below pins that scenario; it cannot
95+
# skip. Once the pin moves past the release that ships the 9.1.x backport
96+
# (pytest-dev/pytest#14968, merged 2026-09-06 but unreleased as of 9.1.1), a
97+
# nested-conftest variant of this scenario can be added to probe the upstream
98+
# fix itself.
99+
def test_pytest_interleaved_arguments_resolve_fixtures(tmp_path: Path) -> None:
100+
"""Pin order-independent fixture resolution WITHOUT a nested conftest.
101+
102+
Mirrors the upstream issue's layout minus the nested conftest: the root
103+
``conftest.py`` defines a fixture and each nested test file defines its
104+
own module-level fixture. A file REVISITED non-adjacently in the argument
105+
list must resolve both, in both orderings.
109106
"""
110-
if _pytest_version_tuple() < _UPSTREAM_FIXED:
111-
pytest.skip(
112-
f"pytest {pytest.__version__} predates the pytest-dev/pytest#14971 fix "
113-
"(backport pytest-dev/pytest#14968); nested conftest fixtures are "
114-
"expected to drop under interleaved file arguments. _UPSTREAM_FIXED "
115-
"must be bumped when the pin moves past it."
116-
)
117-
118107
(tmp_path / "tests" / "services").mkdir(parents=True)
119108
(tmp_path / "tests" / "conftest.py").write_text(
120109
'import pytest\n\n\n@pytest.fixture\ndef root_fixture():\n return "root"\n'
121110
)
122-
(tmp_path / "tests" / "services" / "conftest.py").write_text(
123-
'import pytest\n\n\n@pytest.fixture\ndef nested_fixture():\n return "nested"\n'
124-
)
125111
(tmp_path / "tests" / "services" / "test_a.py").write_text(
126-
"def test_a(nested_fixture):\n assert nested_fixture == 'nested'\n"
112+
"import pytest\n\n\n"
113+
'@pytest.fixture\ndef module_fixture_a():\n return "mod-a"\n\n\n'
114+
"def test_a(module_fixture_a, root_fixture):\n"
115+
" assert module_fixture_a == 'mod-a'\n"
116+
" assert root_fixture == 'root'\n"
127117
)
128118
(tmp_path / "tests" / "services" / "test_b.py").write_text(
129-
"def test_b(nested_fixture):\n assert nested_fixture == 'nested'\n"
119+
"import pytest\n\n\n"
120+
'@pytest.fixture\ndef module_fixture_b():\n return "mod-b"\n\n\n'
121+
"def test_b(module_fixture_b, root_fixture):\n"
122+
" assert module_fixture_b == 'mod-b'\n"
123+
" assert root_fixture == 'root'\n"
130124
)
131125
(tmp_path / "tests" / "test_top.py").write_text(
132126
"def test_top(root_fixture):\n assert root_fixture == 'root'\n"
133127
)
134128

135-
result = subprocess.run( # Why: fixed argv, no shell; the probe IS the system under test.
136-
[
137-
sys.executable,
138-
"-m",
139-
"pytest",
140-
"-q",
141-
"-p",
142-
"no:cacheprovider",
129+
_assert_no_fixture_errors(
130+
_run_pytest(
131+
tmp_path,
143132
"tests/services/test_a.py",
144133
"tests/test_top.py",
145134
"tests/services/test_b.py",
146-
],
147-
cwd=tmp_path,
148-
env=os.environ.copy(),
149-
capture_output=True,
150-
text=True,
151-
timeout=600,
135+
)
136+
)
137+
_assert_no_fixture_errors(
138+
_run_pytest(
139+
tmp_path,
140+
"tests/test_top.py",
141+
"tests/services/test_a.py",
142+
"tests/services/test_b.py",
143+
)
152144
)
153-
_assert_no_fixture_errors(result)

0 commit comments

Comments
 (0)