Skip to content

Split TestJunitXml outcome counts into their own test - #202597

Closed
ezyang wants to merge 1 commit into
mainfrom
ptq/20261029-pytorch-adhoc-7dcc5b
Closed

ezyang wants to merge 1 commit into
mainfrom
ptq/20261029-pytorch-adhoc-7dcc5b

Conversation

@ezyang

@ezyang ezyang commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Human Note

Claude did this refactor, I made it split it out.

Agent Report

Agent Report

A refactor of TestJunitXml (test/test_testing.py) with no change in which
tests run, pass, or skip, so that #202390 can run the outcome-count check on
every pytest version.

setUpClass used to skip the whole class unless the installed pytest and
pytest-rerunfailures matched the versions the goldens were generated with
(7.3.2 and 14.0, the CI pins), before running the fixture suite. Now it runs
the suite and keeps its result; setUp applies the same version skip, then
fails if the suite produced no XML (what setUpClass raised before). The
<testcase>/<failure>/... counts move out of test_pytest_outcome_shapes
into test_pytest_outcome_counts.

The one cost: the fixture suite now runs (about 3s) even when every test then
skips.

Test Plan

cd test && CUDA_VISIBLE_DEVICES= python test_testing.py -k TestJunitXml
  • pytest 7.3.2: all three tests pass.
  • pytest 9.1.1 and 9.2.0: all three skip, as the old two did.

Authored with an AI assistant (Claude).

Agent Worklog

This PR was generated by ptq with human review.

@ezyang ezyang added the topic: not user facing topic category label Oct 29, 2026
@pytorch-bot

pytorch-bot Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

PR Status: in progress

A maintainer requested changes. Please address their feedback before the PR returns to maintainer review.

To become ready for review, the change must pass automated review and every maintainer comment must be addressed, either with a code change or a reply explaining why no change is needed. A fresh automated review checks that the feedback has been addressed. All CI must finish and Dr. CI must classify every failure as unrelated to your PR. Fix the failures it attributes to your PR (see status below in this comment). Workflows waiting for a maintainer to approve their run do not block this.

To minimize iteration time, feel free to run the pr-review skill from the repo locally. If you address comments without pushing, comment @pytorchbot review to re-run the automated review. To bypass automated review, please add the “no automated review” label.

🟡 AUTOMATED REVIEW - Changes requested:

The PR splits TestJunitXml's outcome-count assertions out of
test_pytest_outcome_shapes into a new test_pytest_outcome_counts, and moves the
golden-version skip and the no-XML check from setUpClass into a per-test setUp.
Recommendation: Request Changes. Both problems are in the new setUp: it leaks
global state when it skips or fails, and putting the skip there works against
the planned follow-up # 202390.

Findings (2):

[major] test/test_testing.py:748
The skip and fail run after `super().setUp()`, which leaks global state.
`common_utils.TestCase.setUp` saves the sparse-invariant state and then calls
`torch.sparse.check_sparse_tensor_invariants.enable()`. It also snapshots grad,
torch-function and fp32-precision state, and `tearDown` restores all of it.
unittest does not call `tearDown` when `setUp` raises SkipTest or fails, and
`TestCase.run`/`_run_custom` do not force it either. So every skip, and every
no-XML failure, leaves sparse invariant checks switched on for the rest of the
process. The skip is the common case: any environment whose pytest isn't the CI
pin (including the 9.1.1/9.2.0 runs in the test plan) now hits it once per test.
Before this PR, both were raised from setUpClass, before any per-test state was
touched. Fix: neither check uses anything that `super().setUp()` sets up, so run
`self._skip_unless_golden_versions()` and the `raw_xml` check before calling
`super().setUp()`. `check_if_enable` already follows this order.

[major] test/test_testing.py:749
Putting the version skip in `setUp` works against the follow-up this refactor is
meant to enable. The description says # 202390 will run
`test_pytest_outcome_counts` on every pytest version. With the skip in the
shared `setUp`, that PR has to make `setUp` check `self._testMethodName` or
restructure `setUp` again, which is the kind of per-test flag this split should
avoid. Fix: call `self._skip_unless_golden_versions()` at the top of each test
that should skip. For now that is all three tests, so this PR's behaviour stays
the same, and # 202390 then only deletes the call from
`test_pytest_outcome_counts`. Only the no-XML failure stays in `setUp`, placed
before `super().setUp()`.

Reviewed commit: 7879242

Review run

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/202597

Note: Links to docs will display an error until the docs builds have been completed.

❗ 1 Active SEVs

There are 1 currently active SEVs. If your PR is affected, please view them below:

✅ No Failures

As of commit 7879242 with merge base 421855c (image):
💚 Looks good so far! There are no failures yet. 💚

🟡 GREEN LIGHT - PR requires human review:

  • Triviality
    • Not Additive tests: the diff rewrites the TestJunitXml class fixture (setUpClass/setUp), which steers what already runs
    • Not Mechanical refactor: before does not equal after, because the version skip moves from setUpClass to per-test setUp
  • Behavior change in the fixture
    • setUpClass now runs the 300s-timeout pytest subprocess even when every test will skip on unpinned pytest versions
    • A subprocess.TimeoutExpired there now errors the class where it used to skip it, and every such run pays the roughly 3s cost the PR body mentions
  • Intent
    • The restructure exists to let #​202390 run test_pytest_outcome_counts on every pytest version; a reviewer should weigh it alongside that change
  • Correctness
    • No bug found: the same three tests keep the same version skip, and missing XML still fails via self.fail with the subprocess output

reason: not_trivial
Reviewed commit: 7879242

Inference job

🐛 Report a wrong verdict

This comment was automatically generated by Dr. CI and updates every 15 minutes.

A refactor with no change in which tests run, pass, or skip, so the next PR
can run the counts check on every pytest version.

setUpClass used to skip the whole class unless the installed pytest and
pytest-rerunfailures matched the versions the goldens were generated with
(7.3.2 and 14.0, the CI pins), before running the fixture suite. Now it runs
the suite and keeps its result, and setUp applies the same version skip, then
fails if the suite produced no XML (what setUpClass raised before). The
<testcase>/<failure>/... counts move out of test_pytest_outcome_shapes into
test_pytest_outcome_counts.

The one cost: the fixture suite now runs (about 3s) even when every test then
skips.

```
cd test && CUDA_VISIBLE_DEVICES= python test_testing.py -k TestJunitXml
```

pytest 7.3.2: all three tests pass. pytest 9.1.1 and 9.2.0: all three skip, as
the old two did.

Authored with an AI assistant (Claude).
@ezyang
ezyang force-pushed the ptq/20261029-pytorch-adhoc-7dcc5b branch from 45e7d1c to 7879242 Compare October 29, 2026 16:16
@ezyang ezyang changed the title Check TestJunitXml outcome counts on every pytest version Split TestJunitXml outcome counts into their own test Oct 29, 2026
@ezyang
ezyang marked this pull request as ready for review October 29, 2026 16:17
@pytorchgreenlight pytorchgreenlight Bot added the GL/human GreenLight marked as needing human review label Oct 29, 2026
@pytorchbot pytorchbot added the in progress PR is being worked on by the author, until the automated review accepts it label Oct 29, 2026

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Automated review of 7879242: changes requested before human review.

The PR splits TestJunitXml's outcome-count assertions out of test_pytest_outcome_shapes into a new test_pytest_outcome_counts, and moves the golden-version skip and the no-XML check from setUpClass into a per-test setUp. Recommendation: Request Changes. Both problems are in the new setUp: it leaks global state when it skips or fails, and putting the skip there works against the planned follow-up # 202390.

🔴 2 blocking findings are attached to the code below.

Please address the findings and push; a new commit normally gets a fresh automated review.

This review is AI-generated and advisory. Each new automated review replaces the previous one.

Comment thread test/test_testing.py
)

def setUp(self) -> None:
super().setUp()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔴 The skip and fail run after super().setUp(), which leaks global state. common_utils.TestCase.setUp saves the sparse-invariant state and then calls torch.sparse.check_sparse_tensor_invariants.enable(). It also snapshots grad, torch-function and fp32-precision state, and tearDown restores all of it. unittest does not call tearDown when setUp raises SkipTest or fails, and TestCase.run/_run_custom do not force it either. So every skip, and every no-XML failure, leaves sparse invariant checks switched on for the rest of the process. The skip is the common case: any environment whose pytest isn't the CI pin (including the 9.1.1/9.2.0 runs in the test plan) now hits it once per test. Before this PR, both were raised from setUpClass, before any per-test state was touched. Fix: neither check uses anything that super().setUp() sets up, so run self._skip_unless_golden_versions() and the raw_xml check before calling super().setUp(). check_if_enable already follows this order.

Comment thread test/test_testing.py

def setUp(self) -> None:
super().setUp()
self._skip_unless_golden_versions()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔴 Putting the version skip in setUp works against the follow-up this refactor is meant to enable. The description says # 202390 will run test_pytest_outcome_counts on every pytest version. With the skip in the shared setUp, that PR has to make setUp check self._testMethodName or restructure setUp again, which is the kind of per-test flag this split should avoid. Fix: call self._skip_unless_golden_versions() at the top of each test that should skip. For now that is all three tests, so this PR's behaviour stays the same, and # 202390 then only deletes the call from test_pytest_outcome_counts. Only the no-XML failure stays in setUp, placed before super().setUp().

@ezyang ezyang closed this Oct 29, 2026
pytorchmergebot pushed a commit that referenced this pull request Oct 30, 2026
## 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 #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: #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 #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.

<details>
<summary>Agent Worklog</summary>

# 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): #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 #202385 in install_inductor_benchmark_deps.sh as stale. Rebased onto origin/main 92264e2 and removed the pin (exact revert of #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.

</details>

---
*This PR was generated by [ptq](https://github.com/drisspg/pt_job_queue) with human review.*
Pull Request resolved: #202390
Approved by: https://github.com/nWEIdia, https://github.com/atalman, https://github.com/huydhn, https://github.com/jeanschmidt
dominikjastrzebskix pushed a commit to dominikjastrzebskix/pytorch that referenced this pull request Nov 5, 2026
…h#202390)

## 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.

<details>
<summary>Agent Worklog</summary>

# 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): 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 pytorch#202385 in install_inductor_benchmark_deps.sh as stale. Rebased onto origin/main 92264e2 and removed the pin (exact revert of 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.

</details>

---
*This PR was generated by [ptq](https://github.com/drisspg/pt_job_queue) 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
dominikjastrzebskix pushed a commit to dominikjastrzebskix/pytorch that referenced this pull request Nov 9, 2026
…h#202390)

## 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.

<details>
<summary>Agent Worklog</summary>

# 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): 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 pytorch#202385 in install_inductor_benchmark_deps.sh as stale. Rebased onto origin/main 92264e2 and removed the pin (exact revert of 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.

</details>

---
*This PR was generated by [ptq](https://github.com/drisspg/pt_job_queue) 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
jeffdaily pushed a commit to ROCm/pytorch that referenced this pull request Dec 1, 2026
…est 9.2 (pytorch#202390) (#3936)

## 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.

<details>
<summary>Agent Worklog</summary>

# 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): 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
pytorch#202385 in install_inductor_benchmark_deps.sh as stale. Rebased onto
origin/main 92264e2 and removed the pin (exact revert of 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.

</details>

---
*This PR was generated by [ptq](https://github.com/drisspg/pt_job_queue)
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)

Co-authored-by: Edward Z. Yang <[email protected]>
jeffdaily pushed a commit to ROCm/pytorch that referenced this pull request Dec 1, 2026
…est 9.2 (pytorch#202390) (#3935)

## 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.

<details>
<summary>Agent Worklog</summary>

# 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): 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
pytorch#202385 in install_inductor_benchmark_deps.sh as stale. Rebased onto
origin/main 92264e2 and removed the pin (exact revert of 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.

</details>

---
*This PR was generated by [ptq](https://github.com/drisspg/pt_job_queue)
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)

Co-authored-by: Edward Z. Yang <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

GL/human GreenLight marked as needing human review in progress PR is being worked on by the author, until the automated review accepts it topic: not user facing topic category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants