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
|
|
|
"""
|
|
|
|
|
|
|
|
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]
|
|
|
|
_illegal_unichrs = [(0x00, 0x08), (0x0B, 0x0C), (0x0E, 0x19),
|
|
|
|
(0xD800, 0xDFFF), (0xFDD0, 0xFFFF)]
|
|
|
|
_illegal_ranges = [unicode("%s-%s") % (unichr(low), unichr(high))
|
|
|
|
for (low, high) in _illegal_unichrs
|
|
|
|
if low < sys.maxunicode]
|
|
|
|
illegal_xml_re = re.compile(unicode('[%s]') %
|
|
|
|
unicode('').join(_illegal_ranges))
|
|
|
|
del _illegal_unichrs
|
|
|
|
del _illegal_ranges
|
|
|
|
|
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
|
|
|
|
return 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")
|
2010-07-27 03:15:15 +08:00
|
|
|
group.addoption('--junitxml', action="store", dest="xmlpath",
|
2009-12-31 18:25:07 +08:00
|
|
|
metavar="path", default=None,
|
2010-01-17 06:33:26 +08:00
|
|
|
help="create junit-xml style report file at given path.")
|
2010-07-27 03:15:15 +08:00
|
|
|
group.addoption('--junitprefix', action="store", dest="junitprefix",
|
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
|
|
|
|
if xmlpath:
|
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
|
|
|
|
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))
|
|
|
|
self.logfile = os.path.normpath(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):
|
2010-11-06 16:58:04 +08:00
|
|
|
names = report.nodeid.split("::")
|
2010-11-07 19:05:32 +08:00
|
|
|
names[0] = names[0].replace("/", '.')
|
2010-09-26 22:23:44 +08:00
|
|
|
names = [x.replace(".py", "") for x in names if x != "()"]
|
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),
|
|
|
|
name=names[-1],
|
|
|
|
time=getattr(report, 'duration', 0)
|
|
|
|
))
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
def append_failure(self, report):
|
|
|
|
#msg = str(report.longrepr.reprtraceback.extraline)
|
2010-05-20 20:35:13 +08:00
|
|
|
if "xfail" in report.keywords:
|
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-07-13 05:09:03 +08:00
|
|
|
sec = dict(report.sections)
|
2011-12-02 03:08:51 +08:00
|
|
|
fail = Junit.failure(message="test failure")
|
|
|
|
fail.append(str(report.longrepr))
|
|
|
|
self.append(fail)
|
2011-07-13 05:09:03 +08:00
|
|
|
for name in ('out', 'err'):
|
|
|
|
content = sec.get("Captured std%s" % name)
|
|
|
|
if content:
|
2011-12-02 03:08:51 +08:00
|
|
|
tag = getattr(Junit, 'system-'+name)
|
|
|
|
self.append(tag(bin_xml_escape(content)))
|
2010-05-20 20:35:13 +08:00
|
|
|
self.failed += 1
|
2009-12-31 18:25:07 +08:00
|
|
|
|
|
|
|
def append_collect_failure(self, report):
|
|
|
|
#msg = str(report.longrepr.reprtraceback.extraline)
|
2011-12-02 03:08:51 +08:00
|
|
|
self.append(Junit.failure(str(report.longrepr),
|
|
|
|
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)
|
2011-12-02 03:08:51 +08:00
|
|
|
self.append(Junit.skipped(str(report.longrepr),
|
|
|
|
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):
|
2011-12-02 03:08:51 +08:00
|
|
|
self.append(Junit.error(str(report.longrepr),
|
|
|
|
message="test setup failure"))
|
2009-12-31 18:25:07 +08:00
|
|
|
self.errors += 1
|
|
|
|
|
|
|
|
def append_skipped(self, report):
|
2010-05-20 20:35:13 +08:00
|
|
|
if "xfail" in report.keywords:
|
2011-12-02 03:08:51 +08:00
|
|
|
self.append(Junit.skipped(str(report.keywords['xfail']),
|
|
|
|
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: "):
|
|
|
|
skipreason = skipreason[9:]
|
2011-12-02 03:08:51 +08:00
|
|
|
self.append(
|
|
|
|
Junit.skipped("%s:%s: %s" % report.longrepr,
|
|
|
|
type="pytest.skip",
|
|
|
|
message=skipreason
|
|
|
|
))
|
2009-12-31 18:25:07 +08:00
|
|
|
self.skipped += 1
|
|
|
|
|
|
|
|
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
|
2010-04-27 21:15:43 +08:00
|
|
|
data = py.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
|
|
|
|
|
|
|
def pytest_sessionstart(self, session):
|
|
|
|
self.suite_start_time = time.time()
|
|
|
|
|
|
|
|
def pytest_sessionfinish(self, session, exitstatus, __multicall__):
|
2010-04-27 21:15:43 +08:00
|
|
|
if py.std.sys.version_info[0] < 3:
|
|
|
|
logfile = py.std.codecs.open(self.logfile, 'w', encoding='utf-8')
|
|
|
|
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,
|
|
|
|
name="",
|
|
|
|
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))
|