2010-11-06 18:38:53 +08:00
|
|
|
""" support for skip/xfail functions and markers. """
|
2009-10-15 22:18:57 +08:00
|
|
|
|
2010-11-06 06:37:31 +08:00
|
|
|
import py, pytest
|
2011-03-03 19:19:17 +08:00
|
|
|
import sys
|
2009-10-15 22:18:57 +08:00
|
|
|
|
2010-05-04 19:02:27 +08:00
|
|
|
def pytest_addoption(parser):
|
|
|
|
group = parser.getgroup("general")
|
2010-07-27 03:15:15 +08:00
|
|
|
group.addoption('--runxfail',
|
2010-05-04 19:02:27 +08:00
|
|
|
action="store_true", dest="runxfail", default=False,
|
|
|
|
help="run tests even if they are marked xfail")
|
|
|
|
|
2011-11-12 06:56:11 +08:00
|
|
|
def pytest_configure(config):
|
|
|
|
config.addinivalue_line("markers",
|
2012-10-20 20:05:33 +08:00
|
|
|
"skipif(condition): skip the given test function if eval(condition) "
|
|
|
|
"results in a True value. Evaluation happens within the "
|
2011-11-12 06:56:11 +08:00
|
|
|
"module global context. Example: skipif('sys.platform == \"win32\"') "
|
2012-10-20 19:56:53 +08:00
|
|
|
"skips the test if we are on the win32 platform. see "
|
|
|
|
"http://pytest.org/latest/skipping.html"
|
2011-11-12 06:56:11 +08:00
|
|
|
)
|
|
|
|
config.addinivalue_line("markers",
|
2012-10-20 20:05:33 +08:00
|
|
|
"xfail(condition, reason=None, run=True): mark the the test function "
|
|
|
|
"as an expected failure if eval(condition) has a True value. "
|
|
|
|
"Optionally specify a reason for better reporting and run=False if "
|
|
|
|
"you don't even want to execute the test function. See "
|
|
|
|
"http://pytest.org/latest/skipping.html"
|
2011-11-12 06:56:11 +08:00
|
|
|
)
|
|
|
|
|
2010-11-06 06:37:31 +08:00
|
|
|
def pytest_namespace():
|
|
|
|
return dict(xfail=xfail)
|
|
|
|
|
|
|
|
class XFailed(pytest.fail.Exception):
|
|
|
|
""" raised from an explicit call to py.test.xfail() """
|
|
|
|
|
|
|
|
def xfail(reason=""):
|
|
|
|
""" xfail an executing test or setup functions with the given reason."""
|
|
|
|
__tracebackhide__ = True
|
|
|
|
raise XFailed(reason)
|
|
|
|
xfail.Exception = XFailed
|
|
|
|
|
2010-05-04 18:37:56 +08:00
|
|
|
class MarkEvaluator:
|
|
|
|
def __init__(self, item, name):
|
|
|
|
self.item = item
|
|
|
|
self.name = name
|
|
|
|
|
2010-06-08 08:34:51 +08:00
|
|
|
@property
|
|
|
|
def holder(self):
|
|
|
|
return self.item.keywords.get(self.name, None)
|
2010-05-04 18:37:56 +08:00
|
|
|
def __bool__(self):
|
|
|
|
return bool(self.holder)
|
|
|
|
__nonzero__ = __bool__
|
|
|
|
|
2011-03-03 19:19:17 +08:00
|
|
|
def wasvalid(self):
|
|
|
|
return not hasattr(self, 'exc')
|
|
|
|
|
2010-05-04 18:37:56 +08:00
|
|
|
def istrue(self):
|
2011-03-03 19:19:17 +08:00
|
|
|
try:
|
|
|
|
return self._istrue()
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
raise
|
|
|
|
except:
|
|
|
|
self.exc = sys.exc_info()
|
|
|
|
if isinstance(self.exc[1], SyntaxError):
|
|
|
|
msg = [" " * (self.exc[1].offset + 4) + "^",]
|
|
|
|
msg.append("SyntaxError: invalid syntax")
|
|
|
|
else:
|
|
|
|
msg = py.std.traceback.format_exception_only(*self.exc[:2])
|
|
|
|
pytest.fail("Error evaluating %r expression\n"
|
|
|
|
" %s\n"
|
|
|
|
"%s"
|
|
|
|
%(self.name, self.expr, "\n".join(msg)),
|
|
|
|
pytrace=False)
|
|
|
|
|
2011-03-04 06:22:55 +08:00
|
|
|
def _getglobals(self):
|
|
|
|
d = {'os': py.std.os, 'sys': py.std.sys, 'config': self.item.config}
|
|
|
|
func = self.item.obj
|
|
|
|
try:
|
|
|
|
d.update(func.__globals__)
|
|
|
|
except AttributeError:
|
|
|
|
d.update(func.func_globals)
|
|
|
|
return d
|
|
|
|
|
2011-03-03 19:19:17 +08:00
|
|
|
def _istrue(self):
|
2010-05-04 18:37:56 +08:00
|
|
|
if self.holder:
|
2011-03-04 06:22:55 +08:00
|
|
|
d = self._getglobals()
|
2010-05-22 00:11:47 +08:00
|
|
|
if self.holder.args:
|
|
|
|
self.result = False
|
|
|
|
for expr in self.holder.args:
|
2010-05-04 18:37:56 +08:00
|
|
|
self.expr = expr
|
2013-02-15 21:38:40 +08:00
|
|
|
if isinstance(expr, py.builtin._basestring):
|
2010-05-22 00:11:47 +08:00
|
|
|
result = cached_eval(self.item.config, expr, d)
|
|
|
|
else:
|
2013-05-08 00:40:26 +08:00
|
|
|
if self.get("reason") is None:
|
|
|
|
# XXX better be checked at collection time
|
|
|
|
pytest.fail("you need to specify reason=STRING "
|
|
|
|
"when using booleans as conditions.")
|
|
|
|
result = bool(expr)
|
2010-05-22 00:11:47 +08:00
|
|
|
if result:
|
|
|
|
self.result = True
|
|
|
|
self.expr = expr
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
self.result = True
|
2010-05-04 18:37:56 +08:00
|
|
|
return getattr(self, 'result', False)
|
|
|
|
|
|
|
|
def get(self, attr, default=None):
|
|
|
|
return self.holder.kwargs.get(attr, default)
|
|
|
|
|
|
|
|
def getexplanation(self):
|
|
|
|
expl = self.get('reason', None)
|
|
|
|
if not expl:
|
|
|
|
if not hasattr(self, 'expr'):
|
2010-05-06 01:50:59 +08:00
|
|
|
return ""
|
2010-05-04 18:37:56 +08:00
|
|
|
else:
|
2011-03-04 06:22:55 +08:00
|
|
|
return "condition: " + str(self.expr)
|
2010-05-04 18:37:56 +08:00
|
|
|
return expl
|
2010-07-27 03:15:15 +08:00
|
|
|
|
2009-12-29 23:29:48 +08:00
|
|
|
|
2012-06-20 05:48:39 +08:00
|
|
|
@pytest.mark.tryfirst
|
2009-10-15 22:18:57 +08:00
|
|
|
def pytest_runtest_setup(item):
|
2010-11-13 16:05:11 +08:00
|
|
|
if not isinstance(item, pytest.Function):
|
2010-05-03 04:13:16 +08:00
|
|
|
return
|
2010-05-04 18:37:56 +08:00
|
|
|
evalskip = MarkEvaluator(item, 'skipif')
|
|
|
|
if evalskip.istrue():
|
|
|
|
py.test.skip(evalskip.getexplanation())
|
|
|
|
item._evalxfail = MarkEvaluator(item, 'xfail')
|
2010-06-08 08:34:51 +08:00
|
|
|
check_xfail_no_run(item)
|
|
|
|
|
|
|
|
def pytest_pyfunc_call(pyfuncitem):
|
|
|
|
check_xfail_no_run(pyfuncitem)
|
|
|
|
|
|
|
|
def check_xfail_no_run(item):
|
2010-11-01 06:28:31 +08:00
|
|
|
if not item.config.option.runxfail:
|
2010-06-08 08:34:51 +08:00
|
|
|
evalxfail = item._evalxfail
|
|
|
|
if evalxfail.istrue():
|
|
|
|
if not evalxfail.get('run', True):
|
|
|
|
py.test.xfail("[NOTRUN] " + evalxfail.getexplanation())
|
2009-10-15 22:18:57 +08:00
|
|
|
|
|
|
|
def pytest_runtest_makereport(__multicall__, item, call):
|
2010-11-13 16:05:11 +08:00
|
|
|
if not isinstance(item, pytest.Function):
|
2009-10-15 22:18:57 +08:00
|
|
|
return
|
2012-03-20 13:53:52 +08:00
|
|
|
# unitttest special case, see setting of _unexpectedsuccess
|
|
|
|
if hasattr(item, '_unexpectedsuccess'):
|
|
|
|
rep = __multicall__.execute()
|
|
|
|
if rep.when == "call":
|
|
|
|
# we need to translate into how py.test encodes xpass
|
2012-06-23 17:32:32 +08:00
|
|
|
rep.wasxfail = "reason: " + repr(item._unexpectedsuccess)
|
2012-03-20 13:53:52 +08:00
|
|
|
rep.outcome = "failed"
|
|
|
|
return rep
|
2010-07-27 03:15:15 +08:00
|
|
|
if not (call.excinfo and
|
2010-05-20 19:29:51 +08:00
|
|
|
call.excinfo.errisinstance(py.test.xfail.Exception)):
|
|
|
|
evalxfail = getattr(item, '_evalxfail', None)
|
|
|
|
if not evalxfail:
|
|
|
|
return
|
|
|
|
if call.excinfo and call.excinfo.errisinstance(py.test.xfail.Exception):
|
2010-05-22 23:08:49 +08:00
|
|
|
if not item.config.getvalue("runxfail"):
|
|
|
|
rep = __multicall__.execute()
|
2012-06-23 17:32:32 +08:00
|
|
|
rep.wasxfail = "reason: " + call.excinfo.value.msg
|
2010-09-26 22:23:44 +08:00
|
|
|
rep.outcome = "skipped"
|
2010-05-22 23:08:49 +08:00
|
|
|
return rep
|
2010-12-07 01:20:47 +08:00
|
|
|
rep = __multicall__.execute()
|
|
|
|
evalxfail = item._evalxfail
|
2012-06-23 17:32:32 +08:00
|
|
|
if not rep.skipped:
|
|
|
|
if not item.config.option.runxfail:
|
|
|
|
if evalxfail.wasvalid() and evalxfail.istrue():
|
|
|
|
if call.excinfo:
|
|
|
|
rep.outcome = "skipped"
|
|
|
|
elif call.when == "call":
|
|
|
|
rep.outcome = "failed"
|
|
|
|
else:
|
|
|
|
return rep
|
|
|
|
rep.wasxfail = evalxfail.getexplanation()
|
|
|
|
return rep
|
2010-12-07 01:20:47 +08:00
|
|
|
return rep
|
2009-10-15 22:18:57 +08:00
|
|
|
|
2009-10-23 00:37:24 +08:00
|
|
|
# called by terminalreporter progress reporting
|
2009-10-15 22:18:57 +08:00
|
|
|
def pytest_report_teststatus(report):
|
2012-06-23 17:32:32 +08:00
|
|
|
if hasattr(report, "wasxfail"):
|
2009-10-15 22:18:57 +08:00
|
|
|
if report.skipped:
|
|
|
|
return "xfailed", "x", "xfail"
|
|
|
|
elif report.failed:
|
2010-05-20 20:35:13 +08:00
|
|
|
return "xpassed", "X", "XPASS"
|
2009-10-15 22:18:57 +08:00
|
|
|
|
|
|
|
# called by the terminalreporter instance/plugin
|
|
|
|
def pytest_terminal_summary(terminalreporter):
|
|
|
|
tr = terminalreporter
|
2010-05-06 01:50:59 +08:00
|
|
|
if not tr.reportchars:
|
|
|
|
#for name in "xfailed skipped failed xpassed":
|
|
|
|
# if not tr.stats.get(name, 0):
|
|
|
|
# tr.write_line("HINT: use '-r' option to see extra "
|
|
|
|
# "summary info about tests")
|
|
|
|
# break
|
|
|
|
return
|
|
|
|
|
|
|
|
lines = []
|
|
|
|
for char in tr.reportchars:
|
|
|
|
if char == "x":
|
|
|
|
show_xfailed(terminalreporter, lines)
|
2010-05-20 20:35:13 +08:00
|
|
|
elif char == "X":
|
2010-05-06 01:50:59 +08:00
|
|
|
show_xpassed(terminalreporter, lines)
|
2010-09-26 22:23:44 +08:00
|
|
|
elif char in "fF":
|
2011-09-30 05:44:26 +08:00
|
|
|
show_simple(terminalreporter, lines, 'failed', "FAIL %s")
|
2010-09-26 22:23:44 +08:00
|
|
|
elif char in "sS":
|
2010-05-06 01:50:59 +08:00
|
|
|
show_skipped(terminalreporter, lines)
|
2011-09-30 05:44:26 +08:00
|
|
|
elif char == "E":
|
|
|
|
show_simple(terminalreporter, lines, 'error', "ERROR %s")
|
2010-05-06 01:50:59 +08:00
|
|
|
if lines:
|
|
|
|
tr._tw.sep("=", "short test summary info")
|
|
|
|
for line in lines:
|
|
|
|
tr._tw.line(line)
|
|
|
|
|
2011-09-30 05:44:26 +08:00
|
|
|
def show_simple(terminalreporter, lines, stat, format):
|
2010-05-06 01:50:59 +08:00
|
|
|
tw = terminalreporter._tw
|
2011-09-30 05:44:26 +08:00
|
|
|
failed = terminalreporter.stats.get(stat)
|
2010-05-06 01:50:59 +08:00
|
|
|
if failed:
|
|
|
|
for rep in failed:
|
2010-09-26 22:23:44 +08:00
|
|
|
pos = rep.nodeid
|
2011-09-30 05:44:26 +08:00
|
|
|
lines.append(format %(pos, ))
|
2010-05-06 01:50:59 +08:00
|
|
|
|
|
|
|
def show_xfailed(terminalreporter, lines):
|
|
|
|
xfailed = terminalreporter.stats.get("xfailed")
|
2009-10-15 22:18:57 +08:00
|
|
|
if xfailed:
|
|
|
|
for rep in xfailed:
|
2010-09-26 22:23:44 +08:00
|
|
|
pos = rep.nodeid
|
2012-06-23 17:32:32 +08:00
|
|
|
reason = rep.wasxfail
|
2010-09-26 22:23:44 +08:00
|
|
|
lines.append("XFAIL %s" % (pos,))
|
|
|
|
if reason:
|
|
|
|
lines.append(" " + str(reason))
|
2009-10-15 22:18:57 +08:00
|
|
|
|
2010-05-06 01:50:59 +08:00
|
|
|
def show_xpassed(terminalreporter, lines):
|
2009-10-15 22:18:57 +08:00
|
|
|
xpassed = terminalreporter.stats.get("xpassed")
|
|
|
|
if xpassed:
|
|
|
|
for rep in xpassed:
|
2010-09-26 22:23:44 +08:00
|
|
|
pos = rep.nodeid
|
2012-06-23 17:32:32 +08:00
|
|
|
reason = rep.wasxfail
|
2010-05-06 01:50:59 +08:00
|
|
|
lines.append("XPASS %s %s" %(pos, reason))
|
2009-10-15 22:18:57 +08:00
|
|
|
|
2010-04-21 18:50:03 +08:00
|
|
|
def cached_eval(config, expr, d):
|
|
|
|
if not hasattr(config, '_evalcache'):
|
|
|
|
config._evalcache = {}
|
|
|
|
try:
|
|
|
|
return config._evalcache[expr]
|
|
|
|
except KeyError:
|
|
|
|
#import sys
|
|
|
|
#print >>sys.stderr, ("cache-miss: %r" % expr)
|
2011-03-03 19:19:17 +08:00
|
|
|
exprcode = py.code.compile(expr, mode="eval")
|
|
|
|
config._evalcache[expr] = x = eval(exprcode, d)
|
2010-04-21 18:50:03 +08:00
|
|
|
return x
|
|
|
|
|
|
|
|
|
2009-10-17 23:42:40 +08:00
|
|
|
def folded_skips(skipped):
|
|
|
|
d = {}
|
|
|
|
for event in skipped:
|
2010-11-14 04:03:28 +08:00
|
|
|
key = event.longrepr
|
|
|
|
assert len(key) == 3, (event, key)
|
2009-10-17 23:42:40 +08:00
|
|
|
d.setdefault(key, []).append(event)
|
|
|
|
l = []
|
2010-07-27 03:15:15 +08:00
|
|
|
for key, events in d.items():
|
2009-10-17 23:42:40 +08:00
|
|
|
l.append((len(events),) + key)
|
2010-07-27 03:15:15 +08:00
|
|
|
return l
|
2009-10-17 23:42:40 +08:00
|
|
|
|
2010-05-06 01:50:59 +08:00
|
|
|
def show_skipped(terminalreporter, lines):
|
2009-10-17 23:42:40 +08:00
|
|
|
tr = terminalreporter
|
|
|
|
skipped = tr.stats.get('skipped', [])
|
|
|
|
if skipped:
|
2010-05-06 01:50:59 +08:00
|
|
|
#if not tr.hasopt('skipped'):
|
|
|
|
# tr.write_line(
|
|
|
|
# "%d skipped tests, specify -rs for more info" %
|
|
|
|
# len(skipped))
|
|
|
|
# return
|
2009-10-17 23:42:40 +08:00
|
|
|
fskips = folded_skips(skipped)
|
|
|
|
if fskips:
|
2010-05-06 01:50:59 +08:00
|
|
|
#tr.write_sep("_", "skipped test summary")
|
2009-10-17 23:42:40 +08:00
|
|
|
for num, fspath, lineno, reason in fskips:
|
2010-05-06 01:50:59 +08:00
|
|
|
if reason.startswith("Skipped: "):
|
|
|
|
reason = reason[9:]
|
|
|
|
lines.append("SKIP [%d] %s:%d: %s" %
|
|
|
|
(num, fspath, lineno, reason))
|