Repository navigation
subtests: fix negative duration when the clock is mocked - #15277
Closed
Prasanna543210 wants to merge 3 commits into
Closed
Prasanna543210 wants to merge 3 commits into
Prasanna543210 wants to merge 3 commits into
Conversation
When a test freezes or mocks time.time/time.perf_counter (e.g. via monkeypatch), the duration computed for a subtest could end up negative, which produced invalid negative time values in the JUnit XML report. Compute the subtest duration with timing.Instant, which uses the clock values pytest captured itself, so user code that patches the time module no longer affects the reported duration. Add a regression test that freezes the clock inside a subtest and checks that the generated --junitxml report has no negative times.
for more information, see https://pre-commit.ci
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.
Subtests computed their duration from time.time()/perf_counter() directly,
so a test that freezes or mocks the clock (e.g. with monkeypatch) could get
a negative duration. That ends up in the JUnit XML as
time="-0.01", whichis invalid.
This switches the duration to
timing.Instant, which uses the clockfunctions pytest captured at import time, so patching
timein user codeno longer affects it. Same approach the rest of the runner already uses.
Added
test_subtest_with_mocked_clock, which freezestime.timeandtime.perf_counterinside a subtest and checks the generated report has nonegative
timevalues.I ran testing/test_subtests.py, test_junitxml.py and test_unittest.py
locally (Windows, Python 3.11) and they pass.
I used Claude to help with debugging my local setup and with writing the
commit message and this description. I reviewed and tested the change myself.