Skip to content
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ Daw-Ran Liou
Debi Mishra
Denis Kirisov
Denivy Braiam Rück
Deysha Rivera
Dheeraj C K
Dhiren Serai
Diego Russo
Expand Down
2 changes: 2 additions & 0 deletions changelog/13384.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fixed an issue where pytest could report negative durations due to the use of `time.time()`.
Replaced `time.time()` with `_pytest.timing.perf_counter()` for more reliable timing.
8 changes: 4 additions & 4 deletions testing/_py/test_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from py import error
from py.path import local

from _pytest.timing import perf_counter
import pytest


Expand Down Expand Up @@ -738,7 +739,6 @@ def test_dump(self, tmpdir, bin):

def test_setmtime(self):
import tempfile
import time

try:
fd, name = tempfile.mkstemp()
Expand All @@ -747,7 +747,7 @@ def test_setmtime(self):
name = tempfile.mktemp()
open(name, "w").close()
try:
mtime = int(time.time()) - 100
mtime = int(perf_counter()) - 100
path = local(name)
assert path.mtime() != mtime
path.setmtime(mtime)
Expand Down Expand Up @@ -1405,15 +1405,15 @@ def test_atime(self, tmpdir):
import time

path = tmpdir.ensure("samplefile")
now = time.time()
now = perf_counter()
atime1 = path.atime()
# we could wait here but timer resolution is very
# system dependent
path.read_binary()
time.sleep(ATIME_RESOLUTION)
atime2 = path.atime()
time.sleep(ATIME_RESOLUTION)
duration = time.time() - now
duration = perf_counter() - now
assert (atime2 - atime1) <= duration

def test_commondir(self, path1):
Expand Down
6 changes: 3 additions & 3 deletions testing/test_pytester.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import os
import subprocess
import sys
import time
from types import ModuleType

from _pytest.config import ExitCode
Expand All @@ -16,6 +15,7 @@
from _pytest.pytester import Pytester
from _pytest.pytester import SysModulesSnapshot
from _pytest.pytester import SysPathsSnapshot
from _pytest.timing import perf_counter
import pytest


Expand Down Expand Up @@ -451,9 +451,9 @@ def test_pytester_run_with_timeout(pytester: Pytester) -> None:

timeout = 120

start = time.time()
start = perf_counter()
result = pytester.runpytest_subprocess(testfile, timeout=timeout)
end = time.time()
end = perf_counter()
duration = end - start

assert result.ret == ExitCode.OK
Expand Down
Loading