Skip to content

Commit f7e925d

Browse files
authored
Add hostname and timestamp to JUnit XML testsuite tag (#5692)
Add hostname and timestamp to JUnit XML testsuite tag
2 parents 29e336b + cf6632a commit f7e925d

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

changelog/5471.feature.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
JUnit XML now includes a timestamp and hostname in the testsuite tag.

src/_pytest/junitxml.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@
1010
"""
1111
import functools
1212
import os
13+
import platform
1314
import re
1415
import sys
1516
import time
17+
from datetime import datetime
1618

1719
import py
1820

@@ -666,6 +668,8 @@ def pytest_sessionfinish(self):
666668
skipped=self.stats["skipped"],
667669
tests=numtests,
668670
time="%.3f" % suite_time_delta,
671+
timestamp=datetime.fromtimestamp(self.suite_start_time).isoformat(),
672+
hostname=platform.node(),
669673
)
670674
logfile.write(Junit.testsuites([suite_node]).unicode(indent=0))
671675
logfile.close()

testing/test_junitxml.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import os
2+
import platform
3+
from datetime import datetime
24
from xml.dom import minidom
35

46
import py
@@ -139,6 +141,30 @@ def test_xpass():
139141
node = dom.find_first_by_tag("testsuite")
140142
node.assert_attr(name="pytest", errors=1, failures=2, skipped=1, tests=5)
141143

144+
def test_hostname_in_xml(self, testdir):
145+
testdir.makepyfile(
146+
"""
147+
def test_pass():
148+
pass
149+
"""
150+
)
151+
result, dom = runandparse(testdir)
152+
node = dom.find_first_by_tag("testsuite")
153+
node.assert_attr(hostname=platform.node())
154+
155+
def test_timestamp_in_xml(self, testdir):
156+
testdir.makepyfile(
157+
"""
158+
def test_pass():
159+
pass
160+
"""
161+
)
162+
start_time = datetime.now()
163+
result, dom = runandparse(testdir)
164+
node = dom.find_first_by_tag("testsuite")
165+
timestamp = datetime.strptime(node["timestamp"], "%Y-%m-%dT%H:%M:%S.%f")
166+
assert start_time <= timestamp < datetime.now()
167+
142168
def test_timing_function(self, testdir):
143169
testdir.makepyfile(
144170
"""

0 commit comments

Comments
 (0)