Skip to content

[release/2.15] Make test/conftest.py LogXMLReruns compatible with pytest 9.2 (#202390) - #3936

Merged
jeffdaily merged 1 commit into
release/2.15from
maradosa/release-2.15-pytest-9.2-conftest
Dec 1, 2026
Merged

jeffdaily merged 1 commit into
release/2.15from
maradosa/release-2.15-pytest-9.2-conftest

Conversation

@amd-maradosa

@amd-maradosa amd-maradosa commented Nov 2, 2026 •

Copy link
Copy Markdown

Human Note

I tried getting rid of the custom class entirely but Claude persuaded me it was useful. So we're just going to subclass from pytest so we can track upstream more easily.

Agent Report

Problem

With pytest 9.2.0, any test run that writes --junit-xml-reruns (every CI test shard) aborts with an INTERNALERROR at the first test's teardown:

File "test/conftest.py", line 254, in pytest_runtest_logreport
  super().pytest_runtest_logreport(report)
File "_pytest/junitxml.py", line 544, in finalize
  reporter = self.node_reporters.pop((node_id, workernode))
KeyError: (NodeId(path='test/test_pytree.py', names=('TestGenericPytree', 'test_aligned_public_apis'), params=None), None)

Root cause

LogXMLReruns.node_reporter was a copy of pytest's LogXML.node_reporter. The copy only differed in building a _NodeReporterReruns and stored reporters under (report.nodeid, workernode), with nodeid as a string. pytest 9.2 keys node_reporters by a new NodeId dataclass (report.id), and LogXML.finalize pops with that key. A NodeId never equals a string, so the pop fails.

Fix

Call super().node_reporter(report) so pytest builds the key and the reporter in whatever form that version uses, check that _NodeReporterReruns subclasses the returned type, then set the reporter's __class__ to _NodeReporterReruns. The class docstring now says it may only override methods, not add state or change how instance data is represented. The subclass only overrides output methods (_prepare_content, _write_content, append_skipped) and adds no state, so switching the class in place is safe, and it removes the copy of pytest internals that went stale. I also added a docstring to _NodeReporterReruns explaining why it exists. pytest.ini sets junit_logging_reruns = all, and with the stock _NodeReporter every testcase, including passing tests that print nothing, gets a headed <system-out> and <system-err> element (skipped tests get both twice). The subclass writes captured output raw and omits empty streams. Regenerating the goldens without the subclass shows exactly that difference. The other option, branching on the pytest version to build the right key type, would keep the copy and break again the next time pytest changes the key.

This also reverts pytorch#202385, which pinned pytest==9.1.1 in the inductor-benchmarks images as a stopgap for this crash (TorchBench's requirements pull in an unpinned pytest). The other pytest 9.2 fixes still open shouldn't affect those images: pytorch#202406 (--subprocess test ids) only matters for the few test modules that run_test.py runs with --subprocess, which are CUDA-init and distributed tests the inductor configs don't run, and pytorch#202412 only restores the teardown part of the RERUNS terminal summary.

Test

TestJunitXml (test/test_testing.py) used to skip on every pytest version except the goldens' (the CI pins), so no test could see this crash. setUpClass now records that skip reason instead of raising and still runs the fixture suite; only the exact golden comparisons skip on other versions. The tag counts move out of test_pytest_outcome_shapes into test_pytest_outcome_counts, which runs on every version and also checks for INTERNALERROR. On pytest 9.2.0 the counts test passes with this change and fails without it; on 7.3.2 (the CI pin) all three tests pass.

Where the versions come from

Running the fixture suite on every version means setUpClass now depends on the environment in ways the old blanket skip hid, so the version checks have to describe the process that actually runs the suite. That process is a child (sys.executable -m pytest) with PYTHONPATH removed, so the parent's view can be wrong. Asking the parent with importlib.metadata would mean:

  • With pytest only on the parent's PYTHONPATH, the parent sees pytest, the child doesn't, writes no XML, and setUpClass raises instead of skipping (the case the automated review hit, where pytest is missing entirely, is the simplest instance).
  • With a different pytest on the parent's PYTHONPATH, the golden header check compares the goldens against one version while the child produces XML with another, and the golden tests fail.

So setUpClass first runs a small probe, _junit_child_versions(), in the child's interpreter and environment: it prints the pytest and pytest-rerunfailures versions (or null) as JSON via importlib.metadata, without importing torch. That one result decides all three things: the class skips when pytest is missing, the counts test skips when pytest-rerunfailures is missing, and the golden header is built from it. The probe costs about 60 ms once per class, next to roughly 3.5 s for the fixture suite run the class already does. The alternatives were matching No module named pytest in the fixture run's stderr (covers only the missing-pytest case and leaves the golden mismatch) or parsing the plugins: line from pytest's console header (fragile, and only saves those 60 ms).

The child also drops PYTEST_PLUGINS and PYTEST_DISABLE_PLUGIN_AUTOLOAD, as it already drops PYTEST_ADDOPTS, so it loads the installed plugins as CI does; with autoload off, pytest-rerunfailures was importable but never loaded and the counts were wrong.

Checked in throwaway environments that really have each version (a PYTHONPATH overlay only changes the parent):

Child environment Result
no pytest class skipped
pytest 9.1.1, no pytest-rerunfailures all three skip
pytest 9.2.0 + pytest-rerunfailures 14.0 counts pass, goldens skip; counts fail with the old conftest
pytest 7.3.2 + pytest-rerunfailures 14.0 all three pass
job venv with pytest 7.3.2 on the parent's PYTHONPATH, or PYTEST_DISABLE_PLUGIN_AUTOLOAD=1 counts pass, goldens skip
cd test && CUDA_VISIBLE_DEVICES= python test_testing.py -k TestJunitXml

Validation

A minimal test file covering pass, skip, a flaky test that passes on rerun, a consistent failure with reruns, and an expected failure, run with --reruns=2 --junit-xml-reruns, serially and with -n 2, under pytest 7.3.2 (requirements-ci pin), 9.1.1 and 9.2.0. pytest-rerunfailures 14.0 and pytest-xdist 3.3.1 are the CI versions. In all six configurations the testcase/rerun/skipped/failure elements of the rerun XML match those the unmodified conftest produces under 9.1.1. Unmodified conftest under 9.2.0 crashes with the KeyError above.

PYTHONPATH=<overlay with pytest==$V pytest-rerunfailures==14.0 pytest-xdist==3.3.1> python -m pytest -q -p no:cacheprovider test_mini.py --reruns=2 [-n 2] --junit-xml-reruns=out/r.xml

Real test file with each pytest version: 251 passed and 251 testcases in the XML, no INTERNALERROR. The same command with the old conftest on 9.2.0 crashes after the first test.

cd test && PYTHONPATH=<overlay pytest==$V> python -m pytest -q -p no:cacheprovider test_pytree.py --reruns=2 -x --junit-xml-reruns=/tmp/rr/r.xml

The existing golden fixture test/junit_xml_testdata/pytest_suite.py (13 outcome shapes, normalized the same way TestJunitXml in test/test_testing.py does):

  • pytest 7.3.2: the new conftest output matches the committed golden expected/pytest.xml, which was recorded with pytest 7.3.2.
  • pytest 9.1.1: the new conftest output is identical to the old conftest's.
  • pytest 9.2.0: the new conftest output is identical to the old conftest's output under 9.1.1. The old conftest crashes with 81 INTERNALERRORs.

The change was reviewed by two fresh agents (correctness and quality) before this update; their findings are addressed above.

lintrunner test/conftest.py is clean.

Agent Worklog

Worklog

  • 2026-10-28: Diffed pytest 9.1.1 and 9.2.0 _pytest/junitxml.py. node_reporters is now keyed by a NodeId dataclass (report.id); finalize pops (report.id, workernode). conftest's copied node_reporter used string nodeids, so it fails at the first teardown, not only on reruns.
  • Reproduced with a minimal test file under overlays of pytest 7.3.2/9.1.1/9.2.0 on the job venv's torch.
  • Fix: delegate to super().node_reporter and set __class__ = _NodeReporterReruns. The XML matches the old behavior under all three versions, serial and with xdist. test_pytree.py passes under all three. lintrunner is clean.
  • Golden fixture junit_xml_testdata/pytest_suite.py: new==golden on 7.3.2, new==old on 9.1.1, new on 9.2.0 == old on 9.1.1.
  • 2026-10-29: (superseded) C1 test change moved to its own PR, see below.
  • 2026-10-29: split the C1 test change into job 20261029-pytorch-adhoc-7dcc5b (new PR below this one); this branch now sits on ptq/20261029-pytorch-adhoc-7dcc5b. Counts test passes on 9.2.0 with this fix. Not pushed.
  • 2026-10-29: now on the refactor 7879242; this PR moves the version skip onto the goldens and runs the counts (plus an INTERNALERROR check) on every version. Commit d334d9d. 9.2.0: counts pass with the fix, fail without.
  • 2026-10-29: shrank the test change: setUpClass unchanged except it records the version-mismatch reason (golden_skip) instead of raising, and keeps proc output; goldens skip on it; counts add an INTERNALERROR check. Commit 980d40eddfd. 7.3.2 all pass; 9.2.0 counts pass (fail without the conftest fix), goldens skip.
  • 2026-10-29: consolidated (Edward OK): Split TestJunitXml outcome counts into their own test pytorch/pytorch#202597 closed, this PR retargeted to main, single commit de4b8db pushed, body Test section updated.
  • 2026-10-29: automated review flagged the pytest==9.1.1 pin from [CI] Pin pytest==9.1.1 in inductor-benchmarks images pytorch/pytorch#202385 in install_inductor_benchmark_deps.sh as stale. Rebased onto origin/main 92264e2 and removed the pin (exact revert of [CI] Pin pytest==9.1.1 in inductor-benchmarks images pytorch/pytorch#202385). TestJunitXml: 9.1.1 venv and 9.2.0 overlay pass (goldens skip); the 7.3.2 golden diffs I saw were my overlay run missing the repo-root pytest.ini; the PR-review agent confirmed old and new conftest match the goldens on 7.3.2 run in place. Not pushed.
  • 2026-10-30: Edward approved; pushed 1d11f93 via git (ssh). PR body not yet regenerated: gh on devgpu013 is logged out (~/.config/gh/hosts.yml emptied 2026-10-30 06:20). Body regenerated with ptq pr on 2026-10-30 after gh re-auth.
  • 2026-10-30: automated review of 1d11f93: without pytest installed, TestJunitXml errored (subprocess ran, no XML) instead of skipping. setUpClass now raises SkipTest first when pytest is missing. Verified with _junit_provenance patched to "pytest missing" (class skipped, subprocess not run); 9.1.1 venv and 9.2.0 overlay unchanged; lint clean.
  • 2026-10-30: per Edward, replaced the provenance-string checks: setUpClass skips when the child stderr has "No module named pytest" (no XML), and the counts test probes import pytest_rerunfailures with the child env. Note: earlier PYTHONPATH-overlay runs of TestJunitXml only changed the parent; the child drops PYTHONPATH and used the venv pytest 9.1.1. Re-verified with symlinked throwaway venvs: 9.2.0 counts pass (fail with origin/main conftest), 7.3.2 all pass, no pytest -> class skipped, no rerunfailures -> all skip.
  • 2026-10-30: replaced the stderr match and import probe with one child probe (_junit_child_versions, JSON of pytest/rerunfailures versions under _JUNIT_CHILD_ENV): it drives the class skip, has_rerunfailures, and the golden provenance (fixes parent/child mismatch under PYTHONPATH). Child env also drops PYTEST_PLUGINS/PYTEST_DISABLE_PLUGIN_AUTOLOAD. Verified: no pytest -> class skip; no rerunfailures -> 3 skips; 9.2.0 counts pass (fail with origin/main conftest); 7.3.2 all pass; job venv with PYTHONPATH=ov-7.3.2 or PYTEST_DISABLE_PLUGIN_AUTOLOAD=1 -> OK.

This PR was generated by ptq with human review.
Pull Request resolved: pytorch#202390
Approved by: https://github.com/nWEIdia, https://github.com/atalman, https://github.com/huydhn, https://github.com/jeanschmidt

(cherry picked from commit 15e3725)

…est 9.2

pytest 9.2 keys LogXML.node_reporters by a NodeId object instead of the
string nodeid. LogXMLReruns.node_reporter kept a copy of the old pytest
code that stored reporters under the string key, so LogXML.finalize fails
with KeyError at the first test's teardown and every test module aborts
after one case when --junit-xml-reruns is used.

Delegate to LogXML.node_reporter so pytest builds the key, then switch the
reporter's class to _NodeReporterReruns.

TestJunitXml now runs the fixture suite on every pytest version; only the
exact golden comparisons skip when the versions differ from the goldens'.
A new test_pytest_outcome_counts test checks the XML tag counts and that
no INTERNALERROR occurred, so this crash is caught on any pytest version.

The inductor benchmark pytest pin that upstream reverts was never added to
this branch, so that part is not included.

Upstream: pytorch#202390
(cherry picked from commit 15e3725)
@amd-maradosa
amd-maradosa force-pushed the maradosa/release-2.15-pytest-9.2-conftest branch from cc3dc46 to 686de23 Compare November 13, 2026 11:26
@amd-maradosa

Copy link
Copy Markdown
Author

Hi @jeffdaily, can you review this PR? It's just a cherry-pick from upstream pytorch

@amd-maradosa amd-maradosa changed the title [release/2.15] Make test/conftest.py LogXMLReruns compatible with pytest 9.2 [release/2.15] Make test/conftest.py LogXMLReruns compatible with pytest 9.2 (#202390) Nov 30, 2026
@jeffdaily
jeffdaily merged commit 7016c87 into release/2.15 Dec 1, 2026
4 checks passed
@jeffdaily
jeffdaily deleted the maradosa/release-2.15-pytest-9.2-conftest branch December 1, 2026 18:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants