Skip to content

Commit bd54116

Browse files
authored
[4.6] Add hostname and timestamp to JUnit XML testsuite tag (#5… (#6332)
[4.6] Add hostname and timestamp to JUnit XML testsuite tag (#5692)
2 parents 943f4ac + 8b9482e commit bd54116

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

changelog/5471.feature.rst

+1
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

+4
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@
1515

1616
import functools
1717
import os
18+
import platform
1819
import re
1920
import sys
2021
import time
22+
from datetime import datetime
2123

2224
import py
2325
import six
@@ -676,6 +678,8 @@ def pytest_sessionfinish(self):
676678
skipped=self.stats["skipped"],
677679
tests=numtests,
678680
time="%.3f" % suite_time_delta,
681+
timestamp=datetime.fromtimestamp(self.suite_start_time).isoformat(),
682+
hostname=platform.node(),
679683
)
680684
logfile.write(Junit.testsuites([suite_node]).unicode(indent=0))
681685
logfile.close()

testing/test_junitxml.py

+26
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
from __future__ import print_function
55

66
import os
7+
import platform
78
import sys
9+
from datetime import datetime
810
from xml.dom import minidom
911

1012
import py
@@ -145,6 +147,30 @@ def test_xpass():
145147
node = dom.find_first_by_tag("testsuite")
146148
node.assert_attr(name="pytest", errors=1, failures=2, skipped=1, tests=5)
147149

150+
def test_hostname_in_xml(self, testdir):
151+
testdir.makepyfile(
152+
"""
153+
def test_pass():
154+
pass
155+
"""
156+
)
157+
result, dom = runandparse(testdir)
158+
node = dom.find_first_by_tag("testsuite")
159+
node.assert_attr(hostname=platform.node())
160+
161+
def test_timestamp_in_xml(self, testdir):
162+
testdir.makepyfile(
163+
"""
164+
def test_pass():
165+
pass
166+
"""
167+
)
168+
start_time = datetime.now()
169+
result, dom = runandparse(testdir)
170+
node = dom.find_first_by_tag("testsuite")
171+
timestamp = datetime.strptime(node["timestamp"], "%Y-%m-%dT%H:%M:%S.%f")
172+
assert start_time <= timestamp < datetime.now()
173+
148174
def test_timing_function(self, testdir):
149175
testdir.makepyfile(
150176
"""

0 commit comments

Comments
 (0)