Skip to content

Commit d52a620

Browse files
committed
Rename Instant.duration() to Instant.elapsed()
While at it, noticed that `Duration.seconds` reads much better than `Duration.elapsed_s`.
1 parent 2923b02 commit d52a620

6 files changed

Lines changed: 16 additions & 16 deletions

File tree

src/_pytest/junitxml.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -642,7 +642,7 @@ def pytest_sessionfinish(self) -> None:
642642
os.makedirs(dirname, exist_ok=True)
643643

644644
with open(self.logfile, "w", encoding="utf-8") as logfile:
645-
duration = self.suite_start.duration()
645+
duration = self.suite_start.elapsed()
646646

647647
numtests = (
648648
self.stats["passed"]
@@ -660,7 +660,7 @@ def pytest_sessionfinish(self) -> None:
660660
failures=str(self.stats["failure"]),
661661
skipped=str(self.stats["skipped"]),
662662
tests=str(numtests),
663-
time=f"{duration.elapsed_s:.3f}",
663+
time=f"{duration.seconds:.3f}",
664664
timestamp=self.suite_start.as_utc().astimezone().isoformat(),
665665
hostname=platform.node(),
666666
)

src/_pytest/pytester.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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(), instant.duration().elapsed_s
1183+
reprec.ret, out.splitlines(), err.splitlines(), instant.elapsed().seconds
11841184
)
11851185
res.reprec = reprec # type: ignore
11861186
return res
@@ -1445,7 +1445,7 @@ def handle_timeout() -> None:
14451445

14461446
with contextlib.suppress(ValueError):
14471447
ret = ExitCode(ret)
1448-
return RunResult(ret, out, err, instant.duration().elapsed_s)
1448+
return RunResult(ret, out, err, instant.elapsed().seconds)
14491449

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

src/_pytest/runner.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -347,11 +347,11 @@ def from_call(
347347
if reraise is not None and isinstance(excinfo.value, reraise):
348348
raise
349349
result = None
350-
duration = instant.duration()
350+
duration = instant.elapsed()
351351
return cls(
352352
start=duration.start.time,
353353
stop=duration.stop.time,
354-
duration=duration.elapsed_s,
354+
duration=duration.seconds,
355355
when=when,
356356
result=result,
357357
excinfo=excinfo,

src/_pytest/terminal.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -789,7 +789,7 @@ def report_collect(self, final: bool = False) -> None:
789789
if not final:
790790
# Only write the "collecting" report every `REPORT_COLLECTING_RESOLUTION`.
791791
if (
792-
self._collect_report_last_write.duration().elapsed_s
792+
self._collect_report_last_write.elapsed().seconds
793793
< REPORT_COLLECTING_RESOLUTION
794794
):
795795
return
@@ -1200,7 +1200,7 @@ def summary_stats(self) -> None:
12001200
if self.verbosity < -1:
12011201
return
12021202

1203-
session_duration = self._session_start.duration()
1203+
session_duration = self._session_start.elapsed()
12041204
(parts, main_color) = self.build_summary_stats_line()
12051205
line_parts = []
12061206

@@ -1215,7 +1215,7 @@ def summary_stats(self) -> None:
12151215
msg = ", ".join(line_parts)
12161216

12171217
main_markup = {main_color: True}
1218-
duration = f" in {format_session_duration(session_duration.elapsed_s)}"
1218+
duration = f" in {format_session_duration(session_duration.seconds)}"
12191219
duration_with_markup = self._tw.markup(duration, **main_markup)
12201220
if display_sep:
12211221
fullwidth += len(duration_with_markup) - len(duration)

src/_pytest/timing.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,16 @@ class Instant:
3131
"""
3232

3333
# Creation time of this instant, using time.time(), to measure actual time.
34-
# Use a `lambda` to initialize the default to correctly get the mocked time via `MockTiming`.
34+
# Note: using a `lambda` to correctly get the mocked time via `MockTiming`.
3535
time: float = dataclasses.field(default_factory=lambda: time(), init=False)
3636

3737
# Performance counter tick of the instant, used to measure precise elapsed time.
38-
# Use a `lambda` to initialize the default to correctly get the mocked time via `MockTiming`.
38+
# Note: using a `lambda` to correctly get the mocked time via `MockTiming`.
3939
perf_count: float = dataclasses.field(
4040
default_factory=lambda: perf_counter(), init=False
4141
)
4242

43-
def duration(self) -> Duration:
43+
def elapsed(self) -> Duration:
4444
"""Measure the duration since `Instant` was created."""
4545
return Duration(start=self, stop=Instant())
4646

@@ -57,8 +57,8 @@ class Duration:
5757
stop: Instant
5858

5959
@property
60-
def elapsed_s(self) -> float:
61-
"""Elapsed time of the duration, in seconds, measured using a performance counter for precise timing."""
60+
def seconds(self) -> float:
61+
"""Elapsed time of the duration in seconds, measured using a performance counter for precise timing."""
6262
return self.stop.perf_count - self.start.perf_count
6363

6464

testing/test_pytester.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -453,10 +453,10 @@ def test_pytester_run_with_timeout(pytester: Pytester) -> None:
453453

454454
instant = _pytest.timing.Instant()
455455
result = pytester.runpytest_subprocess(testfile, timeout=timeout)
456-
duration = instant.duration()
456+
duration = instant.elapsed()
457457

458458
assert result.ret == ExitCode.OK
459-
assert duration.elapsed_s < timeout
459+
assert duration.seconds < timeout
460460

461461

462462
def test_pytester_run_timeout_expires(pytester: Pytester) -> None:

0 commit comments

Comments
 (0)