Skip to content

Commit 8173aa8

Browse files
Replace explicit time() calls with _pytest.timing (#13394)
Fix #13384 --------- Co-authored-by: Bruno Oliveira <bruno@soliv.dev>
1 parent ca3a132 commit 8173aa8

File tree

7 files changed

+20
-17
lines changed

7 files changed

+20
-17
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ Daw-Ran Liou
130130
Debi Mishra
131131
Denis Kirisov
132132
Denivy Braiam Rück
133+
Deysha Rivera
133134
Dheeraj C K
134135
Dhiren Serai
135136
Diego Russo

changelog/13384.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed an issue where pytest could report negative durations.

src/_pytest/junitxml.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -637,15 +637,16 @@ def pytest_internalerror(self, excrepr: ExceptionRepr) -> None:
637637

638638
def pytest_sessionstart(self) -> None:
639639
self.suite_start_time = timing.time()
640+
self.suite_start_perf = timing.perf_counter()
640641

641642
def pytest_sessionfinish(self) -> None:
642643
dirname = os.path.dirname(os.path.abspath(self.logfile))
643644
# exist_ok avoids filesystem race conditions between checking path existence and requesting creation
644645
os.makedirs(dirname, exist_ok=True)
645646

646647
with open(self.logfile, "w", encoding="utf-8") as logfile:
647-
suite_stop_time = timing.time()
648-
suite_time_delta = suite_stop_time - self.suite_start_time
648+
suite_stop_perf = timing.perf_counter()
649+
suite_time_delta = suite_stop_perf - self.suite_start_perf
649650

650651
numtests = (
651652
self.stats["passed"]

src/_pytest/pytester.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1150,7 +1150,7 @@ def runpytest_inprocess(
11501150

11511151
if syspathinsert:
11521152
self.syspathinsert()
1153-
now = timing.time()
1153+
now = timing.perf_counter()
11541154
capture = _get_multicapture("sys")
11551155
capture.start_capturing()
11561156
try:
@@ -1180,7 +1180,7 @@ class reprec: # type: ignore
11801180

11811181
assert reprec.ret is not None
11821182
res = RunResult(
1183-
reprec.ret, out.splitlines(), err.splitlines(), timing.time() - now
1183+
reprec.ret, out.splitlines(), err.splitlines(), timing.perf_counter() - now
11841184
)
11851185
res.reprec = reprec # type: ignore
11861186
return res
@@ -1408,7 +1408,7 @@ def run(
14081408
print(" in:", Path.cwd())
14091409

14101410
with p1.open("w", encoding="utf8") as f1, p2.open("w", encoding="utf8") as f2:
1411-
now = timing.time()
1411+
now = timing.perf_counter()
14121412
popen = self.popen(
14131413
cmdargs,
14141414
stdin=stdin,
@@ -1445,7 +1445,7 @@ def handle_timeout() -> None:
14451445

14461446
with contextlib.suppress(ValueError):
14471447
ret = ExitCode(ret)
1448-
return RunResult(ret, out, err, timing.time() - now)
1448+
return RunResult(ret, out, err, timing.perf_counter() - now)
14491449

14501450
def _dump_lines(self, lines, fp):
14511451
try:

src/_pytest/terminal.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -769,7 +769,7 @@ def pytest_collection(self) -> None:
769769
if self.isatty:
770770
if self.config.option.verbose >= 0:
771771
self.write("collecting ... ", flush=True, bold=True)
772-
self._collect_report_last_write = timing.time()
772+
self._collect_report_last_write = timing.perf_counter()
773773
elif self.config.option.verbose >= 1:
774774
self.write("collecting ... ", flush=True, bold=True)
775775

@@ -789,7 +789,7 @@ def report_collect(self, final: bool = False) -> None:
789789

790790
if not final:
791791
# Only write "collecting" report every 0.5s.
792-
t = timing.time()
792+
t = timing.perf_counter()
793793
if (
794794
self._collect_report_last_write is not None
795795
and self._collect_report_last_write > t - REPORT_COLLECTING_RESOLUTION
@@ -823,7 +823,7 @@ def report_collect(self, final: bool = False) -> None:
823823
@hookimpl(trylast=True)
824824
def pytest_sessionstart(self, session: Session) -> None:
825825
self._session = session
826-
self._sessionstarttime = timing.time()
826+
self._sessionstarttime = timing.perf_counter()
827827
if not self.showheader:
828828
return
829829
self.write_sep("=", "test session starts", bold=True)
@@ -1202,7 +1202,7 @@ def summary_stats(self) -> None:
12021202
if self.verbosity < -1:
12031203
return
12041204

1205-
session_duration = timing.time() - self._sessionstarttime
1205+
session_duration = timing.perf_counter() - self._sessionstarttime
12061206
(parts, main_color) = self.build_summary_stats_line()
12071207
line_parts = []
12081208

testing/_py/test_local.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from py import error
1313
from py.path import local
1414

15+
import _pytest.timing
1516
import pytest
1617

1718

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

739740
def test_setmtime(self):
740741
import tempfile
741-
import time
742742

743743
try:
744744
fd, name = tempfile.mkstemp()
@@ -747,7 +747,7 @@ def test_setmtime(self):
747747
name = tempfile.mktemp()
748748
open(name, "w").close()
749749
try:
750-
mtime = int(time.time()) - 100
750+
mtime = int(_pytest.timing.time()) - 100
751751
path = local(name)
752752
assert path.mtime() != mtime
753753
path.setmtime(mtime)
@@ -1405,15 +1405,15 @@ def test_atime(self, tmpdir):
14051405
import time
14061406

14071407
path = tmpdir.ensure("samplefile")
1408-
now = time.time()
1408+
now = _pytest.timing.perf_counter()
14091409
atime1 = path.atime()
14101410
# we could wait here but timer resolution is very
14111411
# system dependent
14121412
path.read_binary()
14131413
time.sleep(ATIME_RESOLUTION)
14141414
atime2 = path.atime()
14151415
time.sleep(ATIME_RESOLUTION)
1416-
duration = time.time() - now
1416+
duration = _pytest.timing.perf_counter() - now
14171417
assert (atime2 - atime1) <= duration
14181418

14191419
def test_commondir(self, path1):

testing/test_pytester.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import os
55
import subprocess
66
import sys
7-
import time
87
from types import ModuleType
98

109
from _pytest.config import ExitCode
@@ -16,6 +15,7 @@
1615
from _pytest.pytester import Pytester
1716
from _pytest.pytester import SysModulesSnapshot
1817
from _pytest.pytester import SysPathsSnapshot
18+
import _pytest.timing
1919
import pytest
2020

2121

@@ -451,9 +451,9 @@ def test_pytester_run_with_timeout(pytester: Pytester) -> None:
451451

452452
timeout = 120
453453

454-
start = time.time()
454+
start = _pytest.timing.perf_counter()
455455
result = pytester.runpytest_subprocess(testfile, timeout=timeout)
456-
end = time.time()
456+
end = _pytest.timing.perf_counter()
457457
duration = end - start
458458

459459
assert result.ret == ExitCode.OK

0 commit comments

Comments
 (0)