2012-06-25 23:35:33 +08:00
|
|
|
""" log machine-parseable test session result information in a plain
|
|
|
|
text file.
|
|
|
|
"""
|
2017-03-17 09:21:30 +08:00
|
|
|
from __future__ import absolute_import, division, print_function
|
2009-05-19 05:26:16 +08:00
|
|
|
|
2009-02-27 18:18:27 +08:00
|
|
|
import py
|
2015-06-17 05:36:18 +08:00
|
|
|
import os
|
2009-02-27 18:18:27 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
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")
|
2018-05-23 22:48:46 +08:00
|
|
|
group.addoption(
|
|
|
|
"--resultlog",
|
|
|
|
"--result-log",
|
|
|
|
action="store",
|
|
|
|
metavar="path",
|
|
|
|
default=None,
|
|
|
|
help="DEPRECATED path for machine-readable result log.",
|
|
|
|
)
|
2009-05-19 05:26:16 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
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)
|
2018-05-23 22:48:46 +08:00
|
|
|
if resultlog and not hasattr(config, "slaveinput"):
|
2015-06-17 05:36:18 +08:00
|
|
|
dirname = os.path.dirname(os.path.abspath(resultlog))
|
2015-06-17 19:08:03 +08:00
|
|
|
if not os.path.isdir(dirname):
|
2015-06-17 05:36:18 +08:00
|
|
|
os.makedirs(dirname)
|
2018-05-23 22:48:46 +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)
|
|
|
|
|
2016-08-17 07:22:15 +08:00
|
|
|
from _pytest.deprecated import RESULT_LOG
|
2018-05-23 22:48:46 +08:00
|
|
|
|
|
|
|
config.warn("C1", RESULT_LOG)
|
2016-08-17 07:22:15 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2009-05-19 05:26:16 +08:00
|
|
|
def pytest_unconfigure(config):
|
2018-05-23 22:48:46 +08:00
|
|
|
resultlog = getattr(config, "_resultlog", None)
|
2009-05-19 05:26:16 +08:00
|
|
|
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
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
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:
|
2018-05-23 22:48:46 +08:00
|
|
|
gpath.append(":")
|
2009-02-27 18:18:27 +08:00
|
|
|
fspart = False
|
|
|
|
else:
|
2018-05-23 22:48:46 +08:00
|
|
|
gpath.append(".")
|
2009-02-27 18:18:27 +08:00
|
|
|
else:
|
2018-05-23 22:48:46 +08:00
|
|
|
gpath.append("/")
|
2009-02-27 18:18:27 +08:00
|
|
|
fspart = True
|
|
|
|
name = node.name
|
2018-05-23 22:48:46 +08:00
|
|
|
if name[0] in "([":
|
2009-02-27 18:18:27 +08:00
|
|
|
gpath.pop()
|
|
|
|
gpath.append(name)
|
|
|
|
fspath = newfspath
|
2018-05-23 22:48:46 +08:00
|
|
|
return "".join(gpath)
|
2010-07-02 19:01:21 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2009-02-27 18:18:27 +08:00
|
|
|
class ResultLog(object):
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2009-09-17 21:31:35 +08:00
|
|
|
def __init__(self, config, logfile):
|
|
|
|
self.config = config
|
2017-07-17 07:25:09 +08:00
|
|
|
self.logfile = logfile # preferably line buffered
|
2009-02-27 18:18:27 +08:00
|
|
|
|
2010-09-26 22:23:44 +08:00
|
|
|
def write_log_entry(self, testpath, lettercode, longrepr):
|
2017-03-17 09:27:28 +08:00
|
|
|
print("%s %s" % (lettercode, testpath), file=self.logfile)
|
2009-02-27 18:18:27 +08:00
|
|
|
for line in longrepr.splitlines():
|
2017-03-17 09:27:28 +08:00
|
|
|
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):
|
2018-05-23 22:48:46 +08:00
|
|
|
testpath = getattr(report, "nodeid", None)
|
2010-09-26 22:23:44 +08:00
|
|
|
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]
|
2018-05-23 22:48:46 +08:00
|
|
|
if code == "x":
|
2009-09-17 21:31:35 +08:00
|
|
|
longrepr = str(report.longrepr)
|
2018-05-23 22:48:46 +08:00
|
|
|
elif code == "X":
|
|
|
|
longrepr = ""
|
2009-09-17 21:31:35 +08:00
|
|
|
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"
|
2013-11-19 21:19:29 +08:00
|
|
|
longrepr = str(report.longrepr)
|
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):
|
2018-05-23 22:48:46 +08:00
|
|
|
reprcrash = getattr(excrepr, "reprcrash", None)
|
2011-12-10 16:49:21 +08:00
|
|
|
path = getattr(reprcrash, "path", None)
|
|
|
|
if path is None:
|
|
|
|
path = "cwd:%s" % py.path.local()
|
2018-05-23 22:48:46 +08:00
|
|
|
self.write_log_entry(path, "!", str(excrepr))
|