Repository navigation
fix(subtests): use _pytest.timing instead of time module to prevent negative durations - #15261
Closed
Abhiroop3917 wants to merge 2 commits into
Closed
Abhiroop3917 wants to merge 2 commits into
Abhiroop3917 wants to merge 2 commits into
Conversation
…cess subtests.py called time.time() and time.perf_counter() via the time module directly. When a test mocks time.perf_counter to return 0.0 inside a subtest body, the start-time was already captured from the real clock but the stop-time would read 0.0, producing a large negative duration and a stop-timestamp of 1970-01-01 in JUnit XML output. The rest of pytest uses _pytest.timing (which captures the real function references at import time, before any test can patch them) specifically to prevent this class of bug (first fixed in pytest-dev#185, extended in pytest-dev#7764). Replace the four time.xxx() calls in subtests.py with timing.xxx() to bring subtests in line with the rest of the codebase. Closes pytest-dev#15201
for more information, see https://pre-commit.ci
Member
|
i already had a pr open for that since a while |
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.
Type of changes
AI?
Checklist
Description
When a test mocks
time.perf_counterortime.time(asfreezegun,time-machine, ormonkeypatchdo), subtests report negative durations and a stop timestamp of0.0(1970-01-01) in JUnit XML output. Normal tests are unaffected by the same mocking.Root cause:
subtests.pyimported thetimemodule and calledtime.time()/time.perf_counter()directly. When a mock is installed inside the subtest body, the start-time is already captured from the real clock, but the stop-time reads the patched value (0.0), givingstop − start < 0.The rest of pytest uses
_pytest.timingwhich captures real function references at import time (before any test can patch them). This pattern was introduced specifically to prevent this class of bug (issue #185) and extended in #7764.subtests.pywas added later and missed it.Fix: Replace the four
time.xxx()calls insubtests.pywithtiming.xxx().Reproducer (before fix):
After fix: duration is always non-negative.
closes #15201