2010-11-06 18:38:53 +08:00
|
|
|
""" terminal reporting of the full testing process.
|
2009-07-22 22:09:49 +08:00
|
|
|
|
2010-07-27 03:15:15 +08:00
|
|
|
This is a good source for looking at the various reporting hooks.
|
2009-07-09 01:18:26 +08:00
|
|
|
"""
|
2012-06-21 17:07:22 +08:00
|
|
|
import pytest
|
2015-05-06 03:53:04 +08:00
|
|
|
import pluggy
|
2012-06-21 17:07:22 +08:00
|
|
|
import py
|
2009-02-27 18:18:27 +08:00
|
|
|
import sys
|
2014-08-01 06:13:40 +08:00
|
|
|
import time
|
2015-06-01 03:31:31 +08:00
|
|
|
import platform
|
2014-08-01 06:13:40 +08:00
|
|
|
|
2009-02-27 18:18:27 +08:00
|
|
|
|
2009-08-03 21:27:26 +08:00
|
|
|
def pytest_addoption(parser):
|
2010-01-03 19:41:29 +08:00
|
|
|
group = parser.getgroup("terminal reporting", "reporting", after="general")
|
2010-07-27 03:15:15 +08:00
|
|
|
group._addoption('-v', '--verbose', action="count",
|
2009-10-17 23:43:59 +08:00
|
|
|
dest="verbose", default=0, help="increase verbosity."),
|
2010-11-01 02:51:16 +08:00
|
|
|
group._addoption('-q', '--quiet', action="count",
|
2012-07-13 00:00:48 +08:00
|
|
|
dest="quiet", default=0, help="decrease verbosity."),
|
2010-05-06 01:50:59 +08:00
|
|
|
group._addoption('-r',
|
|
|
|
action="store", dest="reportchars", default=None, metavar="chars",
|
|
|
|
help="show extra test summary info as specified by chars (f)ailed, "
|
2014-03-12 05:10:18 +08:00
|
|
|
"(E)error, (s)skipped, (x)failed, (X)passed (w)warnings.")
|
2009-10-17 23:43:59 +08:00
|
|
|
group._addoption('-l', '--showlocals',
|
2010-05-06 01:50:59 +08:00
|
|
|
action="store_true", dest="showlocals", default=False,
|
|
|
|
help="show locals in tracebacks (disabled by default).")
|
|
|
|
group._addoption('--report',
|
|
|
|
action="store", dest="report", default=None, metavar="opts",
|
|
|
|
help="(deprecated, use -r)")
|
2010-07-27 03:15:15 +08:00
|
|
|
group._addoption('--tb', metavar="style",
|
2014-06-29 19:32:53 +08:00
|
|
|
action="store", dest="tbstyle", default='auto',
|
|
|
|
choices=['auto', 'long', 'short', 'no', 'line', 'native'],
|
2014-10-11 04:43:33 +08:00
|
|
|
help="traceback print mode (auto/long/short/line/native/no).")
|
2013-08-01 22:49:26 +08:00
|
|
|
group._addoption('--fulltrace', '--full-trace',
|
|
|
|
action="store_true", default=False,
|
2009-10-17 23:43:59 +08:00
|
|
|
help="don't cut any tracebacks (default is to cut).")
|
2013-12-07 03:49:48 +08:00
|
|
|
group._addoption('--color', metavar="color",
|
|
|
|
action="store", dest="color", default='auto',
|
|
|
|
choices=['yes', 'no', 'auto'],
|
2013-12-09 03:25:36 +08:00
|
|
|
help="color terminal output (yes/no/auto).")
|
2009-08-03 21:27:26 +08:00
|
|
|
|
2009-05-19 05:26:16 +08:00
|
|
|
def pytest_configure(config):
|
2010-11-01 02:51:16 +08:00
|
|
|
config.option.verbose -= config.option.quiet
|
2014-04-01 21:03:17 +08:00
|
|
|
reporter = TerminalReporter(config, sys.stdout)
|
2010-09-26 22:23:43 +08:00
|
|
|
config.pluginmanager.register(reporter, 'terminalreporter')
|
2010-11-06 06:37:31 +08:00
|
|
|
if config.option.debug or config.option.traceconfig:
|
|
|
|
def mywriter(tags, args):
|
|
|
|
msg = " ".join(map(str, args))
|
|
|
|
reporter.write_line("[traceconfig] " + msg)
|
|
|
|
config.trace.root.setprocessor("pytest:config", mywriter)
|
2009-02-27 18:18:27 +08:00
|
|
|
|
2010-05-06 01:50:59 +08:00
|
|
|
def getreportopt(config):
|
|
|
|
reportopts = ""
|
2010-11-01 06:28:31 +08:00
|
|
|
optvalue = config.option.report
|
2009-10-17 23:42:40 +08:00
|
|
|
if optvalue:
|
2010-07-27 03:15:15 +08:00
|
|
|
py.builtin.print_("DEPRECATED: use -r instead of --report option.",
|
2014-08-01 06:13:40 +08:00
|
|
|
file=sys.stderr)
|
2010-05-06 01:50:59 +08:00
|
|
|
if optvalue:
|
|
|
|
for setting in optvalue.split(","):
|
|
|
|
setting = setting.strip()
|
|
|
|
if setting == "skipped":
|
|
|
|
reportopts += "s"
|
|
|
|
elif setting == "xfailed":
|
|
|
|
reportopts += "x"
|
2010-11-01 06:28:31 +08:00
|
|
|
reportchars = config.option.reportchars
|
2010-05-06 01:50:59 +08:00
|
|
|
if reportchars:
|
|
|
|
for char in reportchars:
|
|
|
|
if char not in reportopts:
|
|
|
|
reportopts += char
|
|
|
|
return reportopts
|
2009-10-17 23:42:40 +08:00
|
|
|
|
2010-09-26 22:23:44 +08:00
|
|
|
def pytest_report_teststatus(report):
|
|
|
|
if report.passed:
|
|
|
|
letter = "."
|
|
|
|
elif report.skipped:
|
|
|
|
letter = "s"
|
|
|
|
elif report.failed:
|
|
|
|
letter = "F"
|
|
|
|
if report.when != "call":
|
|
|
|
letter = "f"
|
|
|
|
return report.outcome, letter, report.outcome.upper()
|
|
|
|
|
2014-03-12 05:10:17 +08:00
|
|
|
class WarningReport:
|
|
|
|
def __init__(self, code, message, nodeid=None, fslocation=None):
|
|
|
|
self.code = code
|
|
|
|
self.message = message
|
|
|
|
self.nodeid = nodeid
|
|
|
|
self.fslocation = fslocation
|
|
|
|
|
|
|
|
|
2009-02-27 18:18:27 +08:00
|
|
|
class TerminalReporter:
|
|
|
|
def __init__(self, config, file=None):
|
2010-07-27 03:15:15 +08:00
|
|
|
self.config = config
|
2010-11-01 02:51:16 +08:00
|
|
|
self.verbosity = self.config.option.verbose
|
|
|
|
self.showheader = self.verbosity >= 0
|
|
|
|
self.showfspath = self.verbosity >= 0
|
|
|
|
self.showlongtestinfo = self.verbosity > 0
|
2010-11-22 18:59:56 +08:00
|
|
|
self._numcollected = 0
|
2010-11-01 02:51:16 +08:00
|
|
|
|
2010-07-27 03:15:15 +08:00
|
|
|
self.stats = {}
|
2015-02-27 04:56:44 +08:00
|
|
|
self.startdir = py.path.local()
|
2009-02-27 18:18:27 +08:00
|
|
|
if file is None:
|
2014-08-01 06:13:40 +08:00
|
|
|
file = sys.stdout
|
2013-09-27 21:48:03 +08:00
|
|
|
self._tw = self.writer = py.io.TerminalWriter(file)
|
2013-12-07 03:49:48 +08:00
|
|
|
if self.config.option.color == 'yes':
|
|
|
|
self._tw.hasmarkup = True
|
|
|
|
if self.config.option.color == 'no':
|
|
|
|
self._tw.hasmarkup = False
|
2010-07-27 03:15:15 +08:00
|
|
|
self.currentfspath = None
|
2010-05-06 01:50:59 +08:00
|
|
|
self.reportchars = getreportopt(config)
|
2010-11-22 18:59:56 +08:00
|
|
|
self.hasmarkup = self._tw.hasmarkup
|
2009-10-17 23:42:40 +08:00
|
|
|
|
2010-05-06 01:50:59 +08:00
|
|
|
def hasopt(self, char):
|
2013-07-06 21:43:59 +08:00
|
|
|
char = {'xfailed': 'x', 'skipped': 's'}.get(char, char)
|
2010-05-06 01:50:59 +08:00
|
|
|
return char in self.reportchars
|
2009-02-27 18:18:27 +08:00
|
|
|
|
2015-02-27 04:56:44 +08:00
|
|
|
def write_fspath_result(self, nodeid, res):
|
|
|
|
fspath = self.config.rootdir.join(nodeid.split("::")[0])
|
2009-02-27 18:18:27 +08:00
|
|
|
if fspath != self.currentfspath:
|
2010-09-26 22:23:45 +08:00
|
|
|
self.currentfspath = fspath
|
2015-02-27 04:56:44 +08:00
|
|
|
fspath = self.startdir.bestrelpath(fspath)
|
2009-02-27 18:18:27 +08:00
|
|
|
self._tw.line()
|
2010-11-06 16:58:04 +08:00
|
|
|
self._tw.write(fspath + " ")
|
2009-02-27 18:18:27 +08:00
|
|
|
self._tw.write(res)
|
|
|
|
|
2009-03-11 09:40:08 +08:00
|
|
|
def write_ensure_prefix(self, prefix, extra="", **kwargs):
|
2009-02-27 18:18:27 +08:00
|
|
|
if self.currentfspath != prefix:
|
|
|
|
self._tw.line()
|
2010-07-27 03:15:15 +08:00
|
|
|
self.currentfspath = prefix
|
2009-02-27 18:18:27 +08:00
|
|
|
self._tw.write(prefix)
|
|
|
|
if extra:
|
2009-03-11 09:40:08 +08:00
|
|
|
self._tw.write(extra, **kwargs)
|
2009-02-27 18:18:27 +08:00
|
|
|
self.currentfspath = -2
|
|
|
|
|
|
|
|
def ensure_newline(self):
|
2010-07-27 03:15:15 +08:00
|
|
|
if self.currentfspath:
|
2009-02-27 18:18:27 +08:00
|
|
|
self._tw.line()
|
|
|
|
self.currentfspath = None
|
|
|
|
|
2010-11-26 20:37:00 +08:00
|
|
|
def write(self, content, **markup):
|
|
|
|
self._tw.write(content, **markup)
|
|
|
|
|
2009-02-27 18:18:27 +08:00
|
|
|
def write_line(self, line, **markup):
|
2014-03-28 18:27:02 +08:00
|
|
|
if not py.builtin._istext(line):
|
|
|
|
line = py.builtin.text(line, errors="replace")
|
2009-02-27 18:18:27 +08:00
|
|
|
self.ensure_newline()
|
|
|
|
self._tw.line(line, **markup)
|
|
|
|
|
2010-11-22 18:59:56 +08:00
|
|
|
def rewrite(self, line, **markup):
|
|
|
|
line = str(line)
|
|
|
|
self._tw.write("\r" + line, **markup)
|
|
|
|
|
2009-02-27 18:18:27 +08:00
|
|
|
def write_sep(self, sep, title=None, **markup):
|
|
|
|
self.ensure_newline()
|
|
|
|
self._tw.sep(sep, title, **markup)
|
|
|
|
|
2013-09-27 21:48:03 +08:00
|
|
|
def section(self, title, sep="=", **kw):
|
|
|
|
self._tw.sep(sep, title, **kw)
|
|
|
|
|
|
|
|
def line(self, msg, **kw):
|
|
|
|
self._tw.line(msg, **kw)
|
|
|
|
|
2009-04-09 08:12:10 +08:00
|
|
|
def pytest_internalerror(self, excrepr):
|
2014-03-28 18:27:02 +08:00
|
|
|
for line in py.builtin.text(excrepr).split("\n"):
|
2009-04-03 22:18:47 +08:00
|
|
|
self.write_line("INTERNALERROR> " + line)
|
2010-10-05 23:21:41 +08:00
|
|
|
return 1
|
2009-02-27 18:18:27 +08:00
|
|
|
|
2014-03-12 05:10:17 +08:00
|
|
|
def pytest_logwarning(self, code, fslocation, message, nodeid):
|
|
|
|
warnings = self.stats.setdefault("warnings", [])
|
2015-04-27 18:50:34 +08:00
|
|
|
if isinstance(fslocation, tuple):
|
|
|
|
fslocation = "%s:%d" % fslocation
|
2014-03-12 05:10:17 +08:00
|
|
|
warning = WarningReport(code=code, fslocation=fslocation,
|
|
|
|
message=message, nodeid=nodeid)
|
|
|
|
warnings.append(warning)
|
|
|
|
|
2010-04-22 17:57:57 +08:00
|
|
|
def pytest_plugin_registered(self, plugin):
|
2010-07-27 03:15:15 +08:00
|
|
|
if self.config.option.traceconfig:
|
2013-07-06 21:43:59 +08:00
|
|
|
msg = "PLUGIN registered: %s" % (plugin,)
|
2010-07-27 03:15:15 +08:00
|
|
|
# XXX this event may happen during setup/teardown time
|
|
|
|
# which unfortunately captures our output here
|
|
|
|
# which garbles our output if we use self.write_line
|
2010-04-22 17:57:57 +08:00
|
|
|
self.write_line(msg)
|
|
|
|
|
2009-04-09 07:50:02 +08:00
|
|
|
def pytest_deselected(self, items):
|
2010-09-15 16:30:50 +08:00
|
|
|
self.stats.setdefault('deselected', []).extend(items)
|
2009-05-09 00:47:33 +08:00
|
|
|
|
2010-11-06 16:58:04 +08:00
|
|
|
def pytest_runtest_logstart(self, nodeid, location):
|
2010-09-26 22:23:45 +08:00
|
|
|
# ensure that the path is printed before the
|
|
|
|
# 1st test of a module starts running
|
2010-11-01 02:51:16 +08:00
|
|
|
if self.showlongtestinfo:
|
2015-02-27 04:56:44 +08:00
|
|
|
line = self._locationline(nodeid, *location)
|
2010-09-26 22:23:45 +08:00
|
|
|
self.write_ensure_prefix(line, "")
|
2010-11-01 02:51:16 +08:00
|
|
|
elif self.showfspath:
|
2015-02-27 04:56:44 +08:00
|
|
|
fsid = nodeid.split("::")[0]
|
|
|
|
self.write_fspath_result(fsid, "")
|
2010-09-26 22:23:45 +08:00
|
|
|
|
2009-08-04 18:00:04 +08:00
|
|
|
def pytest_runtest_logreport(self, report):
|
|
|
|
rep = report
|
2010-09-26 22:23:44 +08:00
|
|
|
res = self.config.hook.pytest_report_teststatus(report=rep)
|
|
|
|
cat, letter, word = res
|
|
|
|
self.stats.setdefault(cat, []).append(rep)
|
2013-09-27 21:48:03 +08:00
|
|
|
self._tests_ran = True
|
2009-07-31 20:22:02 +08:00
|
|
|
if not letter and not word:
|
|
|
|
# probably passed setup/teardown
|
|
|
|
return
|
2010-11-01 02:51:16 +08:00
|
|
|
if self.verbosity <= 0:
|
|
|
|
if not hasattr(rep, 'node') and self.showfspath:
|
2015-02-27 04:56:44 +08:00
|
|
|
self.write_fspath_result(rep.nodeid, letter)
|
2010-09-28 21:53:10 +08:00
|
|
|
else:
|
|
|
|
self._tw.write(letter)
|
2009-02-27 18:18:27 +08:00
|
|
|
else:
|
2010-09-28 21:53:10 +08:00
|
|
|
if isinstance(word, tuple):
|
|
|
|
word, markup = word
|
|
|
|
else:
|
|
|
|
if rep.passed:
|
|
|
|
markup = {'green':True}
|
|
|
|
elif rep.failed:
|
|
|
|
markup = {'red':True}
|
|
|
|
elif rep.skipped:
|
|
|
|
markup = {'yellow':True}
|
2015-02-27 04:56:44 +08:00
|
|
|
line = self._locationline(rep.nodeid, *rep.location)
|
2009-04-06 04:16:27 +08:00
|
|
|
if not hasattr(rep, 'node'):
|
2009-03-25 19:50:57 +08:00
|
|
|
self.write_ensure_prefix(line, word, **markup)
|
2010-09-26 22:23:45 +08:00
|
|
|
#self._tw.write(word, **markup)
|
2009-03-25 19:50:57 +08:00
|
|
|
else:
|
|
|
|
self.ensure_newline()
|
2009-04-06 04:16:27 +08:00
|
|
|
if hasattr(rep, 'node'):
|
2009-11-20 06:13:29 +08:00
|
|
|
self._tw.write("[%s] " % rep.node.gateway.id)
|
2009-03-25 19:50:57 +08:00
|
|
|
self._tw.write(word, **markup)
|
|
|
|
self._tw.write(" " + line)
|
|
|
|
self.currentfspath = -2
|
2009-02-27 18:18:27 +08:00
|
|
|
|
2010-11-22 18:59:56 +08:00
|
|
|
def pytest_collection(self):
|
2013-07-06 21:43:59 +08:00
|
|
|
if not self.hasmarkup and self.config.option.verbose >= 1:
|
2010-11-26 20:37:00 +08:00
|
|
|
self.write("collecting ... ", bold=True)
|
2010-11-22 18:59:56 +08:00
|
|
|
|
2009-08-04 18:00:04 +08:00
|
|
|
def pytest_collectreport(self, report):
|
2010-11-22 18:59:56 +08:00
|
|
|
if report.failed:
|
|
|
|
self.stats.setdefault("error", []).append(report)
|
|
|
|
elif report.skipped:
|
|
|
|
self.stats.setdefault("skipped", []).append(report)
|
|
|
|
items = [x for x in report.result if isinstance(x, pytest.Item)]
|
|
|
|
self._numcollected += len(items)
|
|
|
|
if self.hasmarkup:
|
2015-02-27 04:56:44 +08:00
|
|
|
#self.write_fspath_result(report.nodeid, 'E')
|
2010-11-22 18:59:56 +08:00
|
|
|
self.report_collect()
|
|
|
|
|
|
|
|
def report_collect(self, final=False):
|
2012-10-07 19:06:17 +08:00
|
|
|
if self.config.option.verbose < 0:
|
|
|
|
return
|
|
|
|
|
2010-11-22 18:59:56 +08:00
|
|
|
errors = len(self.stats.get('error', []))
|
|
|
|
skipped = len(self.stats.get('skipped', []))
|
|
|
|
if final:
|
|
|
|
line = "collected "
|
|
|
|
else:
|
|
|
|
line = "collecting "
|
|
|
|
line += str(self._numcollected) + " items"
|
|
|
|
if errors:
|
|
|
|
line += " / %d errors" % errors
|
|
|
|
if skipped:
|
|
|
|
line += " / %d skipped" % skipped
|
|
|
|
if self.hasmarkup:
|
|
|
|
if final:
|
|
|
|
line += " \n"
|
|
|
|
self.rewrite(line, bold=True)
|
|
|
|
else:
|
|
|
|
self.write_line(line)
|
|
|
|
|
|
|
|
def pytest_collection_modifyitems(self):
|
|
|
|
self.report_collect(True)
|
2009-02-27 18:18:27 +08:00
|
|
|
|
2015-05-06 16:08:08 +08:00
|
|
|
@pytest.hookimpl(trylast=True)
|
2009-05-23 03:53:26 +08:00
|
|
|
def pytest_sessionstart(self, session):
|
2014-08-01 06:13:40 +08:00
|
|
|
self._sessionstarttime = time.time()
|
2010-11-01 02:51:16 +08:00
|
|
|
if not self.showheader:
|
|
|
|
return
|
|
|
|
self.write_sep("=", "test session starts", bold=True)
|
2015-06-01 03:31:31 +08:00
|
|
|
verinfo = platform.python_version()
|
2010-05-22 19:59:01 +08:00
|
|
|
msg = "platform %s -- Python %s" % (sys.platform, verinfo)
|
2010-11-26 03:06:42 +08:00
|
|
|
if hasattr(sys, 'pypy_version_info'):
|
|
|
|
verinfo = ".".join(map(str, sys.pypy_version_info[:3]))
|
2011-07-06 16:18:11 +08:00
|
|
|
msg += "[pypy-%s-%s]" % (verinfo, sys.pypy_version_info[3])
|
2015-05-06 03:53:04 +08:00
|
|
|
msg += ", pytest-%s, py-%s, pluggy-%s" % (
|
|
|
|
pytest.__version__, py.__version__, pluggy.__version__)
|
2010-11-01 02:51:16 +08:00
|
|
|
if self.verbosity > 0 or self.config.option.debug or \
|
2010-09-26 22:23:43 +08:00
|
|
|
getattr(self.config.option, 'pastebin', None):
|
2009-08-19 01:04:57 +08:00
|
|
|
msg += " -- " + str(sys.executable)
|
2009-03-21 09:31:27 +08:00
|
|
|
self.write_line(msg)
|
2012-06-21 17:07:22 +08:00
|
|
|
lines = self.config.hook.pytest_report_header(
|
|
|
|
config=self.config, startdir=self.startdir)
|
2010-01-13 04:43:25 +08:00
|
|
|
lines.reverse()
|
|
|
|
for line in flatten(lines):
|
|
|
|
self.write_line(line)
|
2010-09-26 22:23:43 +08:00
|
|
|
|
2012-06-20 06:16:47 +08:00
|
|
|
def pytest_report_header(self, config):
|
2015-02-27 04:56:44 +08:00
|
|
|
inifile = ""
|
|
|
|
if config.inifile:
|
|
|
|
inifile = config.rootdir.bestrelpath(config.inifile)
|
|
|
|
lines = ["rootdir: %s, inifile: %s" %(config.rootdir, inifile)]
|
2015-05-06 03:53:04 +08:00
|
|
|
|
|
|
|
plugininfo = config.pluginmanager.list_plugin_distinfo()
|
2012-06-20 06:16:47 +08:00
|
|
|
if plugininfo:
|
|
|
|
l = []
|
2015-05-06 03:53:04 +08:00
|
|
|
for plugin, dist in plugininfo:
|
2012-06-20 06:16:47 +08:00
|
|
|
name = dist.project_name
|
|
|
|
if name.startswith("pytest-"):
|
|
|
|
name = name[7:]
|
|
|
|
l.append(name)
|
2015-02-27 04:56:44 +08:00
|
|
|
lines.append("plugins: %s" % ", ".join(l))
|
|
|
|
return lines
|
2012-06-20 06:16:47 +08:00
|
|
|
|
2011-03-07 01:32:00 +08:00
|
|
|
def pytest_collection_finish(self, session):
|
|
|
|
if self.config.option.collectonly:
|
|
|
|
self._printcollecteditems(session.items)
|
|
|
|
if self.stats.get('failed'):
|
|
|
|
self._tw.sep("!", "collection failures")
|
|
|
|
for rep in self.stats.get('failed'):
|
|
|
|
rep.toterminal(self._tw)
|
|
|
|
return 1
|
|
|
|
return 0
|
2010-11-01 02:51:16 +08:00
|
|
|
if not self.showheader:
|
|
|
|
return
|
2010-11-22 18:59:56 +08:00
|
|
|
#for i, testarg in enumerate(self.config.args):
|
|
|
|
# self.write_line("test path %d: %s" %(i+1, testarg))
|
2011-03-08 20:37:00 +08:00
|
|
|
|
2011-03-07 01:32:00 +08:00
|
|
|
def _printcollecteditems(self, items):
|
|
|
|
# to print out items and their parent collectors
|
|
|
|
# we take care to leave out Instances aka ()
|
|
|
|
# because later versions are going to get rid of them anyway
|
|
|
|
if self.config.option.verbose < 0:
|
2012-02-03 23:56:06 +08:00
|
|
|
if self.config.option.verbose < -1:
|
|
|
|
counts = {}
|
|
|
|
for item in items:
|
|
|
|
name = item.nodeid.split('::', 1)[0]
|
|
|
|
counts[name] = counts.get(name, 0) + 1
|
|
|
|
for name, count in sorted(counts.items()):
|
|
|
|
self._tw.line("%s: %d" % (name, count))
|
|
|
|
else:
|
|
|
|
for item in items:
|
|
|
|
nodeid = item.nodeid
|
|
|
|
nodeid = nodeid.replace("::()::", "::")
|
|
|
|
self._tw.line(nodeid)
|
2011-03-07 01:32:00 +08:00
|
|
|
return
|
|
|
|
stack = []
|
|
|
|
indent = ""
|
|
|
|
for item in items:
|
|
|
|
needed_collectors = item.listchain()[1:] # strip root node
|
|
|
|
while stack:
|
|
|
|
if stack == needed_collectors[:len(stack)]:
|
|
|
|
break
|
|
|
|
stack.pop()
|
|
|
|
for col in needed_collectors[len(stack):]:
|
|
|
|
stack.append(col)
|
|
|
|
#if col.name == "()":
|
|
|
|
# continue
|
2013-07-06 21:43:59 +08:00
|
|
|
indent = (len(stack) - 1) * " "
|
|
|
|
self._tw.line("%s%s" % (indent, col))
|
2009-02-27 18:18:27 +08:00
|
|
|
|
2015-05-06 16:08:08 +08:00
|
|
|
@pytest.hookimpl(hookwrapper=True)
|
2014-10-09 02:23:40 +08:00
|
|
|
def pytest_sessionfinish(self, exitstatus):
|
|
|
|
outcome = yield
|
|
|
|
outcome.get_result()
|
2009-02-27 18:18:27 +08:00
|
|
|
self._tw.line("")
|
2013-02-14 19:21:42 +08:00
|
|
|
if exitstatus in (0, 1, 2, 4):
|
2009-07-31 20:22:02 +08:00
|
|
|
self.summary_errors()
|
2009-02-27 18:18:27 +08:00
|
|
|
self.summary_failures()
|
2014-03-12 05:10:17 +08:00
|
|
|
self.summary_warnings()
|
2009-05-08 00:01:53 +08:00
|
|
|
self.config.hook.pytest_terminal_summary(terminalreporter=self)
|
2009-04-03 20:18:25 +08:00
|
|
|
if exitstatus == 2:
|
2009-07-20 20:01:40 +08:00
|
|
|
self._report_keyboardinterrupt()
|
2011-07-08 03:24:09 +08:00
|
|
|
del self._keyboardinterrupt_memo
|
2009-02-27 18:18:27 +08:00
|
|
|
self.summary_deselected()
|
|
|
|
self.summary_stats()
|
|
|
|
|
2009-07-20 20:01:40 +08:00
|
|
|
def pytest_keyboard_interrupt(self, excinfo):
|
2009-12-25 16:53:36 +08:00
|
|
|
self._keyboardinterrupt_memo = excinfo.getrepr(funcargs=True)
|
2009-07-20 20:01:40 +08:00
|
|
|
|
2011-07-08 03:24:09 +08:00
|
|
|
def pytest_unconfigure(self):
|
|
|
|
if hasattr(self, '_keyboardinterrupt_memo'):
|
|
|
|
self._report_keyboardinterrupt()
|
|
|
|
|
2009-07-20 20:01:40 +08:00
|
|
|
def _report_keyboardinterrupt(self):
|
|
|
|
excrepr = self._keyboardinterrupt_memo
|
2010-05-25 22:52:09 +08:00
|
|
|
msg = excrepr.reprcrash.message
|
|
|
|
self.write_sep("!", msg)
|
|
|
|
if "KeyboardInterrupt" in msg:
|
2010-11-01 06:28:31 +08:00
|
|
|
if self.config.option.fulltrace:
|
2010-05-25 22:52:09 +08:00
|
|
|
excrepr.toterminal(self._tw)
|
|
|
|
else:
|
|
|
|
excrepr.reprcrash.toterminal(self._tw)
|
2009-07-20 20:01:40 +08:00
|
|
|
|
2015-02-27 04:56:44 +08:00
|
|
|
def _locationline(self, nodeid, fspath, lineno, domain):
|
|
|
|
def mkrel(nodeid):
|
|
|
|
line = self.config.cwd_relative_nodeid(nodeid)
|
|
|
|
if domain and line.endswith(domain):
|
|
|
|
line = line[:-len(domain)]
|
|
|
|
l = domain.split("[")
|
|
|
|
l[0] = l[0].replace('.', '::') # don't replace '.' in params
|
|
|
|
line += "[".join(l)
|
|
|
|
return line
|
2011-03-08 20:44:53 +08:00
|
|
|
# collect_fspath comes from testid which has a "/"-normalized path
|
2015-02-27 04:56:44 +08:00
|
|
|
|
2011-03-08 20:44:53 +08:00
|
|
|
if fspath:
|
2015-02-27 04:56:44 +08:00
|
|
|
res = mkrel(nodeid).replace("::()", "") # parens-normalization
|
|
|
|
if nodeid.split("::")[0] != fspath.replace("\\", "/"):
|
|
|
|
res += " <- " + self.startdir.bestrelpath(fspath)
|
2009-05-06 05:52:25 +08:00
|
|
|
else:
|
2015-02-27 04:56:44 +08:00
|
|
|
res = "[location]"
|
|
|
|
return res + " "
|
2010-07-27 03:15:15 +08:00
|
|
|
|
2009-05-12 17:05:05 +08:00
|
|
|
def _getfailureheadline(self, rep):
|
2010-09-26 22:23:44 +08:00
|
|
|
if hasattr(rep, 'location'):
|
|
|
|
fspath, lineno, domain = rep.location
|
|
|
|
return domain
|
2009-07-31 20:22:02 +08:00
|
|
|
else:
|
2010-09-26 22:23:44 +08:00
|
|
|
return "test session" # XXX?
|
2009-05-12 23:02:22 +08:00
|
|
|
|
2010-09-26 22:23:44 +08:00
|
|
|
def _getcrashline(self, rep):
|
2009-08-17 22:45:52 +08:00
|
|
|
try:
|
2010-09-26 22:23:44 +08:00
|
|
|
return str(rep.longrepr.reprcrash)
|
2009-08-17 22:45:52 +08:00
|
|
|
except AttributeError:
|
2010-09-26 22:23:44 +08:00
|
|
|
try:
|
|
|
|
return str(rep.longrepr)[:50]
|
|
|
|
except AttributeError:
|
|
|
|
return ""
|
2009-08-17 22:45:52 +08:00
|
|
|
|
2009-02-27 18:18:27 +08:00
|
|
|
#
|
2010-07-27 03:15:15 +08:00
|
|
|
# summaries for sessionfinish
|
2009-02-27 18:18:27 +08:00
|
|
|
#
|
2010-11-23 23:10:47 +08:00
|
|
|
def getreports(self, name):
|
|
|
|
l = []
|
|
|
|
for x in self.stats.get(name, []):
|
|
|
|
if not hasattr(x, '_pdbshown'):
|
|
|
|
l.append(x)
|
|
|
|
return l
|
2009-02-27 18:18:27 +08:00
|
|
|
|
2014-03-12 05:10:17 +08:00
|
|
|
def summary_warnings(self):
|
|
|
|
if self.hasopt("w"):
|
|
|
|
warnings = self.stats.get("warnings")
|
|
|
|
if not warnings:
|
|
|
|
return
|
|
|
|
self.write_sep("=", "warning summary")
|
|
|
|
for w in warnings:
|
|
|
|
self._tw.line("W%s %s %s" % (w.code,
|
|
|
|
w.fslocation, w.message))
|
|
|
|
|
2009-02-27 18:18:27 +08:00
|
|
|
def summary_failures(self):
|
2010-11-23 23:10:47 +08:00
|
|
|
if self.config.option.tbstyle != "no":
|
|
|
|
reports = self.getreports('failed')
|
|
|
|
if not reports:
|
|
|
|
return
|
2009-02-27 18:18:27 +08:00
|
|
|
self.write_sep("=", "FAILURES")
|
2010-11-23 23:10:47 +08:00
|
|
|
for rep in reports:
|
|
|
|
if self.config.option.tbstyle == "line":
|
2010-09-26 22:23:44 +08:00
|
|
|
line = self._getcrashline(rep)
|
2010-01-27 19:52:19 +08:00
|
|
|
self.write_line(line)
|
2010-07-27 03:15:15 +08:00
|
|
|
else:
|
2010-01-27 19:52:19 +08:00
|
|
|
msg = self._getfailureheadline(rep)
|
|
|
|
self.write_sep("_", msg)
|
2011-07-13 05:09:03 +08:00
|
|
|
self._outrep_summary(rep)
|
2009-07-31 20:22:02 +08:00
|
|
|
|
|
|
|
def summary_errors(self):
|
2010-11-23 23:10:47 +08:00
|
|
|
if self.config.option.tbstyle != "no":
|
|
|
|
reports = self.getreports('error')
|
|
|
|
if not reports:
|
|
|
|
return
|
2009-07-31 20:22:02 +08:00
|
|
|
self.write_sep("=", "ERRORS")
|
|
|
|
for rep in self.stats['error']:
|
|
|
|
msg = self._getfailureheadline(rep)
|
|
|
|
if not hasattr(rep, 'when'):
|
|
|
|
# collect
|
2010-07-04 23:06:50 +08:00
|
|
|
msg = "ERROR collecting " + msg
|
2009-07-31 20:22:02 +08:00
|
|
|
elif rep.when == "setup":
|
2010-07-27 03:15:15 +08:00
|
|
|
msg = "ERROR at setup of " + msg
|
2009-07-31 20:22:02 +08:00
|
|
|
elif rep.when == "teardown":
|
2010-07-27 03:15:15 +08:00
|
|
|
msg = "ERROR at teardown of " + msg
|
2009-07-31 20:22:02 +08:00
|
|
|
self.write_sep("_", msg)
|
2011-07-13 05:09:03 +08:00
|
|
|
self._outrep_summary(rep)
|
|
|
|
|
|
|
|
def _outrep_summary(self, rep):
|
|
|
|
rep.toterminal(self._tw)
|
|
|
|
for secname, content in rep.sections:
|
|
|
|
self._tw.sep("-", secname)
|
|
|
|
if content[-1:] == "\n":
|
|
|
|
content = content[:-1]
|
|
|
|
self._tw.line(content)
|
2009-02-27 18:18:27 +08:00
|
|
|
|
|
|
|
def summary_stats(self):
|
2014-08-01 06:13:40 +08:00
|
|
|
session_duration = time.time() - self._sessionstarttime
|
2015-07-03 01:03:05 +08:00
|
|
|
(line, color) = build_summary_stats_line(self.stats)
|
2013-07-06 21:43:59 +08:00
|
|
|
msg = "%s in %.2f seconds" % (line, session_duration)
|
2015-07-03 01:03:05 +08:00
|
|
|
markup = {color: True, 'bold': True}
|
2013-08-05 15:45:10 +08:00
|
|
|
|
2010-11-01 02:51:16 +08:00
|
|
|
if self.verbosity >= 0:
|
2013-07-04 01:43:18 +08:00
|
|
|
self.write_sep("=", msg, **markup)
|
2013-07-06 21:43:59 +08:00
|
|
|
if self.verbosity == -1:
|
2013-08-05 15:45:10 +08:00
|
|
|
self.write_line(msg, **markup)
|
2009-02-27 18:18:27 +08:00
|
|
|
|
|
|
|
def summary_deselected(self):
|
|
|
|
if 'deselected' in self.stats:
|
2011-11-12 07:02:06 +08:00
|
|
|
l = []
|
|
|
|
k = self.config.option.keyword
|
|
|
|
if k:
|
|
|
|
l.append("-k%s" % k)
|
|
|
|
m = self.config.option.markexpr
|
|
|
|
if m:
|
|
|
|
l.append("-m %r" % m)
|
2012-06-21 17:07:22 +08:00
|
|
|
if l:
|
2013-07-06 21:43:59 +08:00
|
|
|
self.write_sep("=", "%d tests deselected by %r" % (
|
2012-06-21 17:07:22 +08:00
|
|
|
len(self.stats['deselected']), " ".join(l)), bold=True)
|
2009-02-27 18:18:27 +08:00
|
|
|
|
|
|
|
def repr_pythonversion(v=None):
|
|
|
|
if v is None:
|
|
|
|
v = sys.version_info
|
|
|
|
try:
|
|
|
|
return "%s.%s.%s-%s-%s" % v
|
|
|
|
except (TypeError, ValueError):
|
|
|
|
return str(v)
|
|
|
|
|
2010-01-13 04:43:25 +08:00
|
|
|
def flatten(l):
|
|
|
|
for x in l:
|
|
|
|
if isinstance(x, (list, tuple)):
|
|
|
|
for y in flatten(x):
|
|
|
|
yield y
|
|
|
|
else:
|
|
|
|
yield x
|
2010-02-05 06:45:07 +08:00
|
|
|
|
2015-07-03 01:03:05 +08:00
|
|
|
def build_summary_stats_line(stats):
|
|
|
|
keys = ("failed passed skipped deselected "
|
2015-07-01 07:27:36 +08:00
|
|
|
"xfailed xpassed warnings error").split()
|
2015-07-01 07:20:25 +08:00
|
|
|
unknown_key_seen = False
|
2015-07-03 01:03:05 +08:00
|
|
|
for key in stats.keys():
|
|
|
|
if key not in keys:
|
2015-07-01 07:06:28 +08:00
|
|
|
if key: # setup/teardown reports have an empty key, ignore them
|
|
|
|
keys.append(key)
|
2015-07-01 07:20:25 +08:00
|
|
|
unknown_key_seen = True
|
2015-07-03 01:03:05 +08:00
|
|
|
parts = []
|
|
|
|
for key in keys:
|
2015-07-01 07:06:28 +08:00
|
|
|
val = stats.get(key, None)
|
|
|
|
if val:
|
|
|
|
parts.append("%d %s" % (len(val), key))
|
2015-07-03 01:03:05 +08:00
|
|
|
line = ", ".join(parts)
|
|
|
|
|
|
|
|
if 'failed' in stats or 'error' in stats:
|
|
|
|
color = 'red'
|
2015-07-01 07:20:25 +08:00
|
|
|
elif 'warnings' in stats or unknown_key_seen:
|
|
|
|
color = 'yellow'
|
2015-07-01 07:32:33 +08:00
|
|
|
elif 'passed' in stats:
|
2015-07-03 01:03:05 +08:00
|
|
|
color = 'green'
|
2015-07-01 07:32:33 +08:00
|
|
|
else:
|
|
|
|
color = 'yellow'
|
2015-07-03 01:03:05 +08:00
|
|
|
|
|
|
|
return (line, color)
|