2009-02-27 18:18:27 +08:00
|
|
|
import py
|
|
|
|
import sys
|
|
|
|
|
|
|
|
class TerminalPlugin(object):
|
|
|
|
""" Report a test run to a terminal. """
|
|
|
|
def pytest_configure(self, config):
|
|
|
|
if config.option.collectonly:
|
|
|
|
self.reporter = CollectonlyReporter(config)
|
|
|
|
else:
|
|
|
|
self.reporter = TerminalReporter(config)
|
|
|
|
# XXX see remote.py's XXX
|
|
|
|
for attr in 'pytest_terminal_hasmarkup', 'pytest_terminal_fullwidth':
|
|
|
|
if hasattr(config, attr):
|
|
|
|
#print "SETTING TERMINAL OPTIONS", attr, getattr(config, attr)
|
|
|
|
name = attr.split("_")[-1]
|
|
|
|
assert hasattr(self.reporter._tw, name), name
|
|
|
|
setattr(self.reporter._tw, name, getattr(config, attr))
|
2009-04-09 22:03:09 +08:00
|
|
|
config.pluginmanager.register(self.reporter)
|
2009-02-27 18:18:27 +08:00
|
|
|
|
|
|
|
class TerminalReporter:
|
|
|
|
def __init__(self, config, file=None):
|
|
|
|
self.config = config
|
|
|
|
self.stats = {}
|
|
|
|
self.curdir = py.path.local()
|
|
|
|
if file is None:
|
|
|
|
file = py.std.sys.stdout
|
|
|
|
self._tw = py.io.TerminalWriter(file)
|
|
|
|
self.currentfspath = None
|
2009-03-23 04:44:45 +08:00
|
|
|
self.gateway2info = {}
|
2009-02-27 18:18:27 +08:00
|
|
|
|
|
|
|
def write_fspath_result(self, fspath, res):
|
2009-04-01 01:58:02 +08:00
|
|
|
fspath = self.curdir.bestrelpath(fspath)
|
2009-02-27 18:18:27 +08:00
|
|
|
if fspath != self.currentfspath:
|
|
|
|
self._tw.line()
|
|
|
|
relpath = self.curdir.bestrelpath(fspath)
|
|
|
|
self._tw.write(relpath + " ")
|
|
|
|
self.currentfspath = fspath
|
|
|
|
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()
|
|
|
|
self.currentfspath = prefix
|
|
|
|
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):
|
|
|
|
if self.currentfspath:
|
|
|
|
self._tw.line()
|
|
|
|
self.currentfspath = None
|
|
|
|
|
|
|
|
def write_line(self, line, **markup):
|
|
|
|
line = str(line)
|
|
|
|
self.ensure_newline()
|
|
|
|
self._tw.line(line, **markup)
|
|
|
|
|
|
|
|
def write_sep(self, sep, title=None, **markup):
|
|
|
|
self.ensure_newline()
|
|
|
|
self._tw.sep(sep, title, **markup)
|
|
|
|
|
2009-04-08 18:06:21 +08:00
|
|
|
def getcategoryletterword(self, rep):
|
2009-04-10 00:55:11 +08:00
|
|
|
res = self.config.api.pytest_report_teststatus(rep=rep)
|
2009-02-27 18:18:27 +08:00
|
|
|
if res:
|
|
|
|
return res
|
|
|
|
for cat in 'skipped failed passed ???'.split():
|
2009-04-08 18:06:21 +08:00
|
|
|
if getattr(rep, cat, None):
|
2009-02-27 18:18:27 +08:00
|
|
|
break
|
2009-04-08 18:06:21 +08:00
|
|
|
return cat, self.getoutcomeletter(rep), self.getoutcomeword(rep)
|
2009-02-27 18:18:27 +08:00
|
|
|
|
2009-04-06 04:16:27 +08:00
|
|
|
def getoutcomeletter(self, rep):
|
|
|
|
return rep.shortrepr
|
2009-02-27 18:18:27 +08:00
|
|
|
|
2009-04-06 04:16:27 +08:00
|
|
|
def getoutcomeword(self, rep):
|
|
|
|
if rep.passed:
|
2009-03-11 09:40:08 +08:00
|
|
|
return "PASS", dict(green=True)
|
2009-04-06 04:16:27 +08:00
|
|
|
elif rep.failed:
|
2009-03-11 09:40:08 +08:00
|
|
|
return "FAIL", dict(red=True)
|
2009-04-06 04:16:27 +08:00
|
|
|
elif rep.skipped:
|
2009-02-27 18:18:27 +08:00
|
|
|
return "SKIP"
|
|
|
|
else:
|
2009-03-11 09:40:08 +08:00
|
|
|
return "???", dict(red=True)
|
2009-02-27 18:18:27 +08:00
|
|
|
|
2009-04-09 08:12:10 +08:00
|
|
|
def pytest_internalerror(self, excrepr):
|
2009-04-03 22:18:47 +08:00
|
|
|
for line in str(excrepr).split("\n"):
|
|
|
|
self.write_line("INTERNALERROR> " + line)
|
2009-02-27 18:18:27 +08:00
|
|
|
|
2009-04-09 01:50:14 +08:00
|
|
|
def pyexecnet_gwmanage_newgateway(self, gateway, platinfo):
|
2009-03-21 09:31:27 +08:00
|
|
|
#self.write_line("%s instantiated gateway from spec %r" %(gateway.id, gateway.spec._spec))
|
|
|
|
d = {}
|
2009-04-09 01:50:14 +08:00
|
|
|
d['version'] = repr_pythonversion(platinfo.version_info)
|
2009-03-21 09:31:27 +08:00
|
|
|
d['id'] = gateway.id
|
|
|
|
d['spec'] = gateway.spec._spec
|
2009-04-09 01:50:14 +08:00
|
|
|
d['platform'] = platinfo.platform
|
2009-03-21 09:31:27 +08:00
|
|
|
if self.config.option.verbose:
|
2009-04-09 01:50:14 +08:00
|
|
|
d['extra'] = "- " + platinfo.executable
|
2009-03-21 09:31:27 +08:00
|
|
|
else:
|
|
|
|
d['extra'] = ""
|
2009-04-09 01:50:14 +08:00
|
|
|
d['cwd'] = platinfo.cwd
|
2009-03-23 04:44:45 +08:00
|
|
|
infoline = ("%(id)s %(spec)s -- platform %(platform)s, "
|
2009-03-21 09:31:27 +08:00
|
|
|
"Python %(version)s "
|
|
|
|
"cwd: %(cwd)s"
|
|
|
|
"%(extra)s" % d)
|
2009-03-23 04:44:45 +08:00
|
|
|
self.write_line(infoline)
|
|
|
|
self.gateway2info[gateway] = infoline
|
2009-03-20 02:25:13 +08:00
|
|
|
|
2009-04-09 01:50:14 +08:00
|
|
|
def pyexecnet_gwmanage_rsyncstart(self, source, gateways):
|
2009-03-20 08:34:59 +08:00
|
|
|
targets = ", ".join([gw.id for gw in gateways])
|
2009-03-21 05:13:47 +08:00
|
|
|
msg = "rsyncstart: %s -> %s" %(source, targets)
|
|
|
|
if not self.config.option.verbose:
|
|
|
|
msg += " # use --verbose to see rsync progress"
|
|
|
|
self.write_line(msg)
|
2009-03-20 08:34:59 +08:00
|
|
|
|
2009-04-09 01:50:14 +08:00
|
|
|
def pyexecnet_gwmanage_rsyncfinish(self, source, gateways):
|
2009-03-20 08:34:59 +08:00
|
|
|
targets = ", ".join([gw.id for gw in gateways])
|
|
|
|
self.write_line("rsyncfinish: %s -> %s" %(source, targets))
|
2009-02-27 18:18:27 +08:00
|
|
|
|
2009-04-09 07:33:48 +08:00
|
|
|
def pytest_plugin_registered(self, plugin):
|
2009-02-27 18:18:27 +08:00
|
|
|
if self.config.option.traceconfig:
|
|
|
|
msg = "PLUGIN registered: %s" %(plugin,)
|
|
|
|
# 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
|
|
|
|
self.write_line(msg)
|
|
|
|
|
2009-04-09 07:33:48 +08:00
|
|
|
def pytest_testnodeready(self, node):
|
2009-03-26 18:16:42 +08:00
|
|
|
self.write_line("%s txnode ready to receive tests" %(node.gateway.id,))
|
2009-03-21 03:04:36 +08:00
|
|
|
|
2009-04-09 07:33:48 +08:00
|
|
|
def pytest_testnodedown(self, node, error):
|
2009-02-27 18:18:27 +08:00
|
|
|
if error:
|
2009-03-21 09:31:27 +08:00
|
|
|
self.write_line("%s node down, error: %s" %(node.gateway.id, error))
|
2009-02-27 18:18:27 +08:00
|
|
|
|
2009-04-09 08:12:10 +08:00
|
|
|
def pytest_trace(self, category, msg):
|
2009-03-17 05:17:14 +08:00
|
|
|
if self.config.option.debug or \
|
|
|
|
self.config.option.traceconfig and category.find("config") != -1:
|
|
|
|
self.write_line("[%s] %s" %(category, msg))
|
|
|
|
|
2009-04-09 07:33:48 +08:00
|
|
|
def pytest_itemstart(self, item, node=None):
|
2009-03-21 10:04:44 +08:00
|
|
|
if self.config.option.debug:
|
2009-05-06 05:52:25 +08:00
|
|
|
line = self._metainfoline(item)
|
2009-02-27 18:18:27 +08:00
|
|
|
extra = ""
|
2009-03-21 10:04:44 +08:00
|
|
|
if node:
|
|
|
|
extra = "-> " + str(node.gateway.id)
|
2009-02-27 18:18:27 +08:00
|
|
|
self.write_ensure_prefix(line, extra)
|
2009-03-25 19:50:57 +08:00
|
|
|
# in dist situations itemstart (currently only means we
|
|
|
|
# queued the item for testing, doesn't tell much
|
|
|
|
elif self.config.option.verbose and self.config.option.dist == "no":
|
|
|
|
# ensure that the path is printed before the 1st test of
|
|
|
|
# a module starts running
|
2009-05-06 05:52:25 +08:00
|
|
|
line = self._metainfoline(item)
|
2009-03-25 19:50:57 +08:00
|
|
|
#self.write_fspath_result(fspath, "")
|
|
|
|
self.write_ensure_prefix(line, "")
|
2009-02-27 18:18:27 +08:00
|
|
|
|
2009-04-09 07:33:48 +08:00
|
|
|
def pytest_rescheduleitems(self, items):
|
2009-02-27 18:18:27 +08:00
|
|
|
if self.config.option.debug:
|
2009-04-04 00:26:21 +08:00
|
|
|
self.write_sep("!", "RESCHEDULING %s " %(items,))
|
2009-02-27 18:18:27 +08:00
|
|
|
|
2009-04-09 07:50:02 +08:00
|
|
|
def pytest_deselected(self, items):
|
2009-04-03 23:47:16 +08:00
|
|
|
self.stats.setdefault('deselected', []).append(items)
|
2009-02-27 18:18:27 +08:00
|
|
|
|
2009-04-09 07:33:48 +08:00
|
|
|
def pytest_itemtestreport(self, rep):
|
2009-04-06 04:16:27 +08:00
|
|
|
fspath = rep.colitem.fspath
|
|
|
|
cat, letter, word = self.getcategoryletterword(rep)
|
2009-03-11 09:40:08 +08:00
|
|
|
if isinstance(word, tuple):
|
|
|
|
word, markup = word
|
|
|
|
else:
|
|
|
|
markup = {}
|
2009-04-06 04:16:27 +08:00
|
|
|
self.stats.setdefault(cat, []).append(rep)
|
2009-02-27 18:18:27 +08:00
|
|
|
if not self.config.option.verbose:
|
|
|
|
self.write_fspath_result(fspath, letter)
|
|
|
|
else:
|
2009-05-06 05:52:25 +08:00
|
|
|
line = self._metainfoline(rep.colitem)
|
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)
|
|
|
|
else:
|
|
|
|
self.ensure_newline()
|
2009-04-06 04:16:27 +08:00
|
|
|
if hasattr(rep, 'node'):
|
|
|
|
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
|
|
|
|
2009-04-09 07:33:48 +08:00
|
|
|
def pytest_collectreport(self, rep):
|
2009-04-06 04:16:27 +08:00
|
|
|
if not rep.passed:
|
|
|
|
if rep.failed:
|
|
|
|
self.stats.setdefault("failed", []).append(rep)
|
|
|
|
msg = rep.longrepr.reprcrash.message
|
|
|
|
self.write_fspath_result(rep.colitem.fspath, "F")
|
|
|
|
elif rep.skipped:
|
|
|
|
self.stats.setdefault("skipped", []).append(rep)
|
|
|
|
self.write_fspath_result(rep.colitem.fspath, "S")
|
2009-02-27 18:18:27 +08:00
|
|
|
|
2009-04-09 07:41:35 +08:00
|
|
|
def pytest_testrunstart(self):
|
2009-02-27 18:18:27 +08:00
|
|
|
self.write_sep("=", "test session starts", bold=True)
|
|
|
|
self._sessionstarttime = py.std.time.time()
|
2009-03-21 09:31:27 +08:00
|
|
|
|
|
|
|
verinfo = ".".join(map(str, sys.version_info[:3]))
|
|
|
|
msg = "python: platform %s -- Python %s" % (sys.platform, verinfo)
|
|
|
|
if self.config.option.verbose or self.config.option.debug:
|
|
|
|
msg += " -- " + str(sys.executable)
|
|
|
|
self.write_line(msg)
|
|
|
|
|
2009-02-27 18:18:27 +08:00
|
|
|
rev = py.__pkg__.getrev()
|
|
|
|
self.write_line("using py lib: %s <rev %s>" % (
|
|
|
|
py.path.local(py.__file__).dirpath(), rev))
|
2009-03-17 05:17:14 +08:00
|
|
|
if self.config.option.traceconfig:
|
|
|
|
plugins = []
|
2009-04-09 22:43:12 +08:00
|
|
|
for plugin in self.config.pluginmanager.comregistry:
|
|
|
|
name = plugin.__class__.__name__
|
|
|
|
if name.endswith("Plugin"):
|
|
|
|
name = name[:-6]
|
|
|
|
#if name == "Conftest":
|
|
|
|
# XXX get filename
|
|
|
|
plugins.append(name)
|
2009-03-17 05:17:14 +08:00
|
|
|
else:
|
2009-04-09 22:43:12 +08:00
|
|
|
plugins.append(str(plugin))
|
|
|
|
|
2009-03-17 05:17:14 +08:00
|
|
|
plugins = ", ".join(plugins)
|
|
|
|
self.write_line("active plugins: %s" %(plugins,))
|
2009-02-27 18:18:27 +08:00
|
|
|
for i, testarg in py.builtin.enumerate(self.config.args):
|
|
|
|
self.write_line("test object %d: %s" %(i+1, testarg))
|
|
|
|
|
2009-04-09 07:41:35 +08:00
|
|
|
def pytest_testrunfinish(self, exitstatus, excrepr=None):
|
2009-02-27 18:18:27 +08:00
|
|
|
self._tw.line("")
|
2009-04-03 20:18:25 +08:00
|
|
|
if exitstatus in (0, 1, 2):
|
2009-02-27 18:18:27 +08:00
|
|
|
self.summary_failures()
|
|
|
|
self.summary_skips()
|
2009-04-08 18:06:21 +08:00
|
|
|
self.config.api.pytest_terminal_summary(terminalreporter=self)
|
2009-04-03 20:18:25 +08:00
|
|
|
if excrepr is not None:
|
|
|
|
self.summary_final_exc(excrepr)
|
|
|
|
if exitstatus == 2:
|
2009-02-27 18:18:27 +08:00
|
|
|
self.write_sep("!", "KEYBOARD INTERRUPT")
|
|
|
|
self.summary_deselected()
|
|
|
|
self.summary_stats()
|
|
|
|
|
2009-04-09 07:50:02 +08:00
|
|
|
def pytest_looponfailinfo(self, failreports, rootdirs):
|
2009-04-04 00:26:21 +08:00
|
|
|
if failreports:
|
2009-02-27 18:18:27 +08:00
|
|
|
self.write_sep("#", "LOOPONFAILING", red=True)
|
2009-04-04 00:26:21 +08:00
|
|
|
for report in failreports:
|
2009-02-27 18:18:27 +08:00
|
|
|
try:
|
|
|
|
loc = report.longrepr.reprcrash
|
|
|
|
except AttributeError:
|
|
|
|
loc = str(report.longrepr)[:50]
|
|
|
|
self.write_line(loc, red=True)
|
|
|
|
self.write_sep("#", "waiting for changes")
|
2009-04-04 00:26:21 +08:00
|
|
|
for rootdir in rootdirs:
|
2009-02-27 18:18:27 +08:00
|
|
|
self.write_line("### Watching: %s" %(rootdir,), bold=True)
|
|
|
|
|
2009-05-06 05:52:25 +08:00
|
|
|
def _metainfoline(self, item):
|
|
|
|
fspath, lineno, msg = item.metainfo()
|
|
|
|
if fspath:
|
|
|
|
fspath = self.curdir.bestrelpath(fspath)
|
|
|
|
if lineno is not None:
|
|
|
|
lineno += 1
|
|
|
|
if fspath and lineno and msg:
|
|
|
|
line = "%(fspath)s:%(lineno)s: %(msg)s"
|
|
|
|
elif fspath and msg:
|
|
|
|
line = "%(fspath)s: %(msg)s"
|
|
|
|
elif fspath and lineno:
|
|
|
|
line = "%(fspath)s:%(lineno)s"
|
|
|
|
else:
|
|
|
|
line = "[nometainfo]"
|
|
|
|
return line % locals() + " "
|
|
|
|
|
2009-02-27 18:18:27 +08:00
|
|
|
#
|
2009-04-03 20:18:25 +08:00
|
|
|
# summaries for testrunfinish
|
2009-02-27 18:18:27 +08:00
|
|
|
#
|
|
|
|
|
|
|
|
def summary_failures(self):
|
|
|
|
if 'failed' in self.stats and self.config.option.tbstyle != "no":
|
|
|
|
self.write_sep("=", "FAILURES")
|
|
|
|
for ev in self.stats['failed']:
|
2009-03-23 04:44:45 +08:00
|
|
|
self.write_sep("_", "FAILURES")
|
|
|
|
if hasattr(ev, 'node'):
|
|
|
|
self.write_line(self.gateway2info.get(
|
|
|
|
ev.node.gateway, "node %r (platinfo not found? strange)")
|
|
|
|
[:self._tw.fullwidth-1])
|
2009-02-27 18:18:27 +08:00
|
|
|
ev.toterminal(self._tw)
|
|
|
|
|
|
|
|
def summary_stats(self):
|
|
|
|
session_duration = py.std.time.time() - self._sessionstarttime
|
|
|
|
|
|
|
|
keys = "failed passed skipped deselected".split()
|
|
|
|
parts = []
|
|
|
|
for key in keys:
|
|
|
|
val = self.stats.get(key, None)
|
|
|
|
if val:
|
|
|
|
parts.append("%d %s" %(len(val), key))
|
|
|
|
line = ", ".join(parts)
|
|
|
|
# XXX coloring
|
|
|
|
self.write_sep("=", "%s in %.2f seconds" %(line, session_duration))
|
|
|
|
|
|
|
|
def summary_deselected(self):
|
|
|
|
if 'deselected' in self.stats:
|
|
|
|
self.write_sep("=", "%d tests deselected by %r" %(
|
|
|
|
len(self.stats['deselected']), self.config.option.keyword), bold=True)
|
|
|
|
|
|
|
|
def summary_skips(self):
|
|
|
|
if 'skipped' in self.stats:
|
2009-03-22 04:20:02 +08:00
|
|
|
if 'failed' not in self.stats: # or self.config.option.showskipsummary:
|
2009-02-27 18:18:27 +08:00
|
|
|
fskips = folded_skips(self.stats['skipped'])
|
|
|
|
if fskips:
|
|
|
|
self.write_sep("_", "skipped test summary")
|
|
|
|
for num, fspath, lineno, reason in fskips:
|
|
|
|
self._tw.line("%s:%d: [%d] %s" %(fspath, lineno, num, reason))
|
|
|
|
|
|
|
|
def summary_final_exc(self, excrepr):
|
|
|
|
self.write_sep("!")
|
|
|
|
if self.config.option.verbose:
|
|
|
|
excrepr.toterminal(self._tw)
|
|
|
|
else:
|
|
|
|
excrepr.reprcrash.toterminal(self._tw)
|
|
|
|
|
|
|
|
|
|
|
|
class CollectonlyReporter:
|
|
|
|
INDENT = " "
|
|
|
|
|
|
|
|
def __init__(self, config, out=None):
|
|
|
|
self.config = config
|
|
|
|
if out is None:
|
|
|
|
out = py.std.sys.stdout
|
|
|
|
self.out = py.io.TerminalWriter(out)
|
|
|
|
self.indent = ""
|
|
|
|
self._failed = []
|
|
|
|
|
|
|
|
def outindent(self, line):
|
|
|
|
self.out.line(self.indent + str(line))
|
|
|
|
|
2009-04-09 07:33:48 +08:00
|
|
|
def pytest_collectstart(self, collector):
|
2009-04-03 23:47:16 +08:00
|
|
|
self.outindent(collector)
|
2009-02-27 18:18:27 +08:00
|
|
|
self.indent += self.INDENT
|
|
|
|
|
2009-04-09 07:33:48 +08:00
|
|
|
def pytest_itemstart(self, item, node=None):
|
2009-03-21 10:04:44 +08:00
|
|
|
self.outindent(item)
|
2009-02-27 18:18:27 +08:00
|
|
|
|
2009-04-09 07:33:48 +08:00
|
|
|
def pytest_collectreport(self, rep):
|
2009-04-06 04:16:27 +08:00
|
|
|
if not rep.passed:
|
|
|
|
self.outindent("!!! %s !!!" % rep.longrepr.reprcrash.message)
|
|
|
|
self._failed.append(rep)
|
2009-02-27 18:18:27 +08:00
|
|
|
self.indent = self.indent[:-len(self.INDENT)]
|
|
|
|
|
2009-04-09 07:41:35 +08:00
|
|
|
def pytest_testrunfinish(self, exitstatus, excrepr=None):
|
2009-02-27 18:18:27 +08:00
|
|
|
if self._failed:
|
|
|
|
self.out.sep("!", "collection failures")
|
2009-04-06 04:16:27 +08:00
|
|
|
for rep in self._failed:
|
|
|
|
rep.toterminal(self.out)
|
2009-02-27 18:18:27 +08:00
|
|
|
|
|
|
|
def folded_skips(skipped):
|
|
|
|
d = {}
|
|
|
|
for event in skipped:
|
|
|
|
entry = event.longrepr.reprcrash
|
|
|
|
key = entry.path, entry.lineno, entry.message
|
|
|
|
d.setdefault(key, []).append(event)
|
|
|
|
l = []
|
|
|
|
for key, events in d.iteritems():
|
|
|
|
l.append((len(events),) + key)
|
|
|
|
return l
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
# ===============================================================================
|
|
|
|
#
|
|
|
|
# plugin tests
|
|
|
|
#
|
|
|
|
# ===============================================================================
|
|
|
|
|
2009-04-04 08:34:20 +08:00
|
|
|
from py.__.test import runner
|
2009-02-27 18:18:27 +08:00
|
|
|
|
|
|
|
class TestTerminal:
|
|
|
|
|
|
|
|
def test_pass_skip_fail(self, testdir, linecomp):
|
|
|
|
modcol = testdir.getmodulecol("""
|
|
|
|
import py
|
|
|
|
def test_ok():
|
|
|
|
pass
|
|
|
|
def test_skip():
|
|
|
|
py.test.skip("xx")
|
|
|
|
def test_func():
|
|
|
|
assert 0
|
|
|
|
""")
|
2009-03-18 07:48:07 +08:00
|
|
|
rep = TerminalReporter(modcol.config, file=linecomp.stringio)
|
2009-04-09 22:03:09 +08:00
|
|
|
rep.config.pluginmanager.register(rep)
|
2009-04-09 07:41:35 +08:00
|
|
|
rep.config.api.pytest_testrunstart()
|
2009-02-27 18:18:27 +08:00
|
|
|
|
|
|
|
for item in testdir.genitems([modcol]):
|
2009-04-04 08:34:20 +08:00
|
|
|
ev = runner.basic_run_report(item)
|
2009-04-09 07:33:48 +08:00
|
|
|
rep.config.api.pytest_itemtestreport(rep=ev)
|
2009-02-27 18:18:27 +08:00
|
|
|
linecomp.assert_contains_lines([
|
|
|
|
"*test_pass_skip_fail.py .sF"
|
|
|
|
])
|
2009-04-09 07:41:35 +08:00
|
|
|
rep.config.api.pytest_testrunfinish(exitstatus=1)
|
2009-02-27 18:18:27 +08:00
|
|
|
linecomp.assert_contains_lines([
|
|
|
|
" def test_func():",
|
|
|
|
"> assert 0",
|
|
|
|
"E assert 0",
|
|
|
|
])
|
|
|
|
|
|
|
|
def test_pass_skip_fail_verbose(self, testdir, linecomp):
|
|
|
|
modcol = testdir.getmodulecol("""
|
|
|
|
import py
|
|
|
|
def test_ok():
|
|
|
|
pass
|
|
|
|
def test_skip():
|
|
|
|
py.test.skip("xx")
|
|
|
|
def test_func():
|
|
|
|
assert 0
|
|
|
|
""", configargs=("-v",))
|
2009-03-18 07:48:07 +08:00
|
|
|
rep = TerminalReporter(modcol.config, file=linecomp.stringio)
|
2009-04-09 22:03:09 +08:00
|
|
|
rep.config.pluginmanager.register(rep)
|
2009-04-09 07:41:35 +08:00
|
|
|
rep.config.api.pytest_testrunstart()
|
2009-02-27 18:18:27 +08:00
|
|
|
items = modcol.collect()
|
2009-03-21 10:04:44 +08:00
|
|
|
rep.config.option.debug = True #
|
2009-02-27 18:18:27 +08:00
|
|
|
for item in items:
|
2009-04-09 07:33:48 +08:00
|
|
|
rep.config.api.pytest_itemstart(item=item, node=None)
|
2009-02-27 18:18:27 +08:00
|
|
|
s = linecomp.stringio.getvalue().strip()
|
|
|
|
assert s.endswith(item.name)
|
2009-04-09 07:33:48 +08:00
|
|
|
rep.config.api.pytest_itemtestreport(rep=runner.basic_run_report(item))
|
2009-02-27 18:18:27 +08:00
|
|
|
|
|
|
|
linecomp.assert_contains_lines([
|
2009-03-25 19:50:57 +08:00
|
|
|
"*test_pass_skip_fail_verbose.py:2: *test_ok*PASS*",
|
|
|
|
"*test_pass_skip_fail_verbose.py:4: *test_skip*SKIP*",
|
|
|
|
"*test_pass_skip_fail_verbose.py:6: *test_func*FAIL*",
|
2009-02-27 18:18:27 +08:00
|
|
|
])
|
2009-04-09 07:41:35 +08:00
|
|
|
rep.config.api.pytest_testrunfinish(exitstatus=1)
|
2009-02-27 18:18:27 +08:00
|
|
|
linecomp.assert_contains_lines([
|
|
|
|
" def test_func():",
|
|
|
|
"> assert 0",
|
|
|
|
"E assert 0",
|
|
|
|
])
|
|
|
|
|
|
|
|
def test_collect_fail(self, testdir, linecomp):
|
|
|
|
modcol = testdir.getmodulecol("import xyz")
|
2009-03-18 07:48:07 +08:00
|
|
|
rep = TerminalReporter(modcol.config, file=linecomp.stringio)
|
2009-04-09 22:03:09 +08:00
|
|
|
rep.config.pluginmanager.register(rep)
|
2009-04-09 07:41:35 +08:00
|
|
|
rep.config.api.pytest_testrunstart()
|
2009-02-27 18:18:27 +08:00
|
|
|
l = list(testdir.genitems([modcol]))
|
|
|
|
assert len(l) == 0
|
|
|
|
linecomp.assert_contains_lines([
|
|
|
|
"*test_collect_fail.py F*"
|
|
|
|
])
|
2009-04-09 07:41:35 +08:00
|
|
|
rep.config.api.pytest_testrunfinish(exitstatus=1)
|
2009-02-27 18:18:27 +08:00
|
|
|
linecomp.assert_contains_lines([
|
|
|
|
"> import xyz",
|
|
|
|
"E ImportError: No module named xyz"
|
|
|
|
])
|
|
|
|
|
|
|
|
def test_internalerror(self, testdir, linecomp):
|
|
|
|
modcol = testdir.getmodulecol("def test_one(): pass")
|
2009-03-18 07:48:07 +08:00
|
|
|
rep = TerminalReporter(modcol.config, file=linecomp.stringio)
|
2009-02-27 18:18:27 +08:00
|
|
|
excinfo = py.test.raises(ValueError, "raise ValueError('hello')")
|
2009-04-09 08:12:10 +08:00
|
|
|
rep.pytest_internalerror(excinfo.getrepr())
|
2009-02-27 18:18:27 +08:00
|
|
|
linecomp.assert_contains_lines([
|
2009-04-03 22:18:47 +08:00
|
|
|
"INTERNALERROR> *raise ValueError*"
|
2009-02-27 18:18:27 +08:00
|
|
|
])
|
|
|
|
|
2009-03-20 08:34:59 +08:00
|
|
|
def test_gwmanage_events(self, testdir, linecomp):
|
2009-02-27 18:18:27 +08:00
|
|
|
modcol = testdir.getmodulecol("""
|
|
|
|
def test_one():
|
|
|
|
pass
|
|
|
|
""", configargs=("-v",))
|
2009-03-20 08:34:59 +08:00
|
|
|
|
2009-03-18 07:48:07 +08:00
|
|
|
rep = TerminalReporter(modcol.config, file=linecomp.stringio)
|
2009-03-20 08:34:59 +08:00
|
|
|
class gw1:
|
|
|
|
id = "X1"
|
2009-03-21 03:04:36 +08:00
|
|
|
spec = py.execnet.XSpec("popen")
|
2009-03-20 08:34:59 +08:00
|
|
|
class gw2:
|
|
|
|
id = "X2"
|
2009-03-21 03:04:36 +08:00
|
|
|
spec = py.execnet.XSpec("popen")
|
2009-03-21 09:31:27 +08:00
|
|
|
class rinfo:
|
|
|
|
version_info = (2, 5, 1, 'final', 0)
|
|
|
|
executable = "hello"
|
|
|
|
platform = "xyz"
|
|
|
|
cwd = "qwe"
|
2009-04-09 01:50:14 +08:00
|
|
|
|
|
|
|
rep.pyexecnet_gwmanage_newgateway(gw1, rinfo)
|
2009-03-20 08:34:59 +08:00
|
|
|
linecomp.assert_contains_lines([
|
2009-03-22 03:58:41 +08:00
|
|
|
"X1*popen*xyz*2.5*"
|
2009-03-20 08:34:59 +08:00
|
|
|
])
|
|
|
|
|
2009-04-09 01:50:14 +08:00
|
|
|
rep.pyexecnet_gwmanage_rsyncstart(source="hello", gateways=[gw1, gw2])
|
2009-02-27 18:18:27 +08:00
|
|
|
linecomp.assert_contains_lines([
|
2009-03-20 08:34:59 +08:00
|
|
|
"rsyncstart: hello -> X1, X2"
|
2009-02-27 18:18:27 +08:00
|
|
|
])
|
2009-04-09 01:50:14 +08:00
|
|
|
rep.pyexecnet_gwmanage_rsyncfinish(source="hello", gateways=[gw1, gw2])
|
2009-02-27 18:18:27 +08:00
|
|
|
linecomp.assert_contains_lines([
|
2009-03-20 08:34:59 +08:00
|
|
|
"rsyncfinish: hello -> X1, X2"
|
2009-02-27 18:18:27 +08:00
|
|
|
])
|
|
|
|
|
|
|
|
def test_writeline(self, testdir, linecomp):
|
|
|
|
modcol = testdir.getmodulecol("def test_one(): pass")
|
|
|
|
stringio = py.std.cStringIO.StringIO()
|
2009-03-18 07:48:07 +08:00
|
|
|
rep = TerminalReporter(modcol.config, file=linecomp.stringio)
|
2009-02-27 18:18:27 +08:00
|
|
|
rep.write_fspath_result(py.path.local("xy.py"), '.')
|
|
|
|
rep.write_line("hello world")
|
|
|
|
lines = linecomp.stringio.getvalue().split('\n')
|
|
|
|
assert not lines[0]
|
|
|
|
assert lines[1].endswith("xy.py .")
|
|
|
|
assert lines[2] == "hello world"
|
|
|
|
|
2009-03-22 04:07:45 +08:00
|
|
|
def test_looponfailreport(self, testdir, linecomp):
|
2009-02-27 18:18:27 +08:00
|
|
|
modcol = testdir.getmodulecol("""
|
|
|
|
def test_fail():
|
|
|
|
assert 0
|
|
|
|
def test_fail2():
|
|
|
|
raise ValueError()
|
|
|
|
""")
|
2009-03-18 07:48:07 +08:00
|
|
|
rep = TerminalReporter(modcol.config, file=linecomp.stringio)
|
2009-04-04 08:34:20 +08:00
|
|
|
reports = [runner.basic_run_report(x) for x in modcol.collect()]
|
2009-04-09 07:50:02 +08:00
|
|
|
rep.pytest_looponfailinfo(reports, [modcol.config.topdir])
|
2009-02-27 18:18:27 +08:00
|
|
|
linecomp.assert_contains_lines([
|
2009-03-22 04:07:45 +08:00
|
|
|
"*test_looponfailreport.py:2: assert 0",
|
|
|
|
"*test_looponfailreport.py:4: ValueError*",
|
2009-02-27 18:18:27 +08:00
|
|
|
"*waiting*",
|
2009-03-18 07:48:07 +08:00
|
|
|
"*%s*" % (modcol.config.topdir),
|
2009-02-27 18:18:27 +08:00
|
|
|
])
|
|
|
|
|
|
|
|
def test_tb_option(self, testdir, linecomp):
|
2009-04-10 02:07:05 +08:00
|
|
|
# XXX usage of testdir
|
2009-02-27 18:18:27 +08:00
|
|
|
for tbopt in ["long", "short", "no"]:
|
|
|
|
print 'testing --tb=%s...' % tbopt
|
|
|
|
modcol = testdir.getmodulecol("""
|
|
|
|
import py
|
|
|
|
def g():
|
|
|
|
raise IndexError
|
|
|
|
def test_func():
|
|
|
|
print 6*7
|
|
|
|
g() # --calling--
|
|
|
|
""", configargs=("--tb=%s" % tbopt,))
|
2009-03-18 07:48:07 +08:00
|
|
|
rep = TerminalReporter(modcol.config, file=linecomp.stringio)
|
2009-04-09 22:03:09 +08:00
|
|
|
rep.config.pluginmanager.register(rep)
|
2009-04-09 07:41:35 +08:00
|
|
|
rep.config.api.pytest_testrunstart()
|
2009-02-27 18:18:27 +08:00
|
|
|
for item in testdir.genitems([modcol]):
|
2009-04-09 07:33:48 +08:00
|
|
|
rep.config.api.pytest_itemtestreport(
|
|
|
|
rep=runner.basic_run_report(item))
|
2009-04-09 07:41:35 +08:00
|
|
|
rep.config.api.pytest_testrunfinish(exitstatus=1)
|
2009-02-27 18:18:27 +08:00
|
|
|
s = linecomp.stringio.getvalue()
|
|
|
|
if tbopt == "long":
|
|
|
|
print s
|
|
|
|
assert 'print 6*7' in s
|
|
|
|
else:
|
|
|
|
assert 'print 6*7' not in s
|
|
|
|
if tbopt != "no":
|
|
|
|
assert '--calling--' in s
|
|
|
|
assert 'IndexError' in s
|
|
|
|
else:
|
|
|
|
assert 'FAILURES' not in s
|
|
|
|
assert '--calling--' not in s
|
|
|
|
assert 'IndexError' not in s
|
|
|
|
linecomp.stringio.truncate(0)
|
|
|
|
|
|
|
|
def test_show_path_before_running_test(self, testdir, linecomp):
|
|
|
|
modcol = testdir.getmodulecol("""
|
|
|
|
def test_foobar():
|
|
|
|
pass
|
|
|
|
""")
|
2009-03-18 07:48:07 +08:00
|
|
|
rep = TerminalReporter(modcol.config, file=linecomp.stringio)
|
2009-04-09 22:03:09 +08:00
|
|
|
modcol.config.pluginmanager.register(rep)
|
2009-02-27 18:18:27 +08:00
|
|
|
l = list(testdir.genitems([modcol]))
|
|
|
|
assert len(l) == 1
|
2009-03-21 10:04:44 +08:00
|
|
|
modcol.config.option.debug = True
|
2009-04-09 07:33:48 +08:00
|
|
|
rep.config.api.pytest_itemstart(item=l[0])
|
2009-02-27 18:18:27 +08:00
|
|
|
linecomp.assert_contains_lines([
|
|
|
|
"*test_show_path_before_running_test.py*"
|
|
|
|
])
|
|
|
|
|
|
|
|
def pseudo_keyboard_interrupt(self, testdir, linecomp, verbose=False):
|
|
|
|
modcol = testdir.getmodulecol("""
|
|
|
|
def test_foobar():
|
|
|
|
assert 0
|
|
|
|
def test_spamegg():
|
|
|
|
import py; py.test.skip('skip me please!')
|
|
|
|
def test_interrupt_me():
|
|
|
|
raise KeyboardInterrupt # simulating the user
|
2009-03-22 04:20:02 +08:00
|
|
|
""", configargs=("-v",)*verbose)
|
|
|
|
#""", configargs=("--showskipsummary",) + ("-v",)*verbose)
|
2009-03-18 07:48:07 +08:00
|
|
|
rep = TerminalReporter(modcol.config, file=linecomp.stringio)
|
2009-04-09 22:03:09 +08:00
|
|
|
modcol.config.pluginmanager.register(rep)
|
2009-04-09 07:41:35 +08:00
|
|
|
modcol.config.api.pytest_testrunstart()
|
2009-02-27 18:18:27 +08:00
|
|
|
try:
|
|
|
|
for item in testdir.genitems([modcol]):
|
2009-04-09 07:33:48 +08:00
|
|
|
modcol.config.api.pytest_itemtestreport(
|
|
|
|
rep=runner.basic_run_report(item))
|
2009-02-27 18:18:27 +08:00
|
|
|
except KeyboardInterrupt:
|
|
|
|
excinfo = py.code.ExceptionInfo()
|
|
|
|
else:
|
|
|
|
py.test.fail("no KeyboardInterrupt??")
|
|
|
|
s = linecomp.stringio.getvalue()
|
|
|
|
if not verbose:
|
|
|
|
assert s.find("_keyboard_interrupt.py Fs") != -1
|
2009-04-09 07:41:35 +08:00
|
|
|
modcol.config.api.pytest_testrunfinish(exitstatus=2, excrepr=excinfo.getrepr())
|
2009-02-27 18:18:27 +08:00
|
|
|
text = linecomp.stringio.getvalue()
|
|
|
|
linecomp.assert_contains_lines([
|
|
|
|
" def test_foobar():",
|
|
|
|
"> assert 0",
|
|
|
|
"E assert 0",
|
|
|
|
])
|
2009-03-22 04:20:02 +08:00
|
|
|
#assert "Skipped: 'skip me please!'" in text
|
2009-02-27 18:18:27 +08:00
|
|
|
assert "_keyboard_interrupt.py:6: KeyboardInterrupt" in text
|
|
|
|
see_details = "raise KeyboardInterrupt # simulating the user" in text
|
|
|
|
assert see_details == verbose
|
|
|
|
|
|
|
|
def test_keyboard_interrupt(self, testdir, linecomp):
|
|
|
|
self.pseudo_keyboard_interrupt(testdir, linecomp)
|
|
|
|
|
|
|
|
def test_verbose_keyboard_interrupt(self, testdir, linecomp):
|
|
|
|
self.pseudo_keyboard_interrupt(testdir, linecomp, verbose=True)
|
|
|
|
|
|
|
|
def test_skip_reasons_folding(self):
|
|
|
|
class longrepr:
|
|
|
|
class reprcrash:
|
|
|
|
path = 'xyz'
|
|
|
|
lineno = 3
|
|
|
|
message = "justso"
|
|
|
|
|
2009-04-07 19:51:55 +08:00
|
|
|
ev1 = runner.CollectReport(None, None)
|
2009-02-27 18:18:27 +08:00
|
|
|
ev1.when = "execute"
|
|
|
|
ev1.skipped = True
|
|
|
|
ev1.longrepr = longrepr
|
|
|
|
|
2009-04-04 08:34:20 +08:00
|
|
|
ev2 = runner.ItemTestReport(None, excinfo=longrepr)
|
2009-02-27 18:18:27 +08:00
|
|
|
ev2.skipped = True
|
|
|
|
|
|
|
|
l = folded_skips([ev1, ev2])
|
|
|
|
assert len(l) == 1
|
|
|
|
num, fspath, lineno, reason = l[0]
|
|
|
|
assert num == 2
|
|
|
|
assert fspath == longrepr.reprcrash.path
|
|
|
|
assert lineno == longrepr.reprcrash.lineno
|
|
|
|
assert reason == longrepr.reprcrash.message
|
|
|
|
|
|
|
|
class TestCollectonly:
|
|
|
|
def test_collectonly_basic(self, testdir, linecomp):
|
|
|
|
modcol = testdir.getmodulecol(configargs=['--collectonly'], source="""
|
|
|
|
def test_func():
|
|
|
|
pass
|
|
|
|
""")
|
2009-03-18 07:48:07 +08:00
|
|
|
rep = CollectonlyReporter(modcol.config, out=linecomp.stringio)
|
2009-04-09 22:03:09 +08:00
|
|
|
modcol.config.pluginmanager.register(rep)
|
2009-02-27 18:18:27 +08:00
|
|
|
indent = rep.indent
|
2009-04-09 07:33:48 +08:00
|
|
|
rep.config.api.pytest_collectstart(collector=modcol)
|
2009-02-27 18:18:27 +08:00
|
|
|
linecomp.assert_contains_lines([
|
|
|
|
"<Module 'test_collectonly_basic.py'>"
|
|
|
|
])
|
|
|
|
item = modcol.join("test_func")
|
2009-04-09 07:33:48 +08:00
|
|
|
rep.config.api.pytest_itemstart(item=item)
|
2009-02-27 18:18:27 +08:00
|
|
|
linecomp.assert_contains_lines([
|
|
|
|
" <Function 'test_func'>",
|
|
|
|
])
|
2009-04-09 07:33:48 +08:00
|
|
|
rep.config.api.pytest_collectreport(
|
|
|
|
rep=runner.CollectReport(modcol, [], excinfo=None))
|
2009-02-27 18:18:27 +08:00
|
|
|
assert rep.indent == indent
|
|
|
|
|
|
|
|
def test_collectonly_skipped_module(self, testdir, linecomp):
|
|
|
|
modcol = testdir.getmodulecol(configargs=['--collectonly'], source="""
|
|
|
|
import py
|
|
|
|
py.test.skip("nomod")
|
|
|
|
""")
|
2009-03-18 07:48:07 +08:00
|
|
|
rep = CollectonlyReporter(modcol.config, out=linecomp.stringio)
|
2009-04-09 22:03:09 +08:00
|
|
|
modcol.config.pluginmanager.register(rep)
|
2009-02-27 18:18:27 +08:00
|
|
|
cols = list(testdir.genitems([modcol]))
|
|
|
|
assert len(cols) == 0
|
|
|
|
linecomp.assert_contains_lines("""
|
|
|
|
<Module 'test_collectonly_skipped_module.py'>
|
|
|
|
!!! Skipped: 'nomod' !!!
|
|
|
|
""")
|
|
|
|
|
|
|
|
def test_collectonly_failed_module(self, testdir, linecomp):
|
|
|
|
modcol = testdir.getmodulecol(configargs=['--collectonly'], source="""
|
|
|
|
raise ValueError(0)
|
|
|
|
""")
|
2009-03-18 07:48:07 +08:00
|
|
|
rep = CollectonlyReporter(modcol.config, out=linecomp.stringio)
|
2009-04-09 22:03:09 +08:00
|
|
|
modcol.config.pluginmanager.register(rep)
|
2009-02-27 18:18:27 +08:00
|
|
|
cols = list(testdir.genitems([modcol]))
|
|
|
|
assert len(cols) == 0
|
|
|
|
linecomp.assert_contains_lines("""
|
|
|
|
<Module 'test_collectonly_failed_module.py'>
|
|
|
|
!!! ValueError: 0 !!!
|
|
|
|
""")
|
|
|
|
|
2009-04-04 01:45:25 +08:00
|
|
|
def test_repr_python_version(monkeypatch):
|
|
|
|
monkeypatch.setattr(sys, 'version_info', (2, 5, 1, 'final', 0))
|
|
|
|
assert repr_pythonversion() == "2.5.1-final-0"
|
|
|
|
py.std.sys.version_info = x = (2,3)
|
|
|
|
assert repr_pythonversion() == str(x)
|
2009-02-27 18:18:27 +08:00
|
|
|
|
|
|
|
def test_generic(plugintester):
|
|
|
|
plugintester.apicheck(TerminalPlugin)
|
|
|
|
plugintester.apicheck(TerminalReporter)
|
|
|
|
plugintester.apicheck(CollectonlyReporter)
|