Skip to content

Report every subtest as a separate JUnit XML testcase - #15236

Open
cjyhjy wants to merge 3 commits into
pytest-dev:mainfrom
cjyhjy:feat/issue-13876-junit-subtests
Open

cjyhjy wants to merge 3 commits into
pytest-dev:mainfrom
cjyhjy:feat/issue-13876-junit-subtests

Conversation

@cjyhjy

@cjyhjy cjyhjy commented Oct 21, 2026 •

Copy link
Copy Markdown

Subtests share their parent's node ID, so JUnit XML currently puts their outcomes in the parent's cached testcase. Passing subtests have no separate entries, and suite counts can disagree with the number of testcase elements. A local reproduction containing fixture and unittest subtests produced 5 testcase elements with tests="13"; this patch produces 13 elements with the same suite count.

Closes #13876.

Each SubtestReport now uses its own reporter outside the parent's setup/call/teardown cache. The testcase name combines the parent name, a report index, and the existing message/parameter description, for example test_parent [subtest 2] [check] (i=1). The index follows report arrival order, which can differ from execution order with deferred unittest reporting on Python 3.10. Repeated contexts remain distinct without changing collection IDs. Existing helpers handle outcome details, family-specific attributes, captured output, and properties.

The parent and every subtest remain in the report. Parent outcomes are preserved, including independent assertions and teardown errors. A fixture parent can therefore count as a failure alongside its failed subtest; unittest currently emits a passing parent call after failed subtests. Each child records its own duration, while the parent retains its inclusive duration according to junit_duration_report. Parent and child times overlap. The documentation explains these accounting choices, which are proposed for review.

Local validation on macOS arm64 / CPython 3.10.21 and 3.13.15:

  • All 12 new regression cases fail against unchanged production sources from c9bf1edf9e0d388f3e7bebc88576e608ffbc40a4.
  • python -m pytest testing/test_junitxml.py testing/test_subtests.py testing/test_unittest.py -q: 271 passed, 8 skipped on Python 3.10 and 270 passed, 9 skipped on Python 3.13. Eight skips require Twisted; the extra skip on 3.13 is asynctest compatibility. The original tests also passed with PYTHONWARNDEFAULTENCODING=1 before the CI follow-up.
  • Coverage includes fixture/unittest, both XML families, pass/fail/skip/xfail, repeated descriptions and executions, escaping, prefixes, location, captured output suppression, duration accounting, parent assertion plus teardown error, ordinary tests, and two xdist workers.
  • Independent pytest subprocess XML before and after the change passes the repository's JUnit XSD. The patched output also has matching testcase counts; ordinary-test attributes and outcome are unchanged, excluding elapsed time.
  • pre-commit run --all-files --show-diff-on-failure: passed, including Ruff, mypy, spelling, and documentation code-block checks. Changed-file checks, including AUTHORS, and git diff --check also passed.
  • Local branch coverage of all added executable lines and branches in both production and test code is 100%, including per-subtest property snapshots. Codecov also reports 100.00% of diff hit (target 100.00%).

The CI run for the current head completed successfully across all configured Windows/Linux/macOS jobs. All 36 PR checks passed. The Linux Python 3.10 full-suite coverage job reported 4707 passed, 50 skipped, 13 xfailed, 7 xpassed, and 2 warnings.

Not run locally: the full pytest suite, other Python versions, Windows/Linux, full Sphinx build, manual Pyright/Pylint hooks, or CI consumer applications. The existing unittest integration supplies zero subtest durations. Existing xdist context deserialization applies repr again, so distributed parameter descriptions can differ from local ones; this patch preserves the received descriptions.

Prepared with Codex assistance for investigation, implementation, regression testing, and this description. The implementation commit includes a Co-authored-by attribution.

  • Include documentation when adding new features.
  • Include new tests or update existing tests when applicable.
  • Allow maintainers to push and squash when merging my commits.
  • Include an issue-closing reference.
  • Credit AI assistance in a Co-authored-by trailer.
  • Add changelog/13876.improvement.rst.
  • Add Cen Junyi(cjyhjy) to AUTHORS in alphabetical order.

jerk cen and others added 2 commits October 21, 2026 07:16
Keep subtest reports out of the parent reporter cache and retain parent outcomes and inclusive durations. Include repeated contexts, unittest, teardown errors, output, and xdist regression coverage.

Refs pytest-dev#13876.

Co-authored-by: Codex <[email protected]>
@pyba-chronographer pyba-chronographer Bot added the bot:chronographer:provided (automation) changelog entry is part of PR label Oct 21, 2026
Check subtest outcomes independently of their arrival order on Python 3.10. Clarify report indices and exercise per-report user property snapshots.

Co-authored-by: Codex <[email protected]>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:chronographer:provided (automation) changelog entry is part of PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

subtests not itemized in Junit XML

1 participant