2010-11-06 18:38:53 +08:00
|
|
|
""" report test results in JUnit-XML format, for use with Hudson and build integration servers.
|
|
|
|
|
|
|
|
Based on initial code from Ross Lawley.
|
2009-12-31 18:25:07 +08:00
|
|
|
"""
|
2014-08-01 06:13:40 +08:00
|
|
|
import codecs
|
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
|
|
|
|
|
2011-04-16 07:09:25 +08:00
|
|
|
# Python 2.X and 3.X compatibility
|
|
|
|
try:
|
|
|
|
unichr(65)
|
|
|
|
except NameError:
|
|
|
|
unichr = chr
|
|
|
|
try:
|
|
|
|
unicode('A')
|
|
|
|
except NameError:
|
|
|
|
unicode = str
|
|
|
|
try:
|
|
|
|
long(1)
|
|
|
|
except NameError:
|
|
|
|
long = int
|
|
|
|
|
|
|
|
|
2011-12-02 03:08:51 +08:00
|
|
|
class Junit(py.xml.Namespace):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
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 = (
|
2013-05-08 21:15:43 +08:00
|
|
|
(0x20, 0x7E),
|
|
|
|
(0x80, 0xD7FF),
|
2012-03-09 20:12:18 +08:00
|
|
|
(0xE000, 0xFFFD),
|
|
|
|
(0x10000, 0x10FFFF),
|
|
|
|
)
|
|
|
|
_legal_xml_re = [unicode("%s-%s") % (unichr(low), unichr(high))
|
|
|
|
for (low, high) in _legal_ranges
|
2011-04-16 07:09:25 +08:00
|
|
|
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
|
|
|
|
illegal_xml_re = re.compile(unicode('[^%s]') %
|
|
|
|
unicode('').join(_legal_xml_re))
|
|
|
|
del _legal_chars
|
|
|
|
del _legal_ranges
|
|
|
|
del _legal_xml_re
|
2011-04-16 07:09:25 +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
|
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
|
|
|
|
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")
|
2013-09-26 14:45:50 +08:00
|
|
|
group.addoption('--junitxml', '--junit-xml', action="store",
|
|
|
|
dest="xmlpath", metavar="path", default=None,
|
2010-01-17 06:33:26 +08:00
|
|
|
help="create junit-xml style report file at given path.")
|
2013-08-01 22:49:26 +08:00
|
|
|
group.addoption('--junitprefix', '--junit-prefix', action="store",
|
2010-07-07 20:43:31 +08:00
|
|
|
metavar="str", default=None,
|
|
|
|
help="prepend prefix to classnames in junit-xml output")
|
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'):
|
2010-07-07 20:43:31 +08:00
|
|
|
config._xml = LogXML(xmlpath, config.option.junitprefix)
|
2009-12-31 18:25:07 +08:00
|
|
|
config.pluginmanager.register(config._xml)
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
2011-04-16 07:09:25 +08:00
|
|
|
|
2012-05-22 23:18:04 +08:00
|
|
|
def mangle_testnames(names):
|
|
|
|
names = [x.replace(".py", "") for x in names if x != '()']
|
|
|
|
names[0] = names[0].replace("/", '.')
|
|
|
|
return names
|
|
|
|
|
2009-12-31 18:25:07 +08:00
|
|
|
class LogXML(object):
|
2010-06-09 22:18:47 +08:00
|
|
|
def __init__(self, logfile, prefix):
|
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
|
2011-12-02 03:08:51 +08:00
|
|
|
self.tests = []
|
2009-12-31 18:25:07 +08:00
|
|
|
self.passed = self.skipped = 0
|
|
|
|
self.failed = self.errors = 0
|
2010-07-27 03:15:15 +08:00
|
|
|
|
2009-12-31 18:25:07 +08:00
|
|
|
def _opentestcase(self, report):
|
2012-05-22 23:18:04 +08:00
|
|
|
names = mangle_testnames(report.nodeid.split("::"))
|
2010-06-09 22:18:47 +08:00
|
|
|
classnames = names[:-1]
|
|
|
|
if self.prefix:
|
|
|
|
classnames.insert(0, self.prefix)
|
2011-12-02 03:08:51 +08:00
|
|
|
self.tests.append(Junit.testcase(
|
|
|
|
classname=".".join(classnames),
|
2013-05-08 21:15:43 +08:00
|
|
|
name=bin_xml_escape(names[-1]),
|
2011-12-02 03:08:51 +08:00
|
|
|
time=getattr(report, 'duration', 0)
|
|
|
|
))
|
|
|
|
|
2013-04-16 12:45:14 +08:00
|
|
|
def _write_captured_output(self, report):
|
2014-03-14 19:49:34 +08:00
|
|
|
for capname in ('out', 'err'):
|
|
|
|
allcontent = ""
|
|
|
|
for name, content in report.get_sections("Captured std%s" %
|
|
|
|
capname):
|
|
|
|
allcontent += content
|
|
|
|
if allcontent:
|
|
|
|
tag = getattr(Junit, 'system-'+capname)
|
|
|
|
self.append(tag(bin_xml_escape(allcontent)))
|
2013-04-16 12:45:14 +08:00
|
|
|
|
2011-12-02 03:08:51 +08:00
|
|
|
def append(self, obj):
|
|
|
|
self.tests[-1].append(obj)
|
2010-07-27 03:15:15 +08:00
|
|
|
|
2009-12-31 18:25:07 +08:00
|
|
|
def append_pass(self, report):
|
|
|
|
self.passed += 1
|
2013-04-16 12:45:14 +08:00
|
|
|
self._write_captured_output(report)
|
2009-12-31 18:25:07 +08:00
|
|
|
|
|
|
|
def append_failure(self, report):
|
|
|
|
#msg = str(report.longrepr.reprtraceback.extraline)
|
2012-06-23 17:32:32 +08:00
|
|
|
if hasattr(report, "wasxfail"):
|
2011-12-02 03:08:51 +08:00
|
|
|
self.append(
|
|
|
|
Junit.skipped(message="xfail-marked test passes unexpectedly"))
|
2010-05-20 20:35:13 +08:00
|
|
|
self.skipped += 1
|
|
|
|
else:
|
2011-12-02 03:08:51 +08:00
|
|
|
fail = Junit.failure(message="test failure")
|
2013-12-13 17:28:23 +08:00
|
|
|
fail.append(bin_xml_escape(report.longrepr))
|
2011-12-02 03:08:51 +08:00
|
|
|
self.append(fail)
|
2010-05-20 20:35:13 +08:00
|
|
|
self.failed += 1
|
2013-04-16 12:45:14 +08:00
|
|
|
self._write_captured_output(report)
|
2009-12-31 18:25:07 +08:00
|
|
|
|
|
|
|
def append_collect_failure(self, report):
|
|
|
|
#msg = str(report.longrepr.reprtraceback.extraline)
|
2013-12-13 17:28:23 +08:00
|
|
|
self.append(Junit.failure(bin_xml_escape(report.longrepr),
|
2011-12-02 03:08:51 +08:00
|
|
|
message="collection failure"))
|
2009-12-31 18:25:07 +08:00
|
|
|
self.errors += 1
|
|
|
|
|
2009-12-31 18:50:01 +08:00
|
|
|
def append_collect_skipped(self, report):
|
|
|
|
#msg = str(report.longrepr.reprtraceback.extraline)
|
2013-12-13 17:28:23 +08:00
|
|
|
self.append(Junit.skipped(bin_xml_escape(report.longrepr),
|
2011-12-02 03:08:51 +08:00
|
|
|
message="collection skipped"))
|
2009-12-31 18:50:01 +08:00
|
|
|
self.skipped += 1
|
|
|
|
|
2009-12-31 18:25:07 +08:00
|
|
|
def append_error(self, report):
|
2013-12-13 17:28:23 +08:00
|
|
|
self.append(Junit.error(bin_xml_escape(report.longrepr),
|
2011-12-02 03:08:51 +08:00
|
|
|
message="test setup failure"))
|
2009-12-31 18:25:07 +08:00
|
|
|
self.errors += 1
|
|
|
|
|
|
|
|
def append_skipped(self, report):
|
2012-06-23 17:32:32 +08:00
|
|
|
if hasattr(report, "wasxfail"):
|
2013-12-13 17:28:23 +08:00
|
|
|
self.append(Junit.skipped(bin_xml_escape(report.wasxfail),
|
2011-12-02 03:08:51 +08:00
|
|
|
message="expected test failure"))
|
2010-05-20 20:35:13 +08:00
|
|
|
else:
|
2011-03-20 00:59:07 +08:00
|
|
|
filename, lineno, skipreason = report.longrepr
|
|
|
|
if skipreason.startswith("Skipped: "):
|
2013-12-13 17:28:23 +08:00
|
|
|
skipreason = bin_xml_escape(skipreason[9:])
|
2011-12-02 03:08:51 +08:00
|
|
|
self.append(
|
2014-04-23 15:38:40 +08:00
|
|
|
Junit.skipped("%s:%s: %s" % (filename, lineno, skipreason),
|
2011-12-02 03:08:51 +08:00
|
|
|
type="pytest.skip",
|
|
|
|
message=skipreason
|
|
|
|
))
|
2009-12-31 18:25:07 +08:00
|
|
|
self.skipped += 1
|
2013-04-16 12:45:14 +08:00
|
|
|
self._write_captured_output(report)
|
2009-12-31 18:25:07 +08:00
|
|
|
|
|
|
|
def pytest_runtest_logreport(self, report):
|
|
|
|
if report.passed:
|
2011-11-09 01:53:46 +08:00
|
|
|
if report.when == "call": # ignore setup/teardown
|
2011-12-02 03:08:51 +08:00
|
|
|
self._opentestcase(report)
|
2011-11-09 01:53:46 +08:00
|
|
|
self.append_pass(report)
|
2009-12-31 18:25:07 +08:00
|
|
|
elif report.failed:
|
2011-12-02 03:08:51 +08:00
|
|
|
self._opentestcase(report)
|
2009-12-31 18:25:07 +08:00
|
|
|
if report.when != "call":
|
|
|
|
self.append_error(report)
|
|
|
|
else:
|
|
|
|
self.append_failure(report)
|
|
|
|
elif report.skipped:
|
2011-12-02 03:08:51 +08:00
|
|
|
self._opentestcase(report)
|
2009-12-31 18:25:07 +08:00
|
|
|
self.append_skipped(report)
|
2010-07-27 03:15:15 +08:00
|
|
|
|
2009-12-31 18:25:07 +08:00
|
|
|
def pytest_collectreport(self, report):
|
|
|
|
if not report.passed:
|
2011-12-02 03:08:51 +08:00
|
|
|
self._opentestcase(report)
|
2009-12-31 18:25:07 +08:00
|
|
|
if report.failed:
|
|
|
|
self.append_collect_failure(report)
|
|
|
|
else:
|
|
|
|
self.append_collect_skipped(report)
|
|
|
|
|
|
|
|
def pytest_internalerror(self, excrepr):
|
|
|
|
self.errors += 1
|
2013-12-13 17:28:23 +08:00
|
|
|
data = bin_xml_escape(excrepr)
|
2011-12-02 03:08:51 +08:00
|
|
|
self.tests.append(
|
|
|
|
Junit.testcase(
|
|
|
|
Junit.error(data, message="internal error"),
|
|
|
|
classname="pytest",
|
|
|
|
name="internal"))
|
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):
|
2014-08-01 06:13:40 +08:00
|
|
|
if sys.version_info[0] < 3:
|
|
|
|
logfile = codecs.open(self.logfile, 'w', encoding='utf-8')
|
2010-04-27 21:15:43 +08:00
|
|
|
else:
|
|
|
|
logfile = open(self.logfile, 'w', encoding='utf-8')
|
2010-07-27 03:15:15 +08:00
|
|
|
|
2009-12-31 18:25:07 +08:00
|
|
|
suite_stop_time = time.time()
|
|
|
|
suite_time_delta = suite_stop_time - self.suite_start_time
|
2009-12-31 18:50:01 +08:00
|
|
|
numtests = self.passed + self.failed
|
2011-12-02 03:08:51 +08:00
|
|
|
|
2010-04-28 21:24:38 +08:00
|
|
|
logfile.write('<?xml version="1.0" encoding="utf-8"?>')
|
2011-12-02 03:08:51 +08:00
|
|
|
logfile.write(Junit.testsuite(
|
|
|
|
self.tests,
|
2013-09-26 14:45:50 +08:00
|
|
|
name="pytest",
|
2011-12-02 03:08:51 +08:00
|
|
|
errors=self.errors,
|
|
|
|
failures=self.failed,
|
|
|
|
skips=self.skipped,
|
|
|
|
tests=numtests,
|
|
|
|
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):
|
2010-11-24 10:05:40 +08:00
|
|
|
terminalreporter.write_sep("-", "generated xml file: %s" % (self.logfile))
|