From 8b9482e39c932b1d32de6e4007f54e9980555306 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Mon, 5 Aug 2019 10:19:15 -0300 Subject: [PATCH] Add hostname and timestamp to JUnit XML testsuite tag (#5692) Add hostname and timestamp to JUnit XML testsuite tag Conflicts: testing/test_junitxml.py --- changelog/5471.feature.rst | 1 + src/_pytest/junitxml.py | 4 ++++ testing/test_junitxml.py | 26 ++++++++++++++++++++++++++ 3 files changed, 31 insertions(+) create mode 100644 changelog/5471.feature.rst diff --git a/changelog/5471.feature.rst b/changelog/5471.feature.rst new file mode 100644 index 00000000000..154b64ea763 --- /dev/null +++ b/changelog/5471.feature.rst @@ -0,0 +1 @@ +JUnit XML now includes a timestamp and hostname in the testsuite tag. diff --git a/src/_pytest/junitxml.py b/src/_pytest/junitxml.py index 6f21a0638d8..7858d8f3165 100644 --- a/src/_pytest/junitxml.py +++ b/src/_pytest/junitxml.py @@ -15,9 +15,11 @@ import functools import os +import platform import re import sys import time +from datetime import datetime import py import six @@ -676,6 +678,8 @@ def pytest_sessionfinish(self): skipped=self.stats["skipped"], tests=numtests, time="%.3f" % suite_time_delta, + timestamp=datetime.fromtimestamp(self.suite_start_time).isoformat(), + hostname=platform.node(), ) logfile.write(Junit.testsuites([suite_node]).unicode(indent=0)) logfile.close() diff --git a/testing/test_junitxml.py b/testing/test_junitxml.py index a09f16fb720..7d6dfde9bc8 100644 --- a/testing/test_junitxml.py +++ b/testing/test_junitxml.py @@ -4,7 +4,9 @@ from __future__ import print_function import os +import platform import sys +from datetime import datetime from xml.dom import minidom import py @@ -145,6 +147,30 @@ def test_xpass(): node = dom.find_first_by_tag("testsuite") node.assert_attr(name="pytest", errors=1, failures=2, skipped=1, tests=5) + def test_hostname_in_xml(self, testdir): + testdir.makepyfile( + """ + def test_pass(): + pass + """ + ) + result, dom = runandparse(testdir) + node = dom.find_first_by_tag("testsuite") + node.assert_attr(hostname=platform.node()) + + def test_timestamp_in_xml(self, testdir): + testdir.makepyfile( + """ + def test_pass(): + pass + """ + ) + start_time = datetime.now() + result, dom = runandparse(testdir) + 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() + def test_timing_function(self, testdir): testdir.makepyfile( """