2010-11-06 18:38:53 +08:00
|
|
|
""" basic collect and runtest protocol implementations """
|
2017-03-17 09:21:30 +08:00
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
|
2014-08-01 06:13:40 +08:00
|
|
|
import bdb
|
2017-07-19 04:18:34 +08:00
|
|
|
import os
|
2014-08-01 06:13:40 +08:00
|
|
|
import sys
|
|
|
|
from time import time
|
2008-08-16 23:26:59 +08:00
|
|
|
|
2014-01-18 19:31:33 +08:00
|
|
|
import py
|
2015-11-27 22:43:01 +08:00
|
|
|
from _pytest._code.code import TerminalRepr, ExceptionInfo
|
2015-09-19 07:03:05 +08:00
|
|
|
from _pytest.outcomes import skip, Skipped, TEST_OUTCOME
|
2010-04-28 03:13:09 +08:00
|
|
|
|
2009-06-09 00:31:10 +08:00
|
|
|
#
|
2010-07-27 03:15:15 +08:00
|
|
|
# pytest plugin hooks
|
2009-06-09 00:31:10 +08:00
|
|
|
|
2017-07-29 00:27:59 +08:00
|
|
|
|
2011-11-09 01:20:56 +08:00
|
|
|
def pytest_addoption(parser):
|
|
|
|
group = parser.getgroup("terminal reporting", "reporting", after="general")
|
|
|
|
group.addoption('--durations',
|
2017-07-17 07:25:07 +08:00
|
|
|
action="store", type=int, default=None, metavar="N",
|
|
|
|
help="show N slowest setup/test durations (N=0 for all)."),
|
2011-11-09 01:20:56 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2011-11-09 01:20:56 +08:00
|
|
|
def pytest_terminal_summary(terminalreporter):
|
|
|
|
durations = terminalreporter.config.option.durations
|
|
|
|
if durations is None:
|
|
|
|
return
|
|
|
|
tr = terminalreporter
|
2011-11-19 01:35:23 +08:00
|
|
|
dlist = []
|
|
|
|
for replist in tr.stats.values():
|
2011-11-09 01:20:56 +08:00
|
|
|
for rep in replist:
|
2011-11-09 03:00:25 +08:00
|
|
|
if hasattr(rep, 'duration'):
|
2011-11-19 01:59:52 +08:00
|
|
|
dlist.append(rep)
|
2011-11-19 01:35:23 +08:00
|
|
|
if not dlist:
|
2011-11-09 01:20:56 +08:00
|
|
|
return
|
2011-11-19 01:59:52 +08:00
|
|
|
dlist.sort(key=lambda x: x.duration)
|
2011-11-19 01:35:23 +08:00
|
|
|
dlist.reverse()
|
2011-11-09 01:20:56 +08:00
|
|
|
if not durations:
|
|
|
|
tr.write_sep("=", "slowest test durations")
|
|
|
|
else:
|
|
|
|
tr.write_sep("=", "slowest %s test durations" % durations)
|
2011-11-19 01:35:23 +08:00
|
|
|
dlist = dlist[:durations]
|
2011-11-09 01:20:56 +08:00
|
|
|
|
2011-11-19 01:59:52 +08:00
|
|
|
for rep in dlist:
|
2011-11-09 01:20:56 +08:00
|
|
|
nodeid = rep.nodeid.replace("::()::", "::")
|
2011-11-18 22:59:39 +08:00
|
|
|
tr.write_line("%02.2fs %-8s %s" %
|
2017-07-17 07:25:07 +08:00
|
|
|
(rep.duration, rep.when, nodeid))
|
2011-11-09 01:20:56 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2011-05-27 06:08:56 +08:00
|
|
|
def pytest_sessionstart(session):
|
|
|
|
session._setupstate = SetupState()
|
2017-07-17 07:25:09 +08:00
|
|
|
|
|
|
|
|
2012-02-01 21:52:34 +08:00
|
|
|
def pytest_sessionfinish(session):
|
|
|
|
session._setupstate.teardown_all()
|
2009-06-09 00:31:10 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2017-05-18 05:16:11 +08:00
|
|
|
class NodeInfo:
|
2010-11-06 16:58:04 +08:00
|
|
|
def __init__(self, location):
|
2010-09-26 22:23:45 +08:00
|
|
|
self.location = location
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2011-12-03 05:00:19 +08:00
|
|
|
def pytest_runtest_protocol(item, nextitem):
|
2010-09-26 22:23:45 +08:00
|
|
|
item.ihook.pytest_runtest_logstart(
|
2010-11-06 16:58:04 +08:00
|
|
|
nodeid=item.nodeid, location=item.location,
|
2010-09-26 22:23:45 +08:00
|
|
|
)
|
2011-12-03 05:00:19 +08:00
|
|
|
runtestprotocol(item, nextitem=nextitem)
|
2009-06-09 00:31:10 +08:00
|
|
|
return True
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2011-12-03 05:00:19 +08:00
|
|
|
def runtestprotocol(item, log=True, nextitem=None):
|
2013-04-22 16:35:48 +08:00
|
|
|
hasrequest = hasattr(item, "_request")
|
|
|
|
if hasrequest and not item._request:
|
|
|
|
item._initrequest()
|
2009-06-09 22:08:34 +08:00
|
|
|
rep = call_and_report(item, "setup", log)
|
|
|
|
reports = [rep]
|
|
|
|
if rep.passed:
|
2016-07-04 04:30:51 +08:00
|
|
|
if item.config.option.setupshow:
|
2016-06-22 17:09:54 +08:00
|
|
|
show_test_item(item)
|
2016-07-04 04:30:51 +08:00
|
|
|
if not item.config.option.setuponly:
|
2016-06-22 00:28:40 +08:00
|
|
|
reports.append(call_and_report(item, "call", log))
|
2011-12-03 05:00:19 +08:00
|
|
|
reports.append(call_and_report(item, "teardown", log,
|
2017-07-17 07:25:07 +08:00
|
|
|
nextitem=nextitem))
|
2013-04-22 16:35:48 +08:00
|
|
|
# after all teardown hooks have been called
|
|
|
|
# want funcargs and request info to go away
|
|
|
|
if hasrequest:
|
|
|
|
item._request = False
|
|
|
|
item.funcargs = None
|
2009-06-09 22:08:34 +08:00
|
|
|
return reports
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2016-06-22 17:09:54 +08:00
|
|
|
def show_test_item(item):
|
|
|
|
"""Show test function, parameters and the fixtures of the test item."""
|
|
|
|
tw = item.config.get_terminal_writer()
|
|
|
|
tw.line()
|
|
|
|
tw.write(' ' * 8)
|
2016-06-23 16:23:04 +08:00
|
|
|
tw.write(item._nodeid)
|
2016-06-22 17:09:54 +08:00
|
|
|
used_fixtures = sorted(item._fixtureinfo.name2fixturedefs.keys())
|
|
|
|
if used_fixtures:
|
2016-06-22 20:53:37 +08:00
|
|
|
tw.write(' (fixtures used: {0})'.format(', '.join(used_fixtures)))
|
2016-06-22 17:09:54 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2009-06-09 00:31:10 +08:00
|
|
|
def pytest_runtest_setup(item):
|
2017-07-19 04:18:34 +08:00
|
|
|
_update_current_test_var(item, 'setup')
|
2011-05-27 06:08:56 +08:00
|
|
|
item.session._setupstate.prepare(item)
|
2009-06-09 00:31:10 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2009-06-09 00:31:10 +08:00
|
|
|
def pytest_runtest_call(item):
|
2017-07-19 04:18:34 +08:00
|
|
|
_update_current_test_var(item, 'call')
|
2015-03-21 16:26:35 +08:00
|
|
|
try:
|
|
|
|
item.runtest()
|
|
|
|
except Exception:
|
|
|
|
# Store trace info to allow postmortem debugging
|
|
|
|
type, value, tb = sys.exc_info()
|
|
|
|
tb = tb.tb_next # Skip *this* frame
|
|
|
|
sys.last_type = type
|
|
|
|
sys.last_value = value
|
|
|
|
sys.last_traceback = tb
|
|
|
|
del tb # Get rid of it in this namespace
|
|
|
|
raise
|
2009-06-09 00:31:10 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2011-12-03 05:00:19 +08:00
|
|
|
def pytest_runtest_teardown(item, nextitem):
|
2017-07-19 04:18:34 +08:00
|
|
|
_update_current_test_var(item, 'teardown')
|
2011-12-03 05:00:19 +08:00
|
|
|
item.session._setupstate.teardown_exact(item, nextitem)
|
2017-07-19 04:18:34 +08:00
|
|
|
_update_current_test_var(item, None)
|
|
|
|
|
|
|
|
|
|
|
|
def _update_current_test_var(item, when):
|
|
|
|
"""
|
|
|
|
Update PYTEST_CURRENT_TEST to reflect the current item and stage.
|
|
|
|
|
|
|
|
If ``when`` is None, delete PYTEST_CURRENT_TEST from the environment.
|
|
|
|
"""
|
|
|
|
var_name = 'PYTEST_CURRENT_TEST'
|
|
|
|
if when:
|
|
|
|
os.environ[var_name] = '{0} ({1})'.format(item.nodeid, when)
|
|
|
|
else:
|
|
|
|
os.environ.pop(var_name)
|
2009-07-31 20:22:02 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2009-08-06 21:02:38 +08:00
|
|
|
def pytest_report_teststatus(report):
|
|
|
|
if report.when in ("setup", "teardown"):
|
|
|
|
if report.failed:
|
2010-07-27 03:15:15 +08:00
|
|
|
# category, shortletter, verbose-word
|
2009-07-31 20:22:02 +08:00
|
|
|
return "error", "E", "ERROR"
|
2009-08-06 21:02:38 +08:00
|
|
|
elif report.skipped:
|
2009-07-31 20:22:02 +08:00
|
|
|
return "skipped", "s", "SKIPPED"
|
|
|
|
else:
|
|
|
|
return "", "", ""
|
2010-09-26 22:23:44 +08:00
|
|
|
|
|
|
|
|
2009-06-09 00:31:10 +08:00
|
|
|
#
|
|
|
|
# Implementation
|
|
|
|
|
2011-12-03 05:00:19 +08:00
|
|
|
def call_and_report(item, when, log=True, **kwds):
|
|
|
|
call = call_runtest_hook(item, when, **kwds)
|
2009-12-30 09:36:58 +08:00
|
|
|
hook = item.ihook
|
2009-06-09 22:08:34 +08:00
|
|
|
report = hook.pytest_runtest_makereport(item=item, call=call)
|
2011-11-09 01:53:46 +08:00
|
|
|
if log:
|
2010-07-27 03:15:15 +08:00
|
|
|
hook.pytest_runtest_logreport(report=report)
|
2013-09-06 17:56:04 +08:00
|
|
|
if check_interactive_exception(call, report):
|
|
|
|
hook.pytest_exception_interact(node=item, call=call, report=report)
|
2009-06-09 22:08:34 +08:00
|
|
|
return report
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2013-09-06 17:56:04 +08:00
|
|
|
def check_interactive_exception(call, report):
|
|
|
|
return call.excinfo and not (
|
2017-07-17 07:25:07 +08:00
|
|
|
hasattr(report, "wasxfail") or
|
|
|
|
call.excinfo.errisinstance(skip.Exception) or
|
|
|
|
call.excinfo.errisinstance(bdb.BdbQuit))
|
2013-09-06 17:56:04 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2011-12-03 05:00:19 +08:00
|
|
|
def call_runtest_hook(item, when, **kwds):
|
2010-07-27 03:15:15 +08:00
|
|
|
hookname = "pytest_runtest_" + when
|
2009-12-30 09:36:58 +08:00
|
|
|
ihook = getattr(item.ihook, hookname)
|
2011-12-03 05:00:19 +08:00
|
|
|
return CallInfo(lambda: ihook(item=item, **kwds), when=when)
|
2009-07-31 20:22:02 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2017-05-18 05:16:11 +08:00
|
|
|
class CallInfo:
|
2010-11-06 18:38:53 +08:00
|
|
|
""" Result/Exception info a function invocation. """
|
2010-10-11 18:54:28 +08:00
|
|
|
#: None or ExceptionInfo object.
|
2010-07-27 03:15:15 +08:00
|
|
|
excinfo = None
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2009-07-31 20:22:02 +08:00
|
|
|
def __init__(self, func, when):
|
2010-11-06 18:38:53 +08:00
|
|
|
#: context of invocation: one of "setup", "call",
|
|
|
|
#: "teardown", "memocollect"
|
2010-07-27 03:15:15 +08:00
|
|
|
self.when = when
|
2012-09-01 15:58:10 +08:00
|
|
|
self.start = time()
|
2009-05-23 01:57:11 +08:00
|
|
|
try:
|
2014-03-14 19:49:36 +08:00
|
|
|
self.result = func()
|
|
|
|
except KeyboardInterrupt:
|
2012-09-01 15:58:10 +08:00
|
|
|
self.stop = time()
|
2014-03-14 19:49:36 +08:00
|
|
|
raise
|
|
|
|
except:
|
2015-11-27 22:43:01 +08:00
|
|
|
self.excinfo = ExceptionInfo()
|
2014-03-14 19:49:36 +08:00
|
|
|
self.stop = time()
|
2009-04-04 04:16:02 +08:00
|
|
|
|
2009-08-06 20:34:19 +08:00
|
|
|
def __repr__(self):
|
|
|
|
if self.excinfo:
|
|
|
|
status = "exception: %s" % str(self.excinfo.value)
|
|
|
|
else:
|
|
|
|
status = "result: %r" % (self.result,)
|
|
|
|
return "<CallInfo when=%r %s>" % (self.when, status)
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2010-11-14 04:03:28 +08:00
|
|
|
def getslaveinfoline(node):
|
|
|
|
try:
|
|
|
|
return node._slaveinfocache
|
|
|
|
except AttributeError:
|
|
|
|
d = node.slaveinfo
|
|
|
|
ver = "%s.%s.%s" % d['version_info'][:3]
|
|
|
|
node._slaveinfocache = s = "[%s] %s -- Python %s %s" % (
|
|
|
|
d['id'], d['sysplatform'], ver, d['executable'])
|
|
|
|
return s
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2009-04-04 08:34:20 +08:00
|
|
|
class BaseReport(object):
|
2012-01-21 02:50:45 +08:00
|
|
|
|
|
|
|
def __init__(self, **kw):
|
|
|
|
self.__dict__.update(kw)
|
|
|
|
|
2009-04-04 08:34:20 +08:00
|
|
|
def toterminal(self, out):
|
2010-11-14 04:03:28 +08:00
|
|
|
if hasattr(self, 'node'):
|
|
|
|
out.line(getslaveinfoline(self.node))
|
2015-09-19 14:36:43 +08:00
|
|
|
|
|
|
|
longrepr = self.longrepr
|
|
|
|
if longrepr is None:
|
|
|
|
return
|
|
|
|
|
2009-04-04 08:34:20 +08:00
|
|
|
if hasattr(longrepr, 'toterminal'):
|
|
|
|
longrepr.toterminal(out)
|
|
|
|
else:
|
2012-06-24 22:42:31 +08:00
|
|
|
try:
|
|
|
|
out.line(longrepr)
|
|
|
|
except UnicodeEncodeError:
|
|
|
|
out.line("<unprintable longrepr>")
|
2010-07-04 23:06:50 +08:00
|
|
|
|
2014-03-14 19:49:34 +08:00
|
|
|
def get_sections(self, prefix):
|
|
|
|
for name, content in self.sections:
|
|
|
|
if name.startswith(prefix):
|
|
|
|
yield prefix, content
|
|
|
|
|
2016-08-04 08:11:19 +08:00
|
|
|
@property
|
|
|
|
def longreprtext(self):
|
|
|
|
"""
|
|
|
|
Read-only property that returns the full string representation
|
|
|
|
of ``longrepr``.
|
|
|
|
|
|
|
|
.. versionadded:: 3.0
|
|
|
|
"""
|
|
|
|
tw = py.io.TerminalWriter(stringio=True)
|
|
|
|
tw.hasmarkup = False
|
|
|
|
self.toterminal(tw)
|
|
|
|
exc = tw.stringio.getvalue()
|
|
|
|
return exc.strip()
|
|
|
|
|
2016-08-04 08:49:43 +08:00
|
|
|
@property
|
|
|
|
def capstdout(self):
|
|
|
|
"""Return captured text from stdout, if capturing is enabled
|
|
|
|
|
|
|
|
.. versionadded:: 3.0
|
|
|
|
"""
|
|
|
|
return ''.join(content for (prefix, content) in self.get_sections('Captured stdout'))
|
|
|
|
|
|
|
|
@property
|
|
|
|
def capstderr(self):
|
|
|
|
"""Return captured text from stderr, if capturing is enabled
|
|
|
|
|
|
|
|
.. versionadded:: 3.0
|
|
|
|
"""
|
|
|
|
return ''.join(content for (prefix, content) in self.get_sections('Captured stderr'))
|
|
|
|
|
2010-09-26 22:23:44 +08:00
|
|
|
passed = property(lambda x: x.outcome == "passed")
|
|
|
|
failed = property(lambda x: x.outcome == "failed")
|
|
|
|
skipped = property(lambda x: x.outcome == "skipped")
|
2010-07-27 03:15:15 +08:00
|
|
|
|
2010-11-06 16:58:04 +08:00
|
|
|
@property
|
|
|
|
def fspath(self):
|
|
|
|
return self.nodeid.split("::")[0]
|
2009-04-04 08:34:20 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2010-09-26 22:23:44 +08:00
|
|
|
def pytest_runtest_makereport(item, call):
|
|
|
|
when = call.when
|
2017-07-17 07:25:08 +08:00
|
|
|
duration = call.stop - call.start
|
2017-07-17 07:25:08 +08:00
|
|
|
keywords = dict([(x, 1) for x in item.keywords])
|
2010-11-23 22:42:23 +08:00
|
|
|
excinfo = call.excinfo
|
2014-03-14 19:49:34 +08:00
|
|
|
sections = []
|
2010-09-26 22:23:44 +08:00
|
|
|
if not call.excinfo:
|
|
|
|
outcome = "passed"
|
|
|
|
longrepr = None
|
|
|
|
else:
|
2015-11-27 22:43:01 +08:00
|
|
|
if not isinstance(excinfo, ExceptionInfo):
|
2010-09-26 22:23:44 +08:00
|
|
|
outcome = "failed"
|
|
|
|
longrepr = excinfo
|
2017-03-16 01:00:59 +08:00
|
|
|
elif excinfo.errisinstance(skip.Exception):
|
2010-09-26 22:23:44 +08:00
|
|
|
outcome = "skipped"
|
2011-03-11 22:43:24 +08:00
|
|
|
r = excinfo._getreprcrash()
|
2010-11-14 04:03:28 +08:00
|
|
|
longrepr = (str(r.path), r.lineno, r.message)
|
2009-04-04 08:34:20 +08:00
|
|
|
else:
|
2010-09-26 22:23:44 +08:00
|
|
|
outcome = "failed"
|
|
|
|
if call.when == "call":
|
|
|
|
longrepr = item.repr_failure(excinfo)
|
2017-07-17 07:25:09 +08:00
|
|
|
else: # exception in setup or teardown
|
2013-05-08 23:01:20 +08:00
|
|
|
longrepr = item._repr_failure_py(excinfo,
|
2017-07-17 07:25:07 +08:00
|
|
|
style=item.config.option.tbstyle)
|
2014-03-14 19:49:34 +08:00
|
|
|
for rwhen, key, content in item._report_sections:
|
2017-07-17 07:25:08 +08:00
|
|
|
sections.append(("Captured %s %s" % (key, rwhen), content))
|
2010-11-06 16:58:04 +08:00
|
|
|
return TestReport(item.nodeid, item.location,
|
2011-09-23 16:53:03 +08:00
|
|
|
keywords, outcome, longrepr, when,
|
2014-03-14 19:49:34 +08:00
|
|
|
sections, duration)
|
2010-09-26 22:23:44 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2010-09-26 22:23:44 +08:00
|
|
|
class TestReport(BaseReport):
|
2010-10-11 18:54:28 +08:00
|
|
|
""" Basic test report object (also used for setup and teardown calls if
|
|
|
|
they fail).
|
|
|
|
"""
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2014-03-14 19:49:34 +08:00
|
|
|
def __init__(self, nodeid, location, keywords, outcome,
|
|
|
|
longrepr, when, sections=(), duration=0, **extra):
|
2010-10-11 18:54:28 +08:00
|
|
|
#: normalized collection node id
|
2010-09-26 22:23:44 +08:00
|
|
|
self.nodeid = nodeid
|
2010-10-11 18:54:28 +08:00
|
|
|
|
|
|
|
#: a (filesystempath, lineno, domaininfo) tuple indicating the
|
|
|
|
#: actual location of a test item - it might be different from the
|
|
|
|
#: collected one e.g. if a method is inherited from a different module.
|
2010-09-26 22:23:44 +08:00
|
|
|
self.location = location
|
2010-10-11 18:54:28 +08:00
|
|
|
|
|
|
|
#: a name -> value dictionary containing all keywords and
|
|
|
|
#: markers associated with a test invocation.
|
2010-09-26 22:23:44 +08:00
|
|
|
self.keywords = keywords
|
2011-11-09 01:20:56 +08:00
|
|
|
|
2010-10-11 18:54:28 +08:00
|
|
|
#: test outcome, always one of "passed", "failed", "skipped".
|
2010-09-26 22:23:44 +08:00
|
|
|
self.outcome = outcome
|
2010-10-11 18:54:28 +08:00
|
|
|
|
|
|
|
#: None or a failure representation.
|
2010-09-26 22:23:44 +08:00
|
|
|
self.longrepr = longrepr
|
2011-11-09 01:20:56 +08:00
|
|
|
|
2010-10-11 18:54:28 +08:00
|
|
|
#: one of 'setup', 'call', 'teardown' to indicate runtest phase.
|
2010-09-26 22:23:44 +08:00
|
|
|
self.when = when
|
2009-05-23 01:56:05 +08:00
|
|
|
|
2016-08-04 08:57:58 +08:00
|
|
|
#: list of pairs ``(str, str)`` of extra information which needs to
|
|
|
|
#: marshallable. Used by pytest to add captured text
|
|
|
|
#: from ``stdout`` and ``stderr``, but may be used by other plugins
|
|
|
|
#: to add arbitrary information to reports.
|
2011-07-13 05:09:03 +08:00
|
|
|
self.sections = list(sections)
|
|
|
|
|
2011-09-23 16:53:03 +08:00
|
|
|
#: time it took to run just the test
|
|
|
|
self.duration = duration
|
|
|
|
|
2012-01-21 02:50:45 +08:00
|
|
|
self.__dict__.update(extra)
|
|
|
|
|
2009-07-31 20:22:02 +08:00
|
|
|
def __repr__(self):
|
2010-09-26 22:23:44 +08:00
|
|
|
return "<TestReport %r when=%r outcome=%r>" % (
|
|
|
|
self.nodeid, self.when, self.outcome)
|
2009-04-05 03:06:20 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2009-07-31 20:22:02 +08:00
|
|
|
class TeardownErrorReport(BaseReport):
|
2010-09-26 22:23:44 +08:00
|
|
|
outcome = "failed"
|
2009-07-31 20:22:02 +08:00
|
|
|
when = "teardown"
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2012-01-21 02:50:45 +08:00
|
|
|
def __init__(self, longrepr, **extra):
|
2010-09-28 18:59:48 +08:00
|
|
|
self.longrepr = longrepr
|
2011-07-13 05:09:03 +08:00
|
|
|
self.sections = []
|
2012-01-21 02:50:45 +08:00
|
|
|
self.__dict__.update(extra)
|
2009-07-31 20:22:02 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2010-09-26 22:23:44 +08:00
|
|
|
def pytest_make_collect_report(collector):
|
2016-08-27 19:04:00 +08:00
|
|
|
call = CallInfo(
|
|
|
|
lambda: list(collector.collect()),
|
|
|
|
'collect')
|
2010-11-14 04:03:28 +08:00
|
|
|
longrepr = None
|
2010-11-06 16:58:04 +08:00
|
|
|
if not call.excinfo:
|
2010-09-26 22:23:44 +08:00
|
|
|
outcome = "passed"
|
|
|
|
else:
|
2014-03-14 21:04:54 +08:00
|
|
|
from _pytest import nose
|
|
|
|
skip_exceptions = (Skipped,) + nose.get_skip_exceptions()
|
|
|
|
if call.excinfo.errisinstance(skip_exceptions):
|
2010-09-26 22:23:44 +08:00
|
|
|
outcome = "skipped"
|
2010-11-14 04:03:28 +08:00
|
|
|
r = collector._repr_failure_py(call.excinfo, "line").reprcrash
|
|
|
|
longrepr = (str(r.path), r.lineno, r.message)
|
2010-09-26 22:23:44 +08:00
|
|
|
else:
|
|
|
|
outcome = "failed"
|
2010-11-06 16:58:04 +08:00
|
|
|
errorinfo = collector.repr_failure(call.excinfo)
|
2010-09-26 22:23:44 +08:00
|
|
|
if not hasattr(errorinfo, "toterminal"):
|
|
|
|
errorinfo = CollectErrorRepr(errorinfo)
|
|
|
|
longrepr = errorinfo
|
2013-09-06 17:56:04 +08:00
|
|
|
rep = CollectReport(collector.nodeid, outcome, longrepr,
|
2017-07-17 07:25:07 +08:00
|
|
|
getattr(call, 'result', None))
|
2013-09-06 17:56:04 +08:00
|
|
|
rep.call = call # see collect_one_node
|
|
|
|
return rep
|
|
|
|
|
2010-09-26 22:23:44 +08:00
|
|
|
|
|
|
|
class CollectReport(BaseReport):
|
2014-03-14 19:49:36 +08:00
|
|
|
def __init__(self, nodeid, outcome, longrepr, result,
|
|
|
|
sections=(), **extra):
|
2010-09-26 22:23:44 +08:00
|
|
|
self.nodeid = nodeid
|
|
|
|
self.outcome = outcome
|
|
|
|
self.longrepr = longrepr
|
2010-10-26 05:08:56 +08:00
|
|
|
self.result = result or []
|
2011-07-13 05:09:03 +08:00
|
|
|
self.sections = list(sections)
|
2012-01-21 02:50:45 +08:00
|
|
|
self.__dict__.update(extra)
|
2010-09-26 22:23:44 +08:00
|
|
|
|
|
|
|
@property
|
|
|
|
def location(self):
|
|
|
|
return (self.fspath, None, self.fspath)
|
|
|
|
|
|
|
|
def __repr__(self):
|
2010-11-06 16:58:04 +08:00
|
|
|
return "<CollectReport %r lenresult=%s outcome=%r>" % (
|
2017-07-17 07:25:07 +08:00
|
|
|
self.nodeid, len(self.result), self.outcome)
|
2010-09-26 22:23:44 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2010-09-26 22:23:44 +08:00
|
|
|
class CollectErrorRepr(TerminalRepr):
|
|
|
|
def __init__(self, msg):
|
|
|
|
self.longrepr = msg
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2010-09-26 22:23:44 +08:00
|
|
|
def toterminal(self, out):
|
2012-06-24 22:42:31 +08:00
|
|
|
out.line(self.longrepr, red=True)
|
2010-09-26 22:23:44 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2009-04-05 03:06:20 +08:00
|
|
|
class SetupState(object):
|
|
|
|
""" shared state for setting up/tearing down test items or collectors. """
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2009-04-05 03:06:20 +08:00
|
|
|
def __init__(self):
|
|
|
|
self.stack = []
|
2009-05-19 01:06:16 +08:00
|
|
|
self._finalizers = {}
|
|
|
|
|
|
|
|
def addfinalizer(self, finalizer, colitem):
|
2010-07-27 03:15:15 +08:00
|
|
|
""" attach a finalizer to the given colitem.
|
|
|
|
if colitem is None, this will add a finalizer that
|
|
|
|
is called at the end of teardown_all().
|
2009-05-21 20:37:30 +08:00
|
|
|
"""
|
2014-01-26 19:44:21 +08:00
|
|
|
assert colitem and not isinstance(colitem, tuple)
|
2014-05-14 03:05:53 +08:00
|
|
|
assert py.builtin.callable(finalizer)
|
2017-07-17 07:25:09 +08:00
|
|
|
# assert colitem in self.stack # some unit tests don't setup stack :/
|
2009-05-19 01:06:16 +08:00
|
|
|
self._finalizers.setdefault(colitem, []).append(finalizer)
|
|
|
|
|
2009-05-21 20:37:30 +08:00
|
|
|
def _pop_and_teardown(self):
|
|
|
|
colitem = self.stack.pop()
|
|
|
|
self._teardown_with_finalization(colitem)
|
|
|
|
|
2009-07-08 22:41:30 +08:00
|
|
|
def _callfinalizers(self, colitem):
|
2009-05-19 01:06:16 +08:00
|
|
|
finalizers = self._finalizers.pop(colitem, None)
|
2013-11-20 01:26:18 +08:00
|
|
|
exc = None
|
2009-05-19 01:06:16 +08:00
|
|
|
while finalizers:
|
|
|
|
fin = finalizers.pop()
|
2013-11-20 01:26:18 +08:00
|
|
|
try:
|
|
|
|
fin()
|
2015-09-19 07:03:05 +08:00
|
|
|
except TEST_OUTCOME:
|
2013-11-21 09:15:24 +08:00
|
|
|
# XXX Only first exception will be seen by user,
|
|
|
|
# ideally all should be reported.
|
2013-11-21 20:53:04 +08:00
|
|
|
if exc is None:
|
2013-11-21 09:15:24 +08:00
|
|
|
exc = sys.exc_info()
|
2013-11-20 01:26:18 +08:00
|
|
|
if exc:
|
|
|
|
py.builtin._reraise(*exc)
|
2009-07-08 22:41:30 +08:00
|
|
|
|
2010-07-27 03:15:15 +08:00
|
|
|
def _teardown_with_finalization(self, colitem):
|
|
|
|
self._callfinalizers(colitem)
|
2012-07-30 16:46:03 +08:00
|
|
|
if hasattr(colitem, "teardown"):
|
2009-05-21 20:37:30 +08:00
|
|
|
colitem.teardown()
|
2009-05-19 01:06:16 +08:00
|
|
|
for colitem in self._finalizers:
|
2012-07-30 16:46:03 +08:00
|
|
|
assert colitem is None or colitem in self.stack \
|
2017-07-17 07:25:07 +08:00
|
|
|
or isinstance(colitem, tuple)
|
2009-04-05 03:06:20 +08:00
|
|
|
|
2010-07-27 03:15:15 +08:00
|
|
|
def teardown_all(self):
|
|
|
|
while self.stack:
|
2009-05-21 20:37:30 +08:00
|
|
|
self._pop_and_teardown()
|
2012-07-30 16:46:03 +08:00
|
|
|
for key in list(self._finalizers):
|
|
|
|
self._teardown_with_finalization(key)
|
2009-05-19 01:06:16 +08:00
|
|
|
assert not self._finalizers
|
2009-04-05 03:06:20 +08:00
|
|
|
|
2011-12-03 05:00:19 +08:00
|
|
|
def teardown_exact(self, item, nextitem):
|
|
|
|
needed_collectors = nextitem and nextitem.listchain() or []
|
2011-11-19 00:01:29 +08:00
|
|
|
self._teardown_towards(needed_collectors)
|
|
|
|
|
|
|
|
def _teardown_towards(self, needed_collectors):
|
|
|
|
while self.stack:
|
|
|
|
if self.stack == needed_collectors[:len(self.stack)]:
|
|
|
|
break
|
2009-07-08 22:41:30 +08:00
|
|
|
self._pop_and_teardown()
|
2010-07-27 03:15:15 +08:00
|
|
|
|
|
|
|
def prepare(self, colitem):
|
2009-04-05 03:06:20 +08:00
|
|
|
""" setup objects along the collector chain to the test-method
|
2009-07-08 22:41:30 +08:00
|
|
|
and teardown previously setup objects."""
|
2010-07-27 03:15:15 +08:00
|
|
|
needed_collectors = colitem.listchain()
|
2011-11-19 00:01:29 +08:00
|
|
|
self._teardown_towards(needed_collectors)
|
|
|
|
|
2010-07-27 03:15:15 +08:00
|
|
|
# check if the last collection node has raised an error
|
2010-01-28 22:36:27 +08:00
|
|
|
for col in self.stack:
|
|
|
|
if hasattr(col, '_prepare_exc'):
|
2010-07-27 03:15:15 +08:00
|
|
|
py.builtin._reraise(*col._prepare_exc)
|
|
|
|
for col in needed_collectors[len(self.stack):]:
|
|
|
|
self.stack.append(col)
|
2010-01-27 19:09:30 +08:00
|
|
|
try:
|
2010-07-27 03:15:15 +08:00
|
|
|
col.setup()
|
2015-09-19 07:03:05 +08:00
|
|
|
except TEST_OUTCOME:
|
2010-01-28 21:20:58 +08:00
|
|
|
col._prepare_exc = sys.exc_info()
|
|
|
|
raise
|
2010-04-28 03:13:09 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2013-09-06 17:56:04 +08:00
|
|
|
def collect_one_node(collector):
|
|
|
|
ihook = collector.ihook
|
|
|
|
ihook.pytest_collectstart(collector=collector)
|
|
|
|
rep = ihook.pytest_make_collect_report(collector=collector)
|
|
|
|
call = rep.__dict__.pop("call", None)
|
|
|
|
if call and check_interactive_exception(call, rep):
|
|
|
|
ihook.pytest_exception_interact(node=collector, call=call, report=rep)
|
|
|
|
return rep
|