-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Replace time.time() with _pytest.timing.perf_counter() #13394
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Replace time.time() with _pytest.timing.perf_counter() #13394
Conversation
for more information, see https://pre-commit.ci
RonnyPfannschmidt
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To ensure we can use the time mocking
We should use module imports only
|
Thank you, the requested changes have been made! |
nicoddemus
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @rivtechprojects, but this is incomplete, only the calls in the test files have been changed: we need to update all the places in production code as well, as listed in the original issue.
Co-authored-by: Bruno Oliveira <[email protected]>
|
Thank you for pointing this out! I see in runner.py we have a start = timing.time() and a precise_start = timing.perf_counter(). What is the recommendation? Keep both and change start/stop to use perf_counter or just use precise_time? |
|
@nicoddemus Apologies for the premature review request. It was unintended. I will update the pr once the above question is addressed. |
Those don't need changing: we need |
|
@nicoddemus @RonnyPfannschmidt Thank you for all the feedback, requested changes have been pushed! However, it appears some tests are now failing related to the changes in junitxml.py. Due to these tests failing, I propose we revert suite_start_time to use to timing.time() or datetime.now(timezone.utc) and add a suite_start_pref variable for measurements. The test for junitxml passes with these adjustments |
…ite start time, but retain perf_counter to measure suite_time_delta for precise calculations.
nicoddemus
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @rivtechprojects!
I will take the liberty of reverting one of the changes as per my suggestion.
|
I will leave this open for a few days in case somebody wants to chime in. Note: we should squash this before merging. |


This pull request addresses issue #13384, where time.time() was causing pytest to report negative durations due to its susceptibility to system clock adjustments.
The following changes were made:
Replaced all occurrences of time.time() with _pytest.timing.perf_counter() for more reliable and monotonic timing.
Updated the relevant tests to use _pytest.timing.perf_counter() where applicable.
Added a changelog entry (13384.bugfix.rst) to document the fix.
Added myself to the AUTHORS file in alphabetical order.
Closes pytest can report negative duration due to usage of
time.time()#13384