Skip to content

Fix negative subtest durations when the time module is mocked - #15244

Closed
Nacl-HCl wants to merge 1 commit into
pytest-dev:mainfrom
Nacl-HCl:fix-subtests-mocked-clock-15201
Closed

Nacl-HCl wants to merge 1 commit into
pytest-dev:mainfrom
Nacl-HCl:fix-subtests-mocked-clock-15201

Conversation

@Nacl-HCl

Copy link
Copy Markdown

Subtests measured their duration with time.time() / time.perf_counter()
looked up on the time module, so a test mocking them (for example with
freezegun, time-machine, or monkeypatch) produced negative durations
and 0.0 stop timestamps in reports.

_SubTestContextManager now uses _pytest.timing.Instant() / .elapsed(),
the same helpers regular tests use via runner.py. They bind the real time
functions at import time and are therefore immune to such mocking, which is
exactly what _pytest.timing exists for (see its docstring, issue #185).

Fixes #15201.

Subtests measured time with time.time()/time.perf_counter() looked up on the
time module, so a test mocking them (freezegun, time-machine, monkeypatch)
produced negative durations and 0.0 stop timestamps in reports.

Switch _SubTestContextManager to _pytest.timing.Instant()/elapsed(), the same
helpers regular tests use via runner.py, which bind the real time functions at
import and are immune to such mocking. Fixes pytest-dev#15201.
@pyba-chronographer pyba-chronographer Bot added the bot:chronographer:provided (automation) changelog entry is part of PR label Oct 22, 2026
@RonnyPfannschmidt

Copy link
Copy Markdown
Member

ai misconduct

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.

Subtest durations become negative when time is mocked (subtests.py bypasses _pytest.timing)

2 participants