2015-09-30 02:46:40 +08:00
|
|
|
"""
|
|
|
|
report test results in JUnit-XML format,
|
|
|
|
for use with Jenkins and build integration servers.
|
2010-11-06 18:38:53 +08:00
|
|
|
|
2015-07-01 07:30:20 +08:00
|
|
|
|
2010-11-06 18:38:53 +08:00
|
|
|
Based on initial code from Ross Lawley.
|
2017-03-17 09:21:30 +08:00
|
|
|
|
|
|
|
Output conforms to https://github.com/jenkinsci/xunit-plugin/blob/master/
|
|
|
|
src/main/resources/org/jenkinsci/plugins/xunit/types/model/xsd/junit-10.xsd
|
2009-12-31 18:25:07 +08:00
|
|
|
"""
|
2017-03-17 09:21:30 +08:00
|
|
|
from __future__ import absolute_import, division, print_function
|
2015-09-30 02:46:40 +08:00
|
|
|
|
2016-11-28 07:55:49 +08:00
|
|
|
import functools
|
2009-12-31 18:25:07 +08:00
|
|
|
import py
|
2010-11-06 16:58:04 +08:00
|
|
|
import os
|
2011-04-16 07:09:25 +08:00
|
|
|
import re
|
|
|
|
import sys
|
2009-12-31 18:25:07 +08:00
|
|
|
import time
|
2015-08-22 04:31:20 +08:00
|
|
|
import pytest
|
2016-11-28 07:55:49 +08:00
|
|
|
from _pytest.config import filename_arg
|
2009-12-31 18:25:07 +08:00
|
|
|
|
2011-04-16 07:09:25 +08:00
|
|
|
# Python 2.X and 3.X compatibility
|
2014-08-01 16:12:53 +08:00
|
|
|
if sys.version_info[0] < 3:
|
|
|
|
from codecs import open
|
|
|
|
else:
|
2011-04-16 07:09:25 +08:00
|
|
|
unichr = chr
|
|
|
|
unicode = str
|
|
|
|
long = int
|
|
|
|
|
2015-09-30 02:46:40 +08:00
|
|
|
|
2011-12-02 03:08:51 +08:00
|
|
|
class Junit(py.xml.Namespace):
|
|
|
|
pass
|
|
|
|
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2011-04-16 07:09:25 +08:00
|
|
|
# We need to get the subset of the invalid unicode ranges according to
|
|
|
|
# XML 1.0 which are valid in this python build. Hence we calculate
|
|
|
|
# this dynamically instead of hardcoding it. The spec range of valid
|
|
|
|
# chars is: Char ::= #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD]
|
|
|
|
# | [#x10000-#x10FFFF]
|
2012-03-09 20:12:18 +08:00
|
|
|
_legal_chars = (0x09, 0x0A, 0x0d)
|
|
|
|
_legal_ranges = (
|
2015-09-30 02:46:40 +08:00
|
|
|
(0x20, 0x7E), (0x80, 0xD7FF), (0xE000, 0xFFFD), (0x10000, 0x10FFFF),
|
2012-03-09 20:12:18 +08:00
|
|
|
)
|
2015-09-30 02:46:40 +08:00
|
|
|
_legal_xml_re = [
|
|
|
|
unicode("%s-%s") % (unichr(low), unichr(high))
|
|
|
|
for (low, high) in _legal_ranges if low < sys.maxunicode
|
|
|
|
]
|
2012-03-09 20:12:18 +08:00
|
|
|
_legal_xml_re = [unichr(x) for x in _legal_chars] + _legal_xml_re
|
2015-09-30 02:46:40 +08:00
|
|
|
illegal_xml_re = re.compile(unicode('[^%s]') % unicode('').join(_legal_xml_re))
|
2012-03-09 20:12:18 +08:00
|
|
|
del _legal_chars
|
|
|
|
del _legal_ranges
|
|
|
|
del _legal_xml_re
|
2011-04-16 07:09:25 +08:00
|
|
|
|
2016-03-01 23:49:55 +08:00
|
|
|
_py_ext_re = re.compile(r"\.py$")
|
|
|
|
|
2015-09-30 02:46:40 +08:00
|
|
|
|
2011-12-02 03:08:51 +08:00
|
|
|
def bin_xml_escape(arg):
|
|
|
|
def repl(matchobj):
|
|
|
|
i = ord(matchobj.group())
|
|
|
|
if i <= 0xFF:
|
|
|
|
return unicode('#x%02X') % i
|
|
|
|
else:
|
|
|
|
return unicode('#x%04X') % i
|
2015-09-30 02:46:40 +08:00
|
|
|
|
2012-08-17 22:08:08 +08:00
|
|
|
return py.xml.raw(illegal_xml_re.sub(repl, py.xml.escape(arg)))
|
2011-04-16 07:09:25 +08:00
|
|
|
|
2015-09-30 02:46:40 +08:00
|
|
|
|
2015-10-10 17:45:56 +08:00
|
|
|
class _NodeReporter(object):
|
|
|
|
def __init__(self, nodeid, xml):
|
|
|
|
|
|
|
|
self.id = nodeid
|
2015-10-11 01:24:21 +08:00
|
|
|
self.xml = xml
|
|
|
|
self.add_stats = self.xml.add_stats
|
2015-10-10 17:45:56 +08:00
|
|
|
self.duration = 0
|
2016-02-29 22:39:37 +08:00
|
|
|
self.properties = []
|
2015-10-11 01:24:21 +08:00
|
|
|
self.nodes = []
|
2015-10-10 17:45:56 +08:00
|
|
|
self.testcase = None
|
2015-10-11 01:24:21 +08:00
|
|
|
self.attrs = {}
|
|
|
|
|
|
|
|
def append(self, node):
|
|
|
|
self.xml.add_stats(type(node).__name__)
|
|
|
|
self.nodes.append(node)
|
2015-10-10 17:45:56 +08:00
|
|
|
|
|
|
|
def add_property(self, name, value):
|
2016-02-29 22:39:37 +08:00
|
|
|
self.properties.append((str(name), bin_xml_escape(value)))
|
2015-10-10 17:45:56 +08:00
|
|
|
|
|
|
|
def make_properties_node(self):
|
2015-10-21 15:40:14 +08:00
|
|
|
"""Return a Junit node containing custom properties, if any.
|
2015-10-10 17:45:56 +08:00
|
|
|
"""
|
|
|
|
if self.properties:
|
|
|
|
return Junit.properties([
|
2016-02-29 22:39:37 +08:00
|
|
|
Junit.property(name=name, value=value)
|
|
|
|
for name, value in self.properties
|
2015-10-10 17:45:56 +08:00
|
|
|
])
|
2015-10-11 01:24:21 +08:00
|
|
|
return ''
|
|
|
|
|
|
|
|
def record_testreport(self, testreport):
|
|
|
|
assert not self.testcase
|
2016-03-01 23:49:55 +08:00
|
|
|
names = mangle_test_address(testreport.nodeid)
|
2015-10-11 01:24:21 +08:00
|
|
|
classnames = names[:-1]
|
|
|
|
if self.xml.prefix:
|
|
|
|
classnames.insert(0, self.xml.prefix)
|
|
|
|
attrs = {
|
|
|
|
"classname": ".".join(classnames),
|
|
|
|
"name": bin_xml_escape(names[-1]),
|
|
|
|
"file": testreport.location[0],
|
|
|
|
}
|
|
|
|
if testreport.location[1] is not None:
|
|
|
|
attrs["line"] = testreport.location[1]
|
2016-08-26 04:08:51 +08:00
|
|
|
if hasattr(testreport, "url"):
|
|
|
|
attrs["url"] = testreport.url
|
2015-10-11 01:24:21 +08:00
|
|
|
self.attrs = attrs
|
|
|
|
|
|
|
|
def to_xml(self):
|
|
|
|
testcase = Junit.testcase(time=self.duration, **self.attrs)
|
|
|
|
testcase.append(self.make_properties_node())
|
|
|
|
for node in self.nodes:
|
|
|
|
testcase.append(node)
|
|
|
|
return testcase
|
|
|
|
|
|
|
|
def _add_simple(self, kind, message, data=None):
|
|
|
|
data = bin_xml_escape(data)
|
|
|
|
node = kind(data, message=message)
|
|
|
|
self.append(node)
|
|
|
|
|
2017-02-22 21:17:45 +08:00
|
|
|
def write_captured_output(self, report):
|
2015-10-11 01:24:21 +08:00
|
|
|
for capname in ('out', 'err'):
|
2016-08-04 08:49:43 +08:00
|
|
|
content = getattr(report, 'capstd' + capname)
|
|
|
|
if content:
|
2015-10-11 01:24:21 +08:00
|
|
|
tag = getattr(Junit, 'system-' + capname)
|
2016-08-04 08:49:43 +08:00
|
|
|
self.append(tag(bin_xml_escape(content)))
|
2015-10-11 01:24:21 +08:00
|
|
|
|
|
|
|
def append_pass(self, report):
|
|
|
|
self.add_stats('passed')
|
|
|
|
|
|
|
|
def append_failure(self, report):
|
|
|
|
# msg = str(report.longrepr.reprtraceback.extraline)
|
|
|
|
if hasattr(report, "wasxfail"):
|
|
|
|
self._add_simple(
|
|
|
|
Junit.skipped,
|
|
|
|
"xfail-marked test passes unexpectedly")
|
|
|
|
else:
|
|
|
|
if hasattr(report.longrepr, "reprcrash"):
|
|
|
|
message = report.longrepr.reprcrash.message
|
|
|
|
elif isinstance(report.longrepr, (unicode, str)):
|
|
|
|
message = report.longrepr
|
|
|
|
else:
|
|
|
|
message = str(report.longrepr)
|
|
|
|
message = bin_xml_escape(message)
|
|
|
|
fail = Junit.failure(message=message)
|
|
|
|
fail.append(bin_xml_escape(report.longrepr))
|
|
|
|
self.append(fail)
|
|
|
|
|
|
|
|
def append_collect_error(self, report):
|
|
|
|
# msg = str(report.longrepr.reprtraceback.extraline)
|
|
|
|
self.append(Junit.error(bin_xml_escape(report.longrepr),
|
|
|
|
message="collection failure"))
|
|
|
|
|
|
|
|
def append_collect_skipped(self, report):
|
|
|
|
self._add_simple(
|
|
|
|
Junit.skipped, "collection skipped", report.longrepr)
|
|
|
|
|
|
|
|
def append_error(self, report):
|
2016-11-02 20:45:40 +08:00
|
|
|
if getattr(report, 'when', None) == 'teardown':
|
|
|
|
msg = "test teardown failure"
|
|
|
|
else:
|
|
|
|
msg = "test setup failure"
|
2015-10-11 01:24:21 +08:00
|
|
|
self._add_simple(
|
2016-11-02 20:45:40 +08:00
|
|
|
Junit.error, msg, report.longrepr)
|
2015-10-11 01:24:21 +08:00
|
|
|
|
|
|
|
def append_skipped(self, report):
|
|
|
|
if hasattr(report, "wasxfail"):
|
|
|
|
self._add_simple(
|
|
|
|
Junit.skipped, "expected test failure", report.wasxfail
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
filename, lineno, skipreason = report.longrepr
|
|
|
|
if skipreason.startswith("Skipped: "):
|
|
|
|
skipreason = bin_xml_escape(skipreason[9:])
|
|
|
|
self.append(
|
|
|
|
Junit.skipped("%s:%s: %s" % (filename, lineno, skipreason),
|
|
|
|
type="pytest.skip",
|
|
|
|
message=skipreason))
|
2017-02-22 21:17:45 +08:00
|
|
|
self.write_captured_output(report)
|
2015-10-10 17:45:56 +08:00
|
|
|
|
2015-10-11 01:44:14 +08:00
|
|
|
def finalize(self):
|
|
|
|
data = self.to_xml().unicode(indent=0)
|
|
|
|
self.__dict__.clear()
|
|
|
|
self.to_xml = lambda: py.xml.raw(data)
|
|
|
|
|
|
|
|
|
2015-08-22 04:31:20 +08:00
|
|
|
@pytest.fixture
|
|
|
|
def record_xml_property(request):
|
2016-07-22 18:39:06 +08:00
|
|
|
"""Add extra xml properties to the tag for the calling test.
|
|
|
|
The fixture is callable with ``(name, value)``, with value being automatically
|
2015-08-23 22:45:39 +08:00
|
|
|
xml-encoded.
|
|
|
|
"""
|
2015-10-10 20:20:51 +08:00
|
|
|
request.node.warn(
|
2015-10-10 17:45:56 +08:00
|
|
|
code='C3',
|
|
|
|
message='record_xml_property is an experimental feature',
|
2015-10-10 20:20:51 +08:00
|
|
|
)
|
2015-10-10 17:45:56 +08:00
|
|
|
xml = getattr(request.config, "_xml", None)
|
|
|
|
if xml is not None:
|
2015-10-14 18:30:56 +08:00
|
|
|
node_reporter = xml.node_reporter(request.node.nodeid)
|
|
|
|
return node_reporter.add_property
|
2015-10-10 17:45:56 +08:00
|
|
|
else:
|
|
|
|
def add_property_noop(name, value):
|
|
|
|
pass
|
2015-09-30 02:46:40 +08:00
|
|
|
|
2015-10-10 17:45:56 +08:00
|
|
|
return add_property_noop
|
2015-08-20 05:36:42 +08:00
|
|
|
|
2015-09-30 02:46:40 +08:00
|
|
|
|
2009-12-31 18:25:07 +08:00
|
|
|
def pytest_addoption(parser):
|
2010-01-03 19:41:29 +08:00
|
|
|
group = parser.getgroup("terminal reporting")
|
2015-09-30 02:46:40 +08:00
|
|
|
group.addoption(
|
|
|
|
'--junitxml', '--junit-xml',
|
|
|
|
action="store",
|
|
|
|
dest="xmlpath",
|
|
|
|
metavar="path",
|
2016-11-28 07:55:49 +08:00
|
|
|
type=functools.partial(filename_arg, optname="--junitxml"),
|
2015-09-30 02:46:40 +08:00
|
|
|
default=None,
|
|
|
|
help="create junit-xml style report file at given path.")
|
|
|
|
group.addoption(
|
|
|
|
'--junitprefix', '--junit-prefix',
|
|
|
|
action="store",
|
|
|
|
metavar="str",
|
|
|
|
default=None,
|
|
|
|
help="prepend prefix to classnames in junit-xml output")
|
2017-05-12 09:45:20 +08:00
|
|
|
parser.addini("junit_suite_name", "Test suite name for JUnit report", default="pytest")
|
2015-09-30 02:46:40 +08:00
|
|
|
|
2009-12-31 18:25:07 +08:00
|
|
|
|
|
|
|
def pytest_configure(config):
|
|
|
|
xmlpath = config.option.xmlpath
|
2013-03-25 03:43:25 +08:00
|
|
|
# prevent opening xmllog on slave nodes (xdist)
|
|
|
|
if xmlpath and not hasattr(config, 'slaveinput'):
|
2017-05-12 09:45:20 +08:00
|
|
|
config._xml = LogXML(xmlpath, config.option.junitprefix, config.getini("junit_suite_name"))
|
2009-12-31 18:25:07 +08:00
|
|
|
config.pluginmanager.register(config._xml)
|
|
|
|
|
2015-09-30 02:46:40 +08:00
|
|
|
|
2009-12-31 18:25:07 +08:00
|
|
|
def pytest_unconfigure(config):
|
|
|
|
xml = getattr(config, '_xml', None)
|
|
|
|
if xml:
|
2010-07-27 03:15:15 +08:00
|
|
|
del config._xml
|
2009-12-31 18:25:07 +08:00
|
|
|
config.pluginmanager.unregister(xml)
|
|
|
|
|
2015-09-30 02:46:40 +08:00
|
|
|
|
2016-03-01 23:49:55 +08:00
|
|
|
def mangle_test_address(address):
|
|
|
|
path, possible_open_bracket, params = address.partition('[')
|
|
|
|
names = path.split("::")
|
|
|
|
try:
|
|
|
|
names.remove('()')
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
# convert file path to dotted path
|
2012-05-22 23:18:04 +08:00
|
|
|
names[0] = names[0].replace("/", '.')
|
2016-03-01 23:49:55 +08:00
|
|
|
names[0] = _py_ext_re.sub("", names[0])
|
|
|
|
# put any params back
|
|
|
|
names[-1] += possible_open_bracket + params
|
2012-05-22 23:18:04 +08:00
|
|
|
return names
|
|
|
|
|
2015-09-30 02:46:40 +08:00
|
|
|
|
2009-12-31 18:25:07 +08:00
|
|
|
class LogXML(object):
|
2017-02-23 04:31:30 +08:00
|
|
|
def __init__(self, logfile, prefix, suite_name="pytest"):
|
2011-05-28 22:21:57 +08:00
|
|
|
logfile = os.path.expanduser(os.path.expandvars(logfile))
|
2012-08-04 16:33:43 +08:00
|
|
|
self.logfile = os.path.normpath(os.path.abspath(logfile))
|
2010-06-09 22:18:47 +08:00
|
|
|
self.prefix = prefix
|
2017-02-23 04:31:30 +08:00
|
|
|
self.suite_name = suite_name
|
2015-10-10 15:31:52 +08:00
|
|
|
self.stats = dict.fromkeys([
|
|
|
|
'error',
|
|
|
|
'passed',
|
|
|
|
'failure',
|
|
|
|
'skipped',
|
|
|
|
], 0)
|
2015-11-25 04:17:42 +08:00
|
|
|
self.node_reporters = {} # nodeid -> _NodeReporter
|
|
|
|
self.node_reporters_ordered = []
|
2016-02-29 22:14:23 +08:00
|
|
|
self.global_properties = []
|
2017-02-03 17:30:28 +08:00
|
|
|
# List of reports that failed on call but teardown is pending.
|
|
|
|
self.open_reports = []
|
|
|
|
self.cnt_double_fail_tests = 0
|
2015-10-10 17:45:56 +08:00
|
|
|
|
2015-12-17 02:07:36 +08:00
|
|
|
def finalize(self, report):
|
|
|
|
nodeid = getattr(report, 'nodeid', report)
|
|
|
|
# local hack to handle xdist report order
|
|
|
|
slavenode = getattr(report, 'node', None)
|
|
|
|
reporter = self.node_reporters.pop((nodeid, slavenode))
|
|
|
|
if reporter is not None:
|
|
|
|
reporter.finalize()
|
|
|
|
|
2015-12-06 20:21:52 +08:00
|
|
|
def node_reporter(self, report):
|
|
|
|
nodeid = getattr(report, 'nodeid', report)
|
|
|
|
# local hack to handle xdist report order
|
|
|
|
slavenode = getattr(report, 'node', None)
|
|
|
|
|
|
|
|
key = nodeid, slavenode
|
|
|
|
|
|
|
|
if key in self.node_reporters:
|
2015-12-17 02:07:36 +08:00
|
|
|
# TODO: breasks for --dist=each
|
2015-12-06 20:21:52 +08:00
|
|
|
return self.node_reporters[key]
|
2016-02-29 22:14:23 +08:00
|
|
|
|
2015-10-10 17:45:56 +08:00
|
|
|
reporter = _NodeReporter(nodeid, self)
|
2016-02-29 22:14:23 +08:00
|
|
|
|
2015-12-06 20:21:52 +08:00
|
|
|
self.node_reporters[key] = reporter
|
2015-11-25 04:17:42 +08:00
|
|
|
self.node_reporters_ordered.append(reporter)
|
2016-02-29 22:14:23 +08:00
|
|
|
|
2015-10-10 17:45:56 +08:00
|
|
|
return reporter
|
2015-08-20 05:36:42 +08:00
|
|
|
|
2015-10-11 01:24:21 +08:00
|
|
|
def add_stats(self, key):
|
2015-10-10 15:31:52 +08:00
|
|
|
if key in self.stats:
|
|
|
|
self.stats[key] += 1
|
|
|
|
|
2009-12-31 18:25:07 +08:00
|
|
|
def _opentestcase(self, report):
|
2015-12-06 20:21:52 +08:00
|
|
|
reporter = self.node_reporter(report)
|
2015-10-11 01:24:21 +08:00
|
|
|
reporter.record_testreport(report)
|
|
|
|
return reporter
|
2009-12-31 18:25:07 +08:00
|
|
|
|
|
|
|
def pytest_runtest_logreport(self, report):
|
2015-09-26 14:11:23 +08:00
|
|
|
"""handle a setup/call/teardown report, generating the appropriate
|
|
|
|
xml tags as necessary.
|
|
|
|
|
|
|
|
note: due to plugins like xdist, this hook may be called in interlaced
|
|
|
|
order with reports from other nodes. for example:
|
|
|
|
|
|
|
|
usual call order:
|
|
|
|
-> setup node1
|
|
|
|
-> call node1
|
|
|
|
-> teardown node1
|
|
|
|
-> setup node2
|
|
|
|
-> call node2
|
|
|
|
-> teardown node2
|
|
|
|
|
|
|
|
possible call order in xdist:
|
|
|
|
-> setup node1
|
|
|
|
-> call node1
|
|
|
|
-> setup node2
|
|
|
|
-> call node2
|
|
|
|
-> teardown node2
|
|
|
|
-> teardown node1
|
|
|
|
"""
|
2017-02-03 17:30:28 +08:00
|
|
|
close_report = None
|
2009-12-31 18:25:07 +08:00
|
|
|
if report.passed:
|
2015-09-26 14:11:23 +08:00
|
|
|
if report.when == "call": # ignore setup/teardown
|
2015-10-11 01:24:21 +08:00
|
|
|
reporter = self._opentestcase(report)
|
|
|
|
reporter.append_pass(report)
|
2009-12-31 18:25:07 +08:00
|
|
|
elif report.failed:
|
2017-02-03 17:30:28 +08:00
|
|
|
if report.when == "teardown":
|
|
|
|
# The following vars are needed when xdist plugin is used
|
|
|
|
report_wid = getattr(report, "worker_id", None)
|
|
|
|
report_ii = getattr(report, "item_index", None)
|
|
|
|
close_report = next(
|
|
|
|
(rep for rep in self.open_reports
|
|
|
|
if (rep.nodeid == report.nodeid and
|
|
|
|
getattr(rep, "item_index", None) == report_ii and
|
|
|
|
getattr(rep, "worker_id", None) == report_wid
|
|
|
|
)
|
|
|
|
), None)
|
|
|
|
if close_report:
|
|
|
|
# We need to open new testcase in case we have failure in
|
|
|
|
# call and error in teardown in order to follow junit
|
|
|
|
# schema
|
|
|
|
self.finalize(close_report)
|
|
|
|
self.cnt_double_fail_tests += 1
|
2015-10-11 01:24:21 +08:00
|
|
|
reporter = self._opentestcase(report)
|
2015-09-30 04:30:22 +08:00
|
|
|
if report.when == "call":
|
2015-10-11 01:24:21 +08:00
|
|
|
reporter.append_failure(report)
|
2017-02-03 17:30:28 +08:00
|
|
|
self.open_reports.append(report)
|
2015-09-30 04:30:22 +08:00
|
|
|
else:
|
2015-10-11 01:24:21 +08:00
|
|
|
reporter.append_error(report)
|
2009-12-31 18:25:07 +08:00
|
|
|
elif report.skipped:
|
2015-10-11 01:24:21 +08:00
|
|
|
reporter = self._opentestcase(report)
|
|
|
|
reporter.append_skipped(report)
|
2015-09-26 14:11:23 +08:00
|
|
|
self.update_testcase_duration(report)
|
2015-10-11 01:44:14 +08:00
|
|
|
if report.when == "teardown":
|
2017-02-22 21:17:45 +08:00
|
|
|
reporter = self._opentestcase(report)
|
|
|
|
reporter.write_captured_output(report)
|
2015-12-17 02:07:36 +08:00
|
|
|
self.finalize(report)
|
2017-02-03 17:30:28 +08:00
|
|
|
report_wid = getattr(report, "worker_id", None)
|
|
|
|
report_ii = getattr(report, "item_index", None)
|
|
|
|
close_report = next(
|
|
|
|
(rep for rep in self.open_reports
|
|
|
|
if (rep.nodeid == report.nodeid and
|
|
|
|
getattr(rep, "item_index", None) == report_ii and
|
|
|
|
getattr(rep, "worker_id", None) == report_wid
|
|
|
|
)
|
|
|
|
), None)
|
|
|
|
if close_report:
|
|
|
|
self.open_reports.remove(close_report)
|
2015-09-26 14:11:23 +08:00
|
|
|
|
|
|
|
def update_testcase_duration(self, report):
|
|
|
|
"""accumulates total duration for nodeid from given report and updates
|
|
|
|
the Junit.testcase with the new total if already created.
|
|
|
|
"""
|
2015-12-06 20:21:52 +08:00
|
|
|
reporter = self.node_reporter(report)
|
2015-10-10 17:45:56 +08:00
|
|
|
reporter.duration += getattr(report, 'duration', 0.0)
|
2015-09-26 14:11:23 +08:00
|
|
|
|
2009-12-31 18:25:07 +08:00
|
|
|
def pytest_collectreport(self, report):
|
|
|
|
if not report.passed:
|
2015-10-11 01:24:21 +08:00
|
|
|
reporter = self._opentestcase(report)
|
2009-12-31 18:25:07 +08:00
|
|
|
if report.failed:
|
2015-10-11 01:24:21 +08:00
|
|
|
reporter.append_collect_error(report)
|
2009-12-31 18:25:07 +08:00
|
|
|
else:
|
2015-10-11 01:24:21 +08:00
|
|
|
reporter.append_collect_skipped(report)
|
2009-12-31 18:25:07 +08:00
|
|
|
|
|
|
|
def pytest_internalerror(self, excrepr):
|
2015-10-14 18:30:56 +08:00
|
|
|
reporter = self.node_reporter('internal')
|
2015-10-11 01:24:21 +08:00
|
|
|
reporter.attrs.update(classname="pytest", name='internal')
|
|
|
|
reporter._add_simple(Junit.error, 'internal error', excrepr)
|
2009-12-31 18:25:07 +08:00
|
|
|
|
2013-10-21 22:54:25 +08:00
|
|
|
def pytest_sessionstart(self):
|
2009-12-31 18:25:07 +08:00
|
|
|
self.suite_start_time = time.time()
|
|
|
|
|
2013-10-21 22:54:25 +08:00
|
|
|
def pytest_sessionfinish(self):
|
2015-06-17 05:36:18 +08:00
|
|
|
dirname = os.path.dirname(os.path.abspath(self.logfile))
|
2015-06-17 19:08:03 +08:00
|
|
|
if not os.path.isdir(dirname):
|
2015-06-17 05:36:18 +08:00
|
|
|
os.makedirs(dirname)
|
2014-08-01 16:12:53 +08:00
|
|
|
logfile = open(self.logfile, 'w', encoding='utf-8')
|
2009-12-31 18:25:07 +08:00
|
|
|
suite_stop_time = time.time()
|
|
|
|
suite_time_delta = suite_stop_time - self.suite_start_time
|
2015-10-10 15:31:52 +08:00
|
|
|
|
2017-02-03 17:30:28 +08:00
|
|
|
numtests = (self.stats['passed'] + self.stats['failure'] +
|
|
|
|
self.stats['skipped'] + self.stats['error'] -
|
|
|
|
self.cnt_double_fail_tests)
|
2010-04-28 21:24:38 +08:00
|
|
|
logfile.write('<?xml version="1.0" encoding="utf-8"?>')
|
2016-02-29 22:14:23 +08:00
|
|
|
|
2011-12-02 03:08:51 +08:00
|
|
|
logfile.write(Junit.testsuite(
|
2016-02-29 22:14:23 +08:00
|
|
|
self._get_global_properties_node(),
|
2015-11-25 04:17:42 +08:00
|
|
|
[x.to_xml() for x in self.node_reporters_ordered],
|
2017-02-23 04:31:30 +08:00
|
|
|
name=self.suite_name,
|
2015-10-10 15:31:52 +08:00
|
|
|
errors=self.stats['error'],
|
|
|
|
failures=self.stats['failure'],
|
|
|
|
skips=self.stats['skipped'],
|
2011-12-02 03:08:51 +08:00
|
|
|
tests=numtests,
|
2015-09-30 02:46:40 +08:00
|
|
|
time="%.3f" % suite_time_delta, ).unicode(indent=0))
|
2009-12-31 18:25:07 +08:00
|
|
|
logfile.close()
|
2010-06-04 06:39:18 +08:00
|
|
|
|
|
|
|
def pytest_terminal_summary(self, terminalreporter):
|
2015-09-30 02:46:40 +08:00
|
|
|
terminalreporter.write_sep("-",
|
|
|
|
"generated xml file: %s" % (self.logfile))
|
2016-02-29 22:14:23 +08:00
|
|
|
|
|
|
|
def add_global_property(self, name, value):
|
|
|
|
self.global_properties.append((str(name), bin_xml_escape(value)))
|
|
|
|
|
|
|
|
def _get_global_properties_node(self):
|
|
|
|
"""Return a Junit node containing custom properties, if any.
|
|
|
|
"""
|
|
|
|
if self.global_properties:
|
|
|
|
return Junit.properties(
|
|
|
|
[
|
|
|
|
Junit.property(name=name, value=value)
|
|
|
|
for name, value in self.global_properties
|
|
|
|
]
|
|
|
|
)
|
|
|
|
return ''
|