Skip to content

Fix subtest frozen clock - #15278

Closed
Prasanna543210 wants to merge 5 commits into
pytest-dev:mainfrom
Prasanna543210:fix-subtest-frozen-clock
Closed

Prasanna543210 wants to merge 5 commits into
pytest-dev:mainfrom
Prasanna543210:fix-subtest-frozen-clock

Conversation

@Prasanna543210

@Prasanna543210 Prasanna543210 commented Oct 28, 2026 •

Copy link
Copy Markdown

Summary

Fixes subtest timing to use timing.Instant instead of direct calls to time.time() / time.perf_counter().

When a test mocks or freezes time (e.g., via monkeypatch or 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

  • Replaced direct calls to time.time() and time.perf_counter() in _SubTestContextManager with timing.Instant.
  • Used timing.Instant.elapsed() to populate start, stop, and duration in CallInfo.
  • Added a changelog entry.

Testing

  • Added regression test test_subtest_with_mocked_clock in testing/test_subtests.py verifying that freezing the clock during a subtest does not produce negative durations in --junitxml reports.
  • Ran testing/test_subtests.py locally and verified all tests pass.

Prasanna543210 and others added 4 commits October 28, 2026 13:21
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.
@pyba-chronographer pyba-chronographer Bot added the bot:chronographer:provided (automation) changelog entry is part of PR label Oct 28, 2026

Copy link
Copy Markdown
Member

🤖 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 time before subtests.test() starts, so start and stop both read 0.0 and the duration is 0. It passes on main before the fix. The bug needs the patch inside the subtest, as in the #15201 reproducer. #15298 covers that in test_timing_unaffected_by_mocked_time.

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

@Prasanna543210

Copy link
Copy Markdown
Author

Thanks for the explanation, and sorry for the duplicate. I’ll check linked PRs first next time @https://github.com/RonnyPfannschmidt

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.

2 participants