Repository navigation
Fix subtest frozen clock - #15278
Fix subtest frozen clock#15278Prasanna543210 wants to merge 5 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
|
🤖 Written by Claude Opus 5.5 via Claude Code for the pytest maintainers; I prompted it, it did the work, I read it. Thanks, but this duplicates #15201, which was already being handled in #15205 when this was opened and is now fixed by #15298. After merging main the diff here is only a test and a changelog. The test also doesn't exercise the bug: it patches Closing as superseded. Please check the linked issue for existing PRs before opening one, and keep the AI disclosure (and a Co-authored-by trailer) as #15277 had it. Generated by Claude Code |
|
Thanks for the explanation, and sorry for the duplicate. I’ll check linked PRs first next time @https://github.com/RonnyPfannschmidt |
Summary
Fixes subtest timing to use
timing.Instantinstead of direct calls totime.time()/time.perf_counter().When a test mocks or freezes time (e.g., via
monkeypatchor time-mocking libraries), subtest duration calculations could produce negative values, resulting in invalid negative timestamps (e.g.,time="-0.01") in JUnit XML reports.Changes
time.time()andtime.perf_counter()in_SubTestContextManagerwithtiming.Instant.timing.Instant.elapsed()to populatestart,stop, anddurationinCallInfo.Testing
test_subtest_with_mocked_clockintesting/test_subtests.pyverifying that freezing the clock during a subtest does not produce negative durations in--junitxmlreports.testing/test_subtests.pylocally and verified all tests pass.