Fix timing output for unittest subtests#14453
Closed
Criseda wants to merge 1 commit intopytest-dev:mainfrom
Closed
Fix timing output for unittest subtests#14453Criseda wants to merge 1 commit intopytest-dev:mainfrom
Criseda wants to merge 1 commit intopytest-dev:mainfrom
Conversation
There was a problem hiding this comment.
Pull request overview
Fixes incorrect console_output_style = times output for unittest subtests by ensuring subtest rows show their own measured duration (instead of collapsing to 0.000us due to shared parent nodeid and previously-zero subtest CallInfo timing).
Changes:
- Measure per-
unittest-subTest durations by temporarily wrappingTestCase.subTest()duringTestCaseFunction.runtest(), and use those timings when buildingSubtestReportCallInfo. - Update terminal “times” progress rendering to display
SubtestReport.durationdirectly while keeping existing node-level aggregation for normal reports. - Add a regression test and a changelog entry.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
testing/test_terminal.py |
Adds regression coverage for duplicate unittest subtests under console_output_style=times. |
src/_pytest/unittest.py |
Captures per-subtest durations and propagates them into CallInfo for SubtestReport creation. |
src/_pytest/terminal.py |
Prints subtest timing directly for console_output_style=times while preserving existing aggregation for non-subtests. |
changelog/14412.bugfix.rst |
Documents the bugfix in the changelog. |
AUTHORS |
Adds contributor attribution. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
7420eb7 to
b384d56
Compare
b384d56 to
5ea4690
Compare
Member
|
Thanks, but I think the solution, as is, is too complex and hard to maintain, not being worth the cost. |
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.
Fixes #14412.
When
console_output_style = timeswas used with unittest subtests, subtest rows could display0.000us.There turned out to be two separate issues behind this:
nodeid, but unittest subtest reports all share the same parent testnodeidSubtestReportinstances were created withduration=0This PR changes terminal timing so that
SubtestReportrows display their own report duration directly, while normal test reports continue using the existing node-level aggregation logic.On the unittest side the problem was that stdlib
unittestonly callsaddSubTest()after a subtest has already completed, so pytest does not receive a start timestamp for the subtest body. My approach measures the publicsubTest()context manager boundary duringTestCaseFunction.runtest()and restores the original method infinally.This seemed like the narrowest approach that could provide accurate per-subtest durations while staying within the public unittest API surface.
A regression test was added using duplicate
subTest()contexts so the fix does not rely on subtest context text being unique.Tests run locally:
Result:
I also ran pre-commit on the changed files during commit.