Skip to content

Commit 0ed2d79

Browse files
junitxml: add timezone to testsuite timestamp (#12491)
Signed-off-by: joseph-sentry <[email protected]> Co-authored-by: Ronny Pfannschmidt <[email protected]>
1 parent f74e947 commit 0ed2d79

File tree

4 files changed

+10
-4
lines changed

4 files changed

+10
-4
lines changed

AUTHORS

+1
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ Jordan Guymon
213213
Jordan Moldow
214214
Jordan Speicher
215215
Joseph Hunkeler
216+
Joseph Sawaya
216217
Josh Karpel
217218
Joshua Bronson
218219
Jurko Gospodnetić

changelog/7662.improvement.rst

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Added timezone information to the testsuite timestamp in the JUnit XML report.

src/_pytest/junitxml.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from __future__ import annotations
1212

1313
from datetime import datetime
14+
from datetime import timezone
1415
import functools
1516
import os
1617
import platform
@@ -664,7 +665,9 @@ def pytest_sessionfinish(self) -> None:
664665
skipped=str(self.stats["skipped"]),
665666
tests=str(numtests),
666667
time=f"{suite_time_delta:.3f}",
667-
timestamp=datetime.fromtimestamp(self.suite_start_time).isoformat(),
668+
timestamp=datetime.fromtimestamp(self.suite_start_time, timezone.utc)
669+
.astimezone()
670+
.isoformat(),
668671
hostname=platform.node(),
669672
)
670673
global_properties = self._get_global_properties_node()

testing/test_junitxml.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from __future__ import annotations
33

44
from datetime import datetime
5+
from datetime import timezone
56
import os
67
from pathlib import Path
78
import platform
@@ -218,11 +219,11 @@ def test_pass():
218219
pass
219220
"""
220221
)
221-
start_time = datetime.now()
222+
start_time = datetime.now(timezone.utc)
222223
result, dom = run_and_parse(family=xunit_family)
223224
node = dom.find_first_by_tag("testsuite")
224-
timestamp = datetime.strptime(node["timestamp"], "%Y-%m-%dT%H:%M:%S.%f")
225-
assert start_time <= timestamp < datetime.now()
225+
timestamp = datetime.strptime(node["timestamp"], "%Y-%m-%dT%H:%M:%S.%f%z")
226+
assert start_time <= timestamp < datetime.now(timezone.utc)
226227

227228
def test_timing_function(
228229
self, pytester: Pytester, run_and_parse: RunAndParse, mock_timing

0 commit comments

Comments
 (0)