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
|
|
|
|
|
|
|
|
|
|
|
|
# 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
|
|
|
|
|
|
|
|
|
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
|
2009-12-31 18:25:07 +08:00
|
|
|
self.test_logs = []
|
|
|
|
self.passed = self.skipped = 0
|
|
|
|
self.failed = self.errors = 0
|
|
|
|
self._durations = {}
|
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-11-06 16:58:04 +08:00
|
|
|
names = tuple(names)
|
2011-05-28 20:03:10 +08:00
|
|
|
d = {'time': self._durations.pop(report.nodeid, "0")}
|
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)
|
|
|
|
d['classname'] = ".".join(classnames)
|
2010-06-09 21:27:45 +08:00
|
|
|
d['name'] = py.xml.escape(names[-1])
|
2009-12-31 18:25:07 +08:00
|
|
|
attrs = ['%s="%s"' % item for item in sorted(d.items())]
|
|
|
|
self.test_logs.append("\n<testcase %s>" % " ".join(attrs))
|
|
|
|
|
|
|
|
def _closetestcase(self):
|
|
|
|
self.test_logs.append("</testcase>")
|
2010-04-27 21:15:43 +08:00
|
|
|
|
|
|
|
def appendlog(self, fmt, *args):
|
2011-04-16 07:09:25 +08:00
|
|
|
def repl(matchobj):
|
|
|
|
i = ord(matchobj.group())
|
|
|
|
if i <= 0xFF:
|
|
|
|
return unicode('#x%02X') % i
|
|
|
|
else:
|
|
|
|
return unicode('#x%04X') % i
|
|
|
|
args = tuple([illegal_xml_re.sub(repl, py.xml.escape(arg))
|
|
|
|
for arg in args])
|
2010-04-27 21:15:43 +08:00
|
|
|
self.test_logs.append(fmt % args)
|
2010-07-27 03:15:15 +08:00
|
|
|
|
2009-12-31 18:25:07 +08:00
|
|
|
def append_pass(self, report):
|
|
|
|
self.passed += 1
|
|
|
|
self._opentestcase(report)
|
|
|
|
self._closetestcase()
|
|
|
|
|
|
|
|
def append_failure(self, report):
|
|
|
|
self._opentestcase(report)
|
|
|
|
#msg = str(report.longrepr.reprtraceback.extraline)
|
2010-05-20 20:35:13 +08:00
|
|
|
if "xfail" in report.keywords:
|
|
|
|
self.appendlog(
|
|
|
|
'<skipped message="xfail-marked test passes unexpectedly"/>')
|
|
|
|
self.skipped += 1
|
|
|
|
else:
|
2010-07-27 03:15:15 +08:00
|
|
|
self.appendlog('<failure message="test failure">%s</failure>',
|
2010-05-20 20:35:13 +08:00
|
|
|
report.longrepr)
|
|
|
|
self.failed += 1
|
2009-12-31 18:25:07 +08:00
|
|
|
self._closetestcase()
|
|
|
|
|
|
|
|
def append_collect_failure(self, report):
|
2010-06-09 22:18:47 +08:00
|
|
|
self._opentestcase(report)
|
2009-12-31 18:25:07 +08:00
|
|
|
#msg = str(report.longrepr.reprtraceback.extraline)
|
2010-07-27 03:15:15 +08:00
|
|
|
self.appendlog('<failure message="collection failure">%s</failure>',
|
2010-04-27 21:15:43 +08:00
|
|
|
report.longrepr)
|
2009-12-31 18:25:07 +08:00
|
|
|
self._closetestcase()
|
|
|
|
self.errors += 1
|
|
|
|
|
2009-12-31 18:50:01 +08:00
|
|
|
def append_collect_skipped(self, report):
|
2010-06-09 22:18:47 +08:00
|
|
|
self._opentestcase(report)
|
2009-12-31 18:50:01 +08:00
|
|
|
#msg = str(report.longrepr.reprtraceback.extraline)
|
2010-04-27 21:15:43 +08:00
|
|
|
self.appendlog('<skipped message="collection skipped">%s</skipped>',
|
|
|
|
report.longrepr)
|
2009-12-31 18:50:01 +08:00
|
|
|
self._closetestcase()
|
|
|
|
self.skipped += 1
|
|
|
|
|
2009-12-31 18:25:07 +08:00
|
|
|
def append_error(self, report):
|
|
|
|
self._opentestcase(report)
|
2010-07-27 03:15:15 +08:00
|
|
|
self.appendlog('<error message="test setup failure">%s</error>',
|
2010-04-27 21:15:43 +08:00
|
|
|
report.longrepr)
|
2009-12-31 18:25:07 +08:00
|
|
|
self._closetestcase()
|
|
|
|
self.errors += 1
|
|
|
|
|
|
|
|
def append_skipped(self, report):
|
|
|
|
self._opentestcase(report)
|
2010-05-20 20:35:13 +08:00
|
|
|
if "xfail" in report.keywords:
|
|
|
|
self.appendlog(
|
2010-07-27 03:15:15 +08:00
|
|
|
'<skipped message="expected test failure">%s</skipped>',
|
2010-05-20 20:35:13 +08:00
|
|
|
report.keywords['xfail'])
|
|
|
|
else:
|
2011-03-20 00:59:07 +08:00
|
|
|
filename, lineno, skipreason = report.longrepr
|
|
|
|
if skipreason.startswith("Skipped: "):
|
|
|
|
skipreason = skipreason[9:]
|
|
|
|
self.appendlog('<skipped type="pytest.skip" '
|
|
|
|
'message="%s">%s</skipped>',
|
|
|
|
skipreason, "%s:%s: %s" % report.longrepr,
|
|
|
|
)
|
2009-12-31 18:25:07 +08:00
|
|
|
self._closetestcase()
|
|
|
|
self.skipped += 1
|
|
|
|
|
|
|
|
def pytest_runtest_logreport(self, report):
|
|
|
|
if report.passed:
|
|
|
|
self.append_pass(report)
|
|
|
|
elif report.failed:
|
|
|
|
if report.when != "call":
|
|
|
|
self.append_error(report)
|
|
|
|
else:
|
|
|
|
self.append_failure(report)
|
|
|
|
elif report.skipped:
|
|
|
|
self.append_skipped(report)
|
2010-07-27 03:15:15 +08:00
|
|
|
|
2009-12-31 18:25:07 +08:00
|
|
|
def pytest_runtest_call(self, item, __multicall__):
|
|
|
|
start = time.time()
|
|
|
|
try:
|
|
|
|
return __multicall__.execute()
|
|
|
|
finally:
|
2011-05-28 20:03:10 +08:00
|
|
|
self._durations[item.nodeid] = time.time() - start
|
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:
|
|
|
|
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)
|
2009-12-31 18:25:07 +08:00
|
|
|
self.test_logs.append(
|
|
|
|
'\n<testcase classname="pytest" name="internal">'
|
|
|
|
' <error message="internal error">'
|
|
|
|
'%s</error></testcase>' % data)
|
|
|
|
|
|
|
|
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
|
2010-04-28 21:24:38 +08:00
|
|
|
logfile.write('<?xml version="1.0" encoding="utf-8"?>')
|
2009-12-31 18:25:07 +08:00
|
|
|
logfile.write('<testsuite ')
|
|
|
|
logfile.write('name="" ')
|
|
|
|
logfile.write('errors="%i" ' % self.errors)
|
|
|
|
logfile.write('failures="%i" ' % self.failed)
|
|
|
|
logfile.write('skips="%i" ' % self.skipped)
|
|
|
|
logfile.write('tests="%i" ' % numtests)
|
|
|
|
logfile.write('time="%.3f"' % suite_time_delta)
|
|
|
|
logfile.write(' >')
|
|
|
|
logfile.writelines(self.test_logs)
|
|
|
|
logfile.write('</testsuite>')
|
|
|
|
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))
|