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
|
2009-12-31 18:25:07 +08:00
|
|
|
import time
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
class LogXML(object):
|
2010-06-09 22:18:47 +08:00
|
|
|
def __init__(self, logfile, prefix):
|
2009-12-31 18:25:07 +08:00
|
|
|
self.logfile = 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)
|
2010-09-26 22:23:44 +08:00
|
|
|
d = {'time': self._durations.pop(names, "0")}
|
|
|
|
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):
|
|
|
|
args = tuple([py.xml.escape(arg) for arg in args])
|
|
|
|
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:
|
|
|
|
self.appendlog("<skipped/>")
|
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__):
|
2010-09-26 22:23:44 +08:00
|
|
|
names = tuple(item.listnames())
|
2009-12-31 18:25:07 +08:00
|
|
|
start = time.time()
|
|
|
|
try:
|
|
|
|
return __multicall__.execute()
|
|
|
|
finally:
|
2010-09-26 22:23:44 +08:00
|
|
|
self._durations[names] = 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))
|