2012-06-25 23:35:33 +08:00
|
|
|
""" log machine-parseable test session result information in a plain
|
|
|
|
text file.
|
|
|
|
"""
|
2009-05-19 05:26:16 +08:00
|
|
|
|
2009-02-27 18:18:27 +08:00
|
|
|
import py
|
|
|
|
|
2009-05-19 05:26:16 +08:00
|
|
|
def pytest_addoption(parser):
|
2010-12-06 23:54:42 +08:00
|
|
|
group = parser.getgroup("terminal reporting", "resultlog plugin options")
|
|
|
|
group.addoption('--resultlog', action="store", dest="resultlog",
|
|
|
|
metavar="path", default=None,
|
|
|
|
help="path for machine-readable result log.")
|
2009-05-19 05:26:16 +08:00
|
|
|
|
|
|
|
def pytest_configure(config):
|
|
|
|
resultlog = config.option.resultlog
|
2010-07-02 19:01:21 +08:00
|
|
|
# prevent opening resultlog on slave nodes (xdist)
|
|
|
|
if resultlog and not hasattr(config, 'slaveinput'):
|
2009-05-19 05:26:16 +08:00
|
|
|
logfile = open(resultlog, 'w', 1) # line buffered
|
2010-07-27 03:15:15 +08:00
|
|
|
config._resultlog = ResultLog(config, logfile)
|
2009-05-19 05:26:16 +08:00
|
|
|
config.pluginmanager.register(config._resultlog)
|
|
|
|
|
|
|
|
def pytest_unconfigure(config):
|
|
|
|
resultlog = getattr(config, '_resultlog', None)
|
|
|
|
if resultlog:
|
|
|
|
resultlog.logfile.close()
|
2010-07-27 03:15:15 +08:00
|
|
|
del config._resultlog
|
2009-05-19 05:26:16 +08:00
|
|
|
config.pluginmanager.unregister(resultlog)
|
2009-02-27 18:18:27 +08:00
|
|
|
|
|
|
|
def generic_path(item):
|
|
|
|
chain = item.listchain()
|
|
|
|
gpath = [chain[0].name]
|
|
|
|
fspath = chain[0].fspath
|
|
|
|
fspart = False
|
|
|
|
for node in chain[1:]:
|
|
|
|
newfspath = node.fspath
|
|
|
|
if newfspath == fspath:
|
|
|
|
if fspart:
|
|
|
|
gpath.append(':')
|
|
|
|
fspart = False
|
|
|
|
else:
|
2010-07-27 03:15:15 +08:00
|
|
|
gpath.append('.')
|
2009-02-27 18:18:27 +08:00
|
|
|
else:
|
|
|
|
gpath.append('/')
|
|
|
|
fspart = True
|
|
|
|
name = node.name
|
|
|
|
if name[0] in '([':
|
|
|
|
gpath.pop()
|
|
|
|
gpath.append(name)
|
|
|
|
fspath = newfspath
|
|
|
|
return ''.join(gpath)
|
2010-07-02 19:01:21 +08:00
|
|
|
|
2009-02-27 18:18:27 +08:00
|
|
|
class ResultLog(object):
|
2009-09-17 21:31:35 +08:00
|
|
|
def __init__(self, config, logfile):
|
|
|
|
self.config = config
|
2009-02-27 18:18:27 +08:00
|
|
|
self.logfile = logfile # preferably line buffered
|
|
|
|
|
2010-09-26 22:23:44 +08:00
|
|
|
def write_log_entry(self, testpath, lettercode, longrepr):
|
2010-12-06 23:54:42 +08:00
|
|
|
py.builtin.print_("%s %s" % (lettercode, testpath), file=self.logfile)
|
2009-02-27 18:18:27 +08:00
|
|
|
for line in longrepr.splitlines():
|
2010-12-06 23:54:42 +08:00
|
|
|
py.builtin.print_(" %s" % line, file=self.logfile)
|
2009-02-27 18:18:27 +08:00
|
|
|
|
2010-09-26 22:23:44 +08:00
|
|
|
def log_outcome(self, report, lettercode, longrepr):
|
|
|
|
testpath = getattr(report, 'nodeid', None)
|
|
|
|
if testpath is None:
|
|
|
|
testpath = report.fspath
|
|
|
|
self.write_log_entry(testpath, lettercode, longrepr)
|
2009-04-04 00:26:21 +08:00
|
|
|
|
2009-08-04 18:00:04 +08:00
|
|
|
def pytest_runtest_logreport(self, report):
|
2011-11-09 01:53:46 +08:00
|
|
|
if report.when != "call" and report.passed:
|
|
|
|
return
|
2009-09-17 21:31:35 +08:00
|
|
|
res = self.config.hook.pytest_report_teststatus(report=report)
|
2010-09-26 22:23:44 +08:00
|
|
|
code = res[1]
|
2009-09-17 21:31:35 +08:00
|
|
|
if code == 'x':
|
|
|
|
longrepr = str(report.longrepr)
|
2010-05-20 20:35:13 +08:00
|
|
|
elif code == 'X':
|
2009-09-17 21:31:35 +08:00
|
|
|
longrepr = ''
|
|
|
|
elif report.passed:
|
2009-04-04 00:26:21 +08:00
|
|
|
longrepr = ""
|
2009-08-04 18:00:04 +08:00
|
|
|
elif report.failed:
|
2010-07-27 03:15:15 +08:00
|
|
|
longrepr = str(report.longrepr)
|
2009-08-04 18:00:04 +08:00
|
|
|
elif report.skipped:
|
2010-11-14 04:03:28 +08:00
|
|
|
longrepr = str(report.longrepr[2])
|
2010-09-26 22:23:44 +08:00
|
|
|
self.log_outcome(report, code, longrepr)
|
2009-04-04 00:26:21 +08:00
|
|
|
|
2009-08-04 18:00:04 +08:00
|
|
|
def pytest_collectreport(self, report):
|
|
|
|
if not report.passed:
|
2010-07-27 03:15:15 +08:00
|
|
|
if report.failed:
|
2009-04-04 00:26:21 +08:00
|
|
|
code = "F"
|
2010-11-14 04:03:28 +08:00
|
|
|
longrepr = str(report.longrepr.reprcrash)
|
2009-04-04 00:26:21 +08:00
|
|
|
else:
|
2009-08-04 18:00:04 +08:00
|
|
|
assert report.skipped
|
2009-04-04 00:26:21 +08:00
|
|
|
code = "S"
|
2010-11-14 04:03:28 +08:00
|
|
|
longrepr = "%s:%d: %s" % report.longrepr
|
2010-09-26 22:23:44 +08:00
|
|
|
self.log_outcome(report, code, longrepr)
|
2009-02-27 18:18:27 +08:00
|
|
|
|
2009-04-09 08:12:10 +08:00
|
|
|
def pytest_internalerror(self, excrepr):
|
2011-12-10 16:49:21 +08:00
|
|
|
reprcrash = getattr(excrepr, 'reprcrash', None)
|
|
|
|
path = getattr(reprcrash, "path", None)
|
|
|
|
if path is None:
|
|
|
|
path = "cwd:%s" % py.path.local()
|
2009-04-04 00:26:21 +08:00
|
|
|
self.write_log_entry(path, '!', str(excrepr))
|