test_ok1/_pytest/assertion/__init__.py

120 lines
4.3 KiB
Python
Raw Normal View History

"""
2011-05-27 02:15:21 +08:00
support for presenting detailed information in failing assertions.
"""
import py
import sys
2011-05-27 03:34:27 +08:00
import pytest
from _pytest.monkeypatch import monkeypatch
2011-06-29 10:13:12 +08:00
from _pytest.assertion import util
def pytest_addoption(parser):
group = parser.getgroup("debugconfig")
group.addoption('--assert', action="store", dest="assertmode",
choices=("rewrite", "reinterp", "plain",),
default="rewrite", metavar="MODE",
2011-05-27 12:15:33 +08:00
help="""control assertion debugging tools.
'plain' performs no assertion debugging.
'reinterp' reinterprets assert statements after they failed to provide assertion expression information.
'rewrite' (the default) rewrites assert statements in test modules on import
to provide assert expression information. """)
2011-05-27 12:15:33 +08:00
group.addoption('--no-assert', action="store_true", default=False,
dest="noassert", help="DEPRECATED equivalent to --assert=plain")
2011-05-27 12:15:33 +08:00
group.addoption('--nomagic', action="store_true", default=False,
dest="nomagic", help="DEPRECATED equivalent to --assert=plain")
2011-05-27 03:34:27 +08:00
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")
def pytest_configure(config):
2011-05-27 03:34:27 +08:00
mode = config.getvalue("assertmode")
if config.getvalue("noassert") or config.getvalue("nomagic"):
mode = "plain"
2011-07-05 21:21:08 +08:00
if mode == "rewrite":
try:
import ast
except ImportError:
mode = "reinterp"
else:
if sys.platform.startswith('java'):
mode = "reinterp"
if mode != "plain":
2011-06-29 10:13:12 +08:00
_load_modules(mode)
2011-05-27 03:34:27 +08:00
m = monkeypatch()
config._cleanup.append(m.undo)
2011-05-26 06:54:02 +08:00
m.setattr(py.builtin.builtins, 'AssertionError',
reinterpret.AssertionError)
2011-06-29 10:13:12 +08:00
hook = None
if mode == "rewrite":
2011-06-29 10:13:12 +08:00
hook = rewrite.AssertionRewritingHook()
sys.meta_path.append(hook)
warn_about_missing_assertion(mode)
2011-05-27 05:18:18 +08:00
config._assertstate = AssertionState(config, mode)
2011-06-29 10:13:12 +08:00
config._assertstate.hook = hook
2011-05-27 05:18:18 +08:00
config._assertstate.trace("configured with mode set to %r" % (mode,))
2011-06-29 10:13:12 +08:00
def pytest_unconfigure(config):
hook = config._assertstate.hook
if hook is not None:
sys.meta_path.remove(hook)
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)
2011-06-29 10:13:12 +08:00
hook = session.config._assertstate.hook
if hook is not None:
hook.set_session(session)
def pytest_runtest_setup(item):
def callbinrepr(op, left, right):
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:
res = '\n~'.join(new_expl)
if item.config.getvalue("assertmode") == "rewrite":
# The result will be fed back a python % formatting
# operation, which will fail if there are extraneous
# '%'s in the string. Escape them here.
res = res.replace("%", "%%")
return res
util._reprcompare = callbinrepr
def pytest_runtest_teardown(item):
util._reprcompare = None
2011-06-29 10:13:12 +08:00
def pytest_sessionfinish(session):
hook = session.config._assertstate.hook
if hook is not None:
hook.session = None
2011-06-29 10:13:12 +08:00
def _load_modules(mode):
"""Lazily import assertion related code."""
global rewrite, reinterpret
from _pytest.assertion import reinterpret
if mode == "rewrite":
2011-06-29 10:13:12 +08:00
from _pytest.assertion import rewrite
def warn_about_missing_assertion(mode):
try:
assert False
except AssertionError:
pass
else:
if mode == "rewrite":
2011-06-29 10:13:12 +08:00
specifically = ("assertions which are not in test modules "
"will be ignored")
else:
specifically = "failing tests may report as passing"
sys.stderr.write("WARNING: " + specifically +
" because assert statements are not executed "
"by the underlying Python interpreter "
2011-06-29 10:13:12 +08:00
"(are you using python -O?)\n")
pytest_assertrepr_compare = util.assertrepr_compare