From 0ed2d79457b499119693588b71233db957e972c0 Mon Sep 17 00:00:00 2001 From: joseph-sentry <136376984+joseph-sentry@users.noreply.github.com> Date: Thu, 27 Jun 2024 08:41:02 -0400 Subject: [PATCH] junitxml: add timezone to testsuite timestamp (#12491) Signed-off-by: joseph-sentry Co-authored-by: Ronny Pfannschmidt --- AUTHORS | 1 + changelog/7662.improvement.rst | 1 + src/_pytest/junitxml.py | 5 ++++- testing/test_junitxml.py | 7 ++++--- 4 files changed, 10 insertions(+), 4 deletions(-) create mode 100644 changelog/7662.improvement.rst diff --git a/AUTHORS b/AUTHORS index 5ae80eb02d3..ad7fccf6d59 100644 --- a/AUTHORS +++ b/AUTHORS @@ -213,6 +213,7 @@ Jordan Guymon Jordan Moldow Jordan Speicher Joseph Hunkeler +Joseph Sawaya Josh Karpel Joshua Bronson Jurko Gospodnetić diff --git a/changelog/7662.improvement.rst b/changelog/7662.improvement.rst new file mode 100644 index 00000000000..b6ae1ba7e4c --- /dev/null +++ b/changelog/7662.improvement.rst @@ -0,0 +1 @@ +Added timezone information to the testsuite timestamp in the JUnit XML report. diff --git a/src/_pytest/junitxml.py b/src/_pytest/junitxml.py index a525f1d14de..3a2cb59a6c1 100644 --- a/src/_pytest/junitxml.py +++ b/src/_pytest/junitxml.py @@ -11,6 +11,7 @@ from __future__ import annotations from datetime import datetime +from datetime import timezone import functools import os import platform @@ -664,7 +665,9 @@ def pytest_sessionfinish(self) -> None: skipped=str(self.stats["skipped"]), tests=str(numtests), time=f"{suite_time_delta:.3f}", - timestamp=datetime.fromtimestamp(self.suite_start_time).isoformat(), + timestamp=datetime.fromtimestamp(self.suite_start_time, timezone.utc) + .astimezone() + .isoformat(), hostname=platform.node(), ) global_properties = self._get_global_properties_node() diff --git a/testing/test_junitxml.py b/testing/test_junitxml.py index 67234302a89..fd1fecb54f1 100644 --- a/testing/test_junitxml.py +++ b/testing/test_junitxml.py @@ -2,6 +2,7 @@ from __future__ import annotations from datetime import datetime +from datetime import timezone import os from pathlib import Path import platform @@ -218,11 +219,11 @@ def test_pass(): pass """ ) - start_time = datetime.now() + start_time = datetime.now(timezone.utc) result, dom = run_and_parse(family=xunit_family) node = dom.find_first_by_tag("testsuite") - timestamp = datetime.strptime(node["timestamp"], "%Y-%m-%dT%H:%M:%S.%f") - assert start_time <= timestamp < datetime.now() + timestamp = datetime.strptime(node["timestamp"], "%Y-%m-%dT%H:%M:%S.%f%z") + assert start_time <= timestamp < datetime.now(timezone.utc) def test_timing_function( self, pytester: Pytester, run_and_parse: RunAndParse, mock_timing