Repository navigation
[release/2.14] Make test/conftest.py LogXMLReruns compatible with pytest 9.2 (#202390) - #3935
Merged
Merged
Conversation
…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. Only the test/conftest.py part of the upstream change applies here: release/2.14 has no TestJunitXml tests, and the inductor benchmark pytest pin that upstream reverts was never added to this branch. Upstream: pytorch#202390 (cherry picked from commit 15e3725)
Author
|
Hi @jeffdaily, can you review this PR? It's just a cherry-pick from upstream pytorch |
jeffdaily
approved these changes
Dec 1, 2026
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.
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:Root cause
LogXMLReruns.node_reporterwas a copy of pytest'sLogXML.node_reporter. The copy only differed in building a_NodeReporterRerunsand stored reporters under(report.nodeid, workernode), withnodeidas a string. pytest 9.2 keysnode_reportersby a newNodeIddataclass (report.id), andLogXML.finalizepops with that key. ANodeIdnever 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_NodeReporterRerunssubclasses 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_NodeReporterRerunsexplaining why it exists. pytest.ini setsjunit_logging_reruns = all, and with the stock_NodeReporterevery 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.1in 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 (--subprocesstest ids) only matters for the few test modules thatrun_test.pyruns 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.setUpClassnow 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 oftest_pytest_outcome_shapesintotest_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
setUpClassnow 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) withPYTHONPATHremoved, so the parent's view can be wrong. Asking the parent withimportlib.metadatawould mean:PYTHONPATH, the parent sees pytest, the child doesn't, writes no XML, andsetUpClassraises instead of skipping (the case the automated review hit, where pytest is missing entirely, is the simplest instance).PYTHONPATH, the golden header check compares the goldens against one version while the child produces XML with another, and the golden tests fail.So
setUpClassfirst 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 viaimportlib.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 matchingNo module named pytestin the fixture run's stderr (covers only the missing-pytest case and leaves the golden mismatch) or parsing theplugins:line from pytest's console header (fragile, and only saves those 60 ms).The child also drops
PYTEST_PLUGINSandPYTEST_DISABLE_PLUGIN_AUTOLOAD, as it already dropsPYTEST_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
PYTHONPATHoverlay only changes the parent):PYTHONPATH, orPYTEST_DISABLE_PLUGIN_AUTOLOAD=1Validation
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.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.
The existing golden fixture
test/junit_xml_testdata/pytest_suite.py(13 outcome shapes, normalized the same wayTestJunitXmlintest/test_testing.pydoes):expected/pytest.xml, which was recorded with pytest 7.3.2.The change was reviewed by two fresh agents (correctness and quality) before this update; their findings are addressed above.
lintrunner test/conftest.pyis clean.Agent Worklog
Worklog
_pytest/junitxml.py.node_reportersis now keyed by aNodeIddataclass (report.id);finalizepops(report.id, workernode). conftest's copiednode_reporterused string nodeids, so it fails at the first teardown, not only on reruns.super().node_reporterand set__class__ = _NodeReporterReruns. The XML matches the old behavior under all three versions, serial and with xdist.test_pytree.pypasses under all three. lintrunner is clean.import pytest_rerunfailureswith 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.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)