Skip to content

Commit edc4ba4

Browse files
committed
New API
1 parent 65395b9 commit edc4ba4

File tree

3 files changed

+17
-22
lines changed

3 files changed

+17
-22
lines changed

src/_pytest/junitxml.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -661,7 +661,7 @@ def pytest_sessionfinish(self) -> None:
661661
skipped=str(self.stats["skipped"]),
662662
tests=str(numtests),
663663
time=f"{duration.elapsed_s:.3f}",
664-
timestamp=duration.start_utc().astimezone().isoformat(),
664+
timestamp=self.suite_start.as_utc().astimezone().isoformat(),
665665
hostname=platform.node(),
666666
)
667667
global_properties = self._get_global_properties_node()

src/_pytest/runner.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -349,8 +349,8 @@ def from_call(
349349
result = None
350350
duration = instant.duration()
351351
return cls(
352-
start=duration.start,
353-
stop=duration.stop,
352+
start=duration.start.time,
353+
stop=duration.stop.time,
354354
duration=duration.elapsed_s,
355355
when=when,
356356
result=result,

src/_pytest/timing.py

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,41 +31,36 @@ class Instant:
3131

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

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

4242
def duration(self) -> Duration:
4343
"""Measure the duration since `Instant` was created."""
44-
return Duration(
45-
start=self._start_time,
46-
elapsed_s=perf_counter() - self._start_perf,
47-
stop=time(),
48-
)
44+
return Duration(start=self, stop=Instant())
45+
46+
def as_utc(self) -> datetime:
47+
"""Instant as UTC datetime."""
48+
return datetime.fromtimestamp(self.time, timezone.utc)
4949

5050

5151
@dataclasses.dataclass(frozen=True)
5252
class Duration:
5353
"""A span of time as measured by `Instant.duration()`."""
5454

5555
# Start time of the duration, as seconds since epoch.
56-
start: float
56+
start: Instant
5757
# Stop time of the duration, as seconds since epoch.
58-
stop: float
59-
# Elapsed time of the duration, in seconds, measured using a performance counter for precise timing.
60-
elapsed_s: float
61-
62-
def start_utc(self) -> datetime:
63-
"""Start time of the duration, as a datetime in UTC."""
64-
return datetime.fromtimestamp(self.start, timezone.utc)
58+
stop: Instant
6559

66-
def stop_utc(self) -> datetime: # pragma: no cover
67-
"""Stop time of the duration, as a datetime in UTC."""
68-
return datetime.fromtimestamp(self.stop, timezone.utc)
60+
@property
61+
def elapsed_s(self) -> float:
62+
"""Elapsed time of the duration, in seconds, measured using a performance counter for precise timing."""
63+
return self.stop.perf_count - self.start.perf_count
6964

7065

7166
@dataclasses.dataclass

0 commit comments

Comments
 (0)