Skip to content

Add an ISO-8601 timestamp to every testsuite and testcase in the XML-report #157

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 30, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions tests/builder_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ def test_add_time_attribute_on_end_context(self):

element.attributes['time'].value

def test_add_timestamp_attribute_on_end_context(self):
self.root.begin('testsuite', 'name')
element = self.root.end()

element.attributes['timestamp'].value


class TestXMLBuilderTest(unittest.TestCase):
"""TestXMLBuilder test cases.
Expand Down
7 changes: 7 additions & 0 deletions xmlrunner/builder.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import re
import sys
import datetime
import time
import six

Expand Down Expand Up @@ -80,6 +81,7 @@ def end(self):
"""
self._stop_time = time.time()
self.element.setAttribute('time', self.elapsed_time())
self.element.setAttribute('timestamp', self.timestamp())
self._set_result_counters()
return self.element

Expand Down Expand Up @@ -121,6 +123,11 @@ def elapsed_time(self):
"""
return format(self._stop_time - self._start_time, '.3f')

def timestamp(self):
"""Returns the time the context ended as ISO-8601-formatted timestamp.
"""
return datetime.datetime.fromtimestamp(self._stop_time).replace(microsecond=0).isoformat()


class TestXMLBuilder(object):
"""This class encapsulates most rules needed to create a XML test report
Expand Down
37 changes: 23 additions & 14 deletions xmlrunner/result.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

import os
import sys
import datetime
import time
import traceback
import six
Expand All @@ -16,28 +17,28 @@
# http://stackoverflow.com/questions/1707890/fast-way-to-filter-illegal-xml-unicode-chars-in-python

_illegal_unichrs = [
(0x00, 0x08), (0x0B, 0x0C), (0x0E, 0x1F),
(0x7F, 0x84), (0x86, 0x9F),
(0x00, 0x08), (0x0B, 0x0C), (0x0E, 0x1F),
(0x7F, 0x84), (0x86, 0x9F),
(0xFDD0, 0xFDDF), (0xFFFE, 0xFFFF),
]
if sys.maxunicode >= 0x10000: # not narrow build
]
if sys.maxunicode >= 0x10000: # not narrow build
_illegal_unichrs.extend([
(0x1FFFE, 0x1FFFF), (0x2FFFE, 0x2FFFF),
(0x3FFFE, 0x3FFFF), (0x4FFFE, 0x4FFFF),
(0x5FFFE, 0x5FFFF), (0x6FFFE, 0x6FFFF),
(0x7FFFE, 0x7FFFF), (0x8FFFE, 0x8FFFF),
(0x9FFFE, 0x9FFFF), (0xAFFFE, 0xAFFFF),
(0xBFFFE, 0xBFFFF), (0xCFFFE, 0xCFFFF),
(0xDFFFE, 0xDFFFF), (0xEFFFE, 0xEFFFF),
(0x1FFFE, 0x1FFFF), (0x2FFFE, 0x2FFFF),
(0x3FFFE, 0x3FFFF), (0x4FFFE, 0x4FFFF),
(0x5FFFE, 0x5FFFF), (0x6FFFE, 0x6FFFF),
(0x7FFFE, 0x7FFFF), (0x8FFFE, 0x8FFFF),
(0x9FFFE, 0x9FFFF), (0xAFFFE, 0xAFFFF),
(0xBFFFE, 0xBFFFF), (0xCFFFE, 0xCFFFF),
(0xDFFFE, 0xDFFFF), (0xEFFFE, 0xEFFFF),
(0xFFFFE, 0xFFFFF), (0x10FFFE, 0x10FFFF),
])
])

_illegal_ranges = [
"%s-%s" % (six.unichr(low), six.unichr(high))
for (low, high) in _illegal_unichrs
]

INVALID_XML_1_0_UNICODE_RE = re.compile(u'[%s]' % u''.join(_illegal_ranges))
INVALID_XML_1_0_UNICODE_RE = re.compile(u'[%s]' % u''.join(_illegal_ranges))


STDOUT_LINE = '\nStdout:\n%s'
Expand Down Expand Up @@ -94,6 +95,7 @@ def __init__(self, test_result, test_method, outcome=SUCCESS, err=None, subTest=
self.test_result = test_result
self.outcome = outcome
self.elapsed_time = 0
self.timestamp = datetime.datetime.min.replace(microsecond=0).isoformat()
if err:
if self.outcome != _TestInfo.SKIP:
self.test_exception_name = safe_unicode(err[0].__name__)
Expand Down Expand Up @@ -124,6 +126,8 @@ def test_finished(self):
"""
self.elapsed_time = \
self.test_result.stop_time - self.test_result.start_time
timestamp = datetime.datetime.fromtimestamp(self.test_result.stop_time)
self.timestamp = timestamp.replace(microsecond=0).isoformat()

def get_description(self):
"""
Expand Down Expand Up @@ -343,6 +347,10 @@ def _report_testsuite(suite_name, tests, xml_document, parentElement,
testsuite.setAttribute(
'time', '%.3f' % sum(map(lambda e: e.elapsed_time, tests))
)
if tests:
testsuite.setAttribute(
'timestamp', max(map(lambda e: e.timestamp, tests))
)
failures = filter(lambda e: e.outcome == e.FAILURE, tests)
testsuite.setAttribute('failures', str(len(list(failures))))

Expand All @@ -351,7 +359,7 @@ def _report_testsuite(suite_name, tests, xml_document, parentElement,

skips = filter(lambda e: e.outcome == _TestInfo.SKIP, tests)
testsuite.setAttribute('skipped', str(len(list(skips))))

_XMLTestResult._report_testsuite_properties(
testsuite, xml_document, properties)

Expand Down Expand Up @@ -420,6 +428,7 @@ def _report_testcase(test_result, xml_testsuite, xml_document):
'name', _XMLTestResult._test_method_name(test_result.test_id)
)
testcase.setAttribute('time', '%.3f' % test_result.elapsed_time)
testcase.setAttribute('timestamp', test_result.timestamp)

if (test_result.outcome != test_result.SUCCESS):
elem_name = ('failure', 'error', 'skipped')[test_result.outcome-1]
Expand Down