2010-11-06 18:38:53 +08:00
|
|
|
"""
|
2011-05-27 02:15:21 +08:00
|
|
|
support for presenting detailed information in failing assertions.
|
2010-11-06 18:38:53 +08:00
|
|
|
"""
|
2009-08-27 23:26:02 +08:00
|
|
|
import py
|
2016-01-08 17:58:26 +08:00
|
|
|
import os
|
2009-08-29 01:16:15 +08:00
|
|
|
import sys
|
2016-06-20 20:45:13 +08:00
|
|
|
|
2011-06-29 10:13:12 +08:00
|
|
|
from _pytest.assertion import util
|
2016-06-26 00:26:45 +08:00
|
|
|
from _pytest.assertion import rewrite
|
2009-08-27 23:26:02 +08:00
|
|
|
|
2014-04-03 00:16:37 +08:00
|
|
|
|
2009-08-27 23:26:02 +08:00
|
|
|
def pytest_addoption(parser):
|
|
|
|
group = parser.getgroup("debugconfig")
|
2014-04-03 00:16:37 +08:00
|
|
|
group.addoption('--assert',
|
|
|
|
action="store",
|
|
|
|
dest="assertmode",
|
2016-07-15 07:18:50 +08:00
|
|
|
choices=("rewrite", "plain",),
|
2014-04-03 00:16:37 +08:00
|
|
|
default="rewrite",
|
|
|
|
metavar="MODE",
|
2016-07-15 07:18:50 +08:00
|
|
|
help="""Control assertion debugging tools. 'plain'
|
|
|
|
performs no assertion debugging. 'rewrite'
|
|
|
|
(the default) rewrites assert statements in
|
|
|
|
test modules on import to provide assert
|
|
|
|
expression information.""")
|
2014-04-03 00:16:37 +08:00
|
|
|
|
2011-05-27 03:34:27 +08:00
|
|
|
|
2016-06-26 00:26:45 +08:00
|
|
|
def pytest_namespace():
|
|
|
|
return {'register_assert_rewrite': register_assert_rewrite}
|
|
|
|
|
|
|
|
|
|
|
|
def register_assert_rewrite(*names):
|
|
|
|
"""Register a module name to be rewritten on import.
|
|
|
|
|
2016-08-03 06:16:27 +08:00
|
|
|
This function will make sure that this module or all modules inside
|
|
|
|
the package will get their assert statements rewritten.
|
|
|
|
Thus you should make sure to call this before the module is
|
|
|
|
actually imported, usually in your __init__.py if you are a plugin
|
|
|
|
using a package.
|
2016-08-31 09:15:53 +08:00
|
|
|
|
|
|
|
:raise TypeError: if the given module names are not strings.
|
2016-06-26 00:26:45 +08:00
|
|
|
"""
|
2016-08-31 09:15:53 +08:00
|
|
|
for name in names:
|
|
|
|
if not isinstance(name, str):
|
|
|
|
msg = 'expected module names as *args, got {0} instead'
|
|
|
|
raise TypeError(msg.format(repr(names)))
|
2016-06-26 00:26:45 +08:00
|
|
|
for hook in sys.meta_path:
|
|
|
|
if isinstance(hook, rewrite.AssertionRewritingHook):
|
|
|
|
importhook = hook
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
importhook = DummyRewriteHook()
|
|
|
|
importhook.mark_rewrite(*names)
|
|
|
|
|
|
|
|
|
|
|
|
class DummyRewriteHook(object):
|
|
|
|
"""A no-op import hook for when rewriting is disabled."""
|
|
|
|
|
|
|
|
def mark_rewrite(self, *names):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2011-05-27 05:08:25 +08:00
|
|
|
class AssertionState:
|
|
|
|
"""State for the assertion plugin."""
|
|
|
|
|
|
|
|
def __init__(self, config, mode):
|
|
|
|
self.mode = mode
|
|
|
|
self.trace = config.trace.root.get("assertion")
|
2016-07-15 07:18:50 +08:00
|
|
|
self.hook = None
|
2009-08-27 23:26:02 +08:00
|
|
|
|
2014-04-03 00:16:37 +08:00
|
|
|
|
2016-07-15 07:18:50 +08:00
|
|
|
def install_importhook(config):
|
|
|
|
"""Try to install the rewrite hook, raise SystemError if it fails."""
|
|
|
|
# Both Jython and CPython 2.6.0 have AST bugs that make the
|
|
|
|
# assertion rewriting hook malfunction.
|
|
|
|
if (sys.platform.startswith('java') or
|
|
|
|
sys.version_info[:3] == (2, 6, 0)):
|
|
|
|
raise SystemError('rewrite not supported')
|
|
|
|
|
|
|
|
config._assertstate = AssertionState(config, 'rewrite')
|
|
|
|
config._assertstate.hook = hook = rewrite.AssertionRewritingHook(config)
|
|
|
|
sys.meta_path.insert(0, hook)
|
|
|
|
config._assertstate.trace('installed rewrite import hook')
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2015-04-22 22:33:20 +08:00
|
|
|
def undo():
|
2016-06-22 18:42:11 +08:00
|
|
|
hook = config._assertstate.hook
|
2015-04-22 22:33:20 +08:00
|
|
|
if hook is not None and hook in sys.meta_path:
|
|
|
|
sys.meta_path.remove(hook)
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2016-06-22 18:42:11 +08:00
|
|
|
config.add_cleanup(undo)
|
|
|
|
return hook
|
2011-05-25 06:48:56 +08:00
|
|
|
|
2014-04-03 00:16:37 +08:00
|
|
|
|
2011-07-06 00:01:31 +08:00
|
|
|
def pytest_collection(session):
|
|
|
|
# this hook is only called when test modules are collected
|
|
|
|
# so for example not in the master process of pytest-xdist
|
|
|
|
# (which does not collect test modules)
|
2016-06-22 18:42:11 +08:00
|
|
|
assertstate = getattr(session.config, '_assertstate', None)
|
|
|
|
if assertstate:
|
|
|
|
if assertstate.hook is not None:
|
|
|
|
assertstate.hook.set_session(session)
|
2011-05-20 10:52:10 +08:00
|
|
|
|
2014-04-03 00:16:37 +08:00
|
|
|
|
2016-01-08 17:58:26 +08:00
|
|
|
def _running_on_ci():
|
|
|
|
"""Check if we're currently running on a CI system."""
|
|
|
|
env_vars = ['CI', 'BUILD_NUMBER']
|
|
|
|
return any(var in os.environ for var in env_vars)
|
|
|
|
|
|
|
|
|
2011-10-16 18:51:15 +08:00
|
|
|
def pytest_runtest_setup(item):
|
2014-04-03 00:35:22 +08:00
|
|
|
"""Setup the pytest_assertrepr_compare hook
|
|
|
|
|
|
|
|
The newinterpret and rewrite modules will use util._reprcompare if
|
|
|
|
it exists to use custom reporting via the
|
|
|
|
pytest_assertrepr_compare hook. This sets up this custom
|
|
|
|
comparison for the test.
|
|
|
|
"""
|
2011-10-16 18:51:15 +08:00
|
|
|
def callbinrepr(op, left, right):
|
2014-04-03 00:35:22 +08:00
|
|
|
"""Call the pytest_assertrepr_compare hook and prepare the result
|
|
|
|
|
|
|
|
This uses the first result from the hook and then ensures the
|
|
|
|
following:
|
2016-01-08 17:58:26 +08:00
|
|
|
* Overly verbose explanations are dropped unless -vv was used or
|
|
|
|
running on a CI.
|
2014-04-03 00:35:22 +08:00
|
|
|
* Embedded newlines are escaped to help util.format_explanation()
|
|
|
|
later.
|
|
|
|
* If the rewrite mode is used embedded %-characters are replaced
|
|
|
|
to protect later % formatting.
|
|
|
|
|
|
|
|
The result can be formatted by util.format_explanation() for
|
|
|
|
pretty printing.
|
|
|
|
"""
|
2011-10-16 18:51:15 +08:00
|
|
|
hook_result = item.ihook.pytest_assertrepr_compare(
|
|
|
|
config=item.config, op=op, left=left, right=right)
|
|
|
|
for new_expl in hook_result:
|
|
|
|
if new_expl:
|
2016-01-15 07:01:07 +08:00
|
|
|
if (sum(len(p) for p in new_expl[1:]) > 80*8 and
|
2016-01-23 04:32:45 +08:00
|
|
|
item.config.option.verbose < 2 and
|
|
|
|
not _running_on_ci()):
|
2015-08-28 09:20:13 +08:00
|
|
|
show_max = 10
|
|
|
|
truncated_lines = len(new_expl) - show_max
|
|
|
|
new_expl[show_max:] = [py.builtin._totext(
|
|
|
|
'Detailed information truncated (%d more lines)'
|
|
|
|
', use "-vv" to show' % truncated_lines)]
|
2014-04-03 00:35:22 +08:00
|
|
|
new_expl = [line.replace("\n", "\\n") for line in new_expl]
|
2014-04-03 00:16:37 +08:00
|
|
|
res = py.builtin._totext("\n~").join(new_expl)
|
2011-10-16 18:51:15 +08:00
|
|
|
if item.config.getvalue("assertmode") == "rewrite":
|
|
|
|
res = res.replace("%", "%%")
|
|
|
|
return res
|
|
|
|
util._reprcompare = callbinrepr
|
|
|
|
|
2014-04-03 00:16:37 +08:00
|
|
|
|
2011-10-16 18:51:15 +08:00
|
|
|
def pytest_runtest_teardown(item):
|
|
|
|
util._reprcompare = None
|
|
|
|
|
2014-04-03 00:16:37 +08:00
|
|
|
|
2011-06-29 10:13:12 +08:00
|
|
|
def pytest_sessionfinish(session):
|
2016-06-22 18:42:11 +08:00
|
|
|
assertstate = getattr(session.config, '_assertstate', None)
|
|
|
|
if assertstate:
|
|
|
|
if assertstate.hook is not None:
|
|
|
|
assertstate.hook.set_session(None)
|
2009-08-27 23:26:02 +08:00
|
|
|
|
2014-04-03 00:16:37 +08:00
|
|
|
|
2014-04-03 00:35:22 +08:00
|
|
|
# Expose this plugin's implementation for the pytest_assertrepr_compare hook
|
2011-05-27 01:01:34 +08:00
|
|
|
pytest_assertrepr_compare = util.assertrepr_compare
|