Skip to content

Commit 4417f2c

Browse files
#282: pytest 9.2, and a browser test that lost the harness fails the run at collection (#284)
* #282: a test that a browser file named after one outside browser/ keeps the harness's fixtures It fails on pytest 9.1.1, which drops the browser conftest's fixtures for such a file (pytest-dev/pytest#14635). Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01BvgiHt1n4HBuYDWCz5SBvg * #282: pytest 9.2, and a browser test that lost the harness fails the run at collection pytest 9.1 bound a conftest's fixtures to the first collector of its folder (pytest-dev/pytest#14635, fixed in 9.2.0), so a browser file named after a file outside browser/ lost every browser fixture: no sizes and no DJANGO_ALLOW_ASYNC_UNSAFE, and 52 SynchronousOnlyOperation errors at setup. The run order did not matter. check_harness turns a pytest that drops them again into one usage error at collection. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01BvgiHt1n4HBuYDWCz5SBvg * #282: the guard test's docstring states no count it does not assert Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01BvgiHt1n4HBuYDWCz5SBvg * #282 review: a test drives the collection hook and the first lost test it names; the guard reads fixturenames directly Deleting the hook's check_harness call, or reporting the last lost test, passed both earlier tests. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01BvgiHt1n4HBuYDWCz5SBvg --------- Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 841e9ea commit 4417f2c

4 files changed

Lines changed: 121 additions & 6 deletions

File tree

‎server/pyproject.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ browser = [
2020
dev = [
2121
"factory-boy>=3.3",
2222
"hypothesis>=6.170.11",
23-
"pytest>=8.4",
23+
"pytest>=9.2",
2424
"pytest-django>=4.11",
2525
"pytest-repeat>=0.9.4",
2626
"pytest-timeout>=2.4",

‎server/ui/tests/browser/conftest.py‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,34 @@
9191
)
9292

9393

94+
# pytest 9.1 bound a conftest's fixtures to the first collector of its
95+
# folder, so a browser file named on the command line after a file outside
96+
# browser/ lost every fixture here, and each of its tests failed at setup
97+
# with `SynchronousOnlyOperation` (pytest-dev/pytest#14635, #282). A pytest
98+
# that drops them again fails the run at collection instead.
99+
HARNESS_LOST = (
100+
"{} lost the browser harness's fixtures; pytest-dev/pytest#14635 drops a "
101+
"conftest's fixtures when a file outside its folder is named between two "
102+
"of its files: use pytest 9.2 or later, or name the browser files together"
103+
)
104+
105+
106+
def check_harness(items, here):
107+
"""Raises `HARNESS_LOST` for the first test under `here` that lacks
108+
`_async_unsafe`, which every test there has as an autouse fixture."""
109+
for item in items:
110+
if here in Path(item.fspath).parents and "_async_unsafe" not in (
111+
item.fixturenames
112+
):
113+
raise pytest.UsageError(HARNESS_LOST.format(item.nodeid))
114+
115+
94116
def pytest_collection_modifyitems(items):
95117
here = Path(__file__).parent
96118
for item in items:
97119
if here in Path(item.fspath).parents:
98120
item.add_marker(pytest.mark.browser)
121+
check_harness(items, here)
99122

100123

101124
# A host name that reaches the live server over plain HTTP. `localhost` is a

‎server/ui/tests/browser/test_harness.py‎

Lines changed: 93 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
11
"""The page fixture's own rules (#42 retro)."""
22

33
import os
4+
import re
45
import subprocess
6+
import sys
7+
from pathlib import Path
8+
from types import SimpleNamespace
59

610
import pytest
711
from playwright.sync_api import expect
812

9-
from ui.tests.browser.conftest import InFlight, _settle, close_tab, font_config
13+
from ui.tests.browser.conftest import (
14+
InFlight,
15+
_settle,
16+
check_harness,
17+
close_tab,
18+
font_config,
19+
)
1020

1121

1222
def test_an_aborted_htmx_request_is_no_page_error(page, page_errors, event):
@@ -262,3 +272,85 @@ def test_the_page_draws_the_font_asked_for(page):
262272
).stdout
263273
)
264274
assert drawn_font(page) == [family]
275+
276+
277+
SERVER_DIR = Path(__file__).resolve().parents[3]
278+
# #282's command: a test outside browser/ named between two browser files.
279+
SPLIT_ARGS = [
280+
"ui/tests/browser/test_shell.py",
281+
"ui/tests/test_kiosk_screens_tool.py",
282+
"ui/tests/browser/test_kiosk_names.py",
283+
]
284+
285+
286+
def test_a_browser_file_named_after_one_outside_keeps_the_harness():
287+
"""pytest 9.1 bound a conftest's fixtures to the first collector of its
288+
folder, so a browser file named after a file outside browser/ lost all
289+
of them (pytest-dev/pytest#14635, fixed in 9.2.0): no phone and desktop
290+
sizes, and no `_async_unsafe`, so every test errored at setup with
291+
`SynchronousOnlyOperation` (#282). `--setup-plan` collects and plans the
292+
fixtures without running a test, in its own process, as #282's command
293+
was run."""
294+
env = {k: v for k, v in os.environ.items() if k != "PYTEST_ADDOPTS"}
295+
result = subprocess.run(
296+
[
297+
sys.executable,
298+
"-m",
299+
"pytest",
300+
"-m",
301+
"browser",
302+
"-p",
303+
"no:xdist",
304+
"-p",
305+
"no:cacheprovider",
306+
"--setup-plan",
307+
"-k",
308+
"test_long_names_show_whole",
309+
*SPLIT_ARGS,
310+
],
311+
cwd=SERVER_DIR,
312+
env=env,
313+
capture_output=True,
314+
text=True,
315+
timeout=120,
316+
)
317+
out = result.stdout
318+
assert result.returncode == 0, out + result.stderr
319+
names = "ui/tests/browser/test_kiosk_names.py::test_long_names_show_whole"
320+
assert f"{names}[phone-chromium]" in out
321+
assert f"{names}[desktop-chromium]" in out
322+
assert re.search(r"^ *SETUP +S _async_unsafe$", out, re.M), out
323+
324+
325+
def collected(path, *fixtures):
326+
return SimpleNamespace(
327+
fspath=path,
328+
nodeid=f"{path.name}::t",
329+
fixturenames=fixtures,
330+
add_marker=lambda mark: None,
331+
)
332+
333+
334+
def test_a_browser_test_without_the_harness_fails_the_run():
335+
"""`check_harness` turns #282's setup errors into one usage error at
336+
collection, for a test under browser/ only, and names the first test
337+
that lost the harness."""
338+
here = Path(__file__).parent
339+
kept = collected(here / "test_a.py", "page", "_async_unsafe")
340+
lost = collected(here / "test_b.py", "page")
341+
later = collected(here / "test_d.py", "page")
342+
outside = collected(here.parent / "test_c.py", "page")
343+
check_harness([kept, outside], here)
344+
with pytest.raises(pytest.UsageError, match=r"^test_b\.py::t lost .*#14635"):
345+
check_harness([kept, outside, lost, later], here)
346+
347+
348+
def test_the_collection_hook_runs_the_harness_check():
349+
"""The browser conftest's `pytest_collection_modifyitems` calls
350+
`check_harness` on the items it marks, so a collection that lost the
351+
harness stops there."""
352+
from ui.tests.browser import conftest
353+
354+
here = Path(conftest.__file__).parent
355+
with pytest.raises(pytest.UsageError, match=r"^test_b\.py::t lost "):
356+
conftest.pytest_collection_modifyitems([collected(here / "test_b.py", "page")])

‎server/uv.lock‎

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)