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
|
|
|
"""
|
2017-03-17 09:21:30 +08:00
|
|
|
from __future__ import absolute_import, division, print_function
|
2009-08-29 01:16:15 +08:00
|
|
|
import sys
|
2017-08-03 01:33:52 +08:00
|
|
|
import six
|
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
|
2016-09-22 07:06:45 +08:00
|
|
|
from _pytest.assertion import truncate
|
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 register_assert_rewrite(*names):
|
2016-12-02 19:22:47 +08:00
|
|
|
"""Register one or more module names to be rewritten on import.
|
2016-06-26 00:26:45 +08:00
|
|
|
|
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
|
|
|
|
|
|
|
|
|
2017-05-18 05:16:11 +08:00
|
|
|
class AssertionState:
|
2011-05-27 05:08:25 +08:00
|
|
|
"""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."""
|
2017-10-10 13:54:56 +08:00
|
|
|
# Jython has an AST bug that make the assertion rewriting hook malfunction.
|
|
|
|
if (sys.platform.startswith('java')):
|
2016-07-15 07:18:50 +08:00
|
|
|
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
|
|
|
|
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-09-22 07:06:45 +08:00
|
|
|
* Overly verbose explanations are truncated unless configured otherwise
|
|
|
|
(eg. if running in verbose mode).
|
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-09-22 07:06:45 +08:00
|
|
|
new_expl = truncate.truncate_if_required(new_expl, item)
|
2014-04-03 00:35:22 +08:00
|
|
|
new_expl = [line.replace("\n", "\\n") for line in new_expl]
|
2017-08-03 01:33:52 +08:00
|
|
|
res = six.text_type("\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
|