| 1 |
""" |
| 2 |
support for presenting detailed information in failing assertions. |
| 3 |
""" |
| 4 |
import py |
| 5 |
import os |
| 6 |
import sys |
| 7 |
from _pytest.monkeypatch import monkeypatch |
| 8 |
from _pytest.assertion import util |
| 9 |
|
| 10 |
|
| 11 |
def pytest_addoption(parser): |
| 12 |
group = parser.getgroup("debugconfig") |
| 13 |
group.addoption('--assert', |
| 14 |
action="store", |
| 15 |
dest="assertmode", |
| 16 |
choices=("rewrite", "reinterp", "plain",), |
| 17 |
default="rewrite", |
| 18 |
metavar="MODE", |
| 19 |
help="""control assertion debugging tools. 'plain' |
| 20 |
performs no assertion debugging. 'reinterp' |
| 21 |
reinterprets assert statements after they failed |
| 22 |
to provide assertion expression information. |
| 23 |
'rewrite' (the default) rewrites assert |
| 24 |
statements in test modules on import to |
| 25 |
provide assert expression information. """) |
| 26 |
group.addoption('--no-assert', |
| 27 |
action="store_true", |
| 28 |
default=False, |
| 29 |
dest="noassert", |
| 30 |
help="DEPRECATED equivalent to --assert=plain") |
| 31 |
group.addoption('--nomagic', '--no-magic', |
| 32 |
action="store_true", |
| 33 |
default=False, |
| 34 |
help="DEPRECATED equivalent to --assert=plain") |
| 35 |
|
| 36 |
|
| 37 |
class AssertionState: |
| 38 |
"""State for the assertion plugin.""" |
| 39 |
|
| 40 |
def __init__(self, config, mode): |
| 41 |
self.mode = mode |
| 42 |
self.trace = config.trace.root.get("assertion") |
| 43 |
|
| 44 |
|
| 45 |
def pytest_configure(config): |
| 46 |
mode = config.getvalue("assertmode") |
| 47 |
if config.getvalue("noassert") or config.getvalue("nomagic"): |
| 48 |
mode = "plain" |
| 49 |
if mode == "rewrite": |
| 50 |
try: |
| 51 |
import ast |
| 52 |
except ImportError: |
| 53 |
mode = "reinterp" |
| 54 |
else: |
| 55 |
|
| 56 |
|
| 57 |
if (sys.platform.startswith('java') or |
| 58 |
sys.version_info[:3] == (2, 6, 0)): |
| 59 |
mode = "reinterp" |
| 60 |
if mode != "plain": |
| 61 |
_load_modules(mode) |
| 62 |
m = monkeypatch() |
| 63 |
config._cleanup.append(m.undo) |
| 64 |
m.setattr(py.builtin.builtins, 'AssertionError', |
| 65 |
reinterpret.AssertionError) |
| 66 |
hook = None |
| 67 |
if mode == "rewrite": |
| 68 |
hook = rewrite.AssertionRewritingHook() |
| 69 |
sys.meta_path.insert(0, hook) |
| 70 |
warn_about_missing_assertion(mode) |
| 71 |
config._assertstate = AssertionState(config, mode) |
| 72 |
config._assertstate.hook = hook |
| 73 |
config._assertstate.trace("configured with mode set to %r" % (mode,)) |
| 74 |
def undo(): |
| 75 |
hook = config._assertstate.hook |
| 76 |
if hook is not None and hook in sys.meta_path: |
| 77 |
sys.meta_path.remove(hook) |
| 78 |
config.add_cleanup(undo) |
| 79 |
|
| 80 |
|
| 81 |
def pytest_collection(session): |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
hook = session.config._assertstate.hook |
| 86 |
if hook is not None: |
| 87 |
hook.set_session(session) |
| 88 |
|
| 89 |
|
| 90 |
def _running_on_ci(): |
| 91 |
"""Check if we're currently running on a CI system.""" |
| 92 |
env_vars = ['CI', 'BUILD_NUMBER'] |
| 93 |
return any(var in os.environ for var in env_vars) |
| 94 |
|
| 95 |
|
| 96 |
def pytest_runtest_setup(item): |
| 97 |
"""Setup the pytest_assertrepr_compare hook |
| 98 |
|
| 99 |
The newinterpret and rewrite modules will use util._reprcompare if |
| 100 |
it exists to use custom reporting via the |
| 101 |
pytest_assertrepr_compare hook. This sets up this custom |
| 102 |
comparison for the test. |
| 103 |
""" |
| 104 |
def callbinrepr(op, left, right): |
| 105 |
"""Call the pytest_assertrepr_compare hook and prepare the result |
| 106 |
|
| 107 |
This uses the first result from the hook and then ensures the |
| 108 |
following: |
| 109 |
* Overly verbose explanations are dropped unless -vv was used or |
| 110 |
running on a CI. |
| 111 |
* Embedded newlines are escaped to help util.format_explanation() |
| 112 |
later. |
| 113 |
* If the rewrite mode is used embedded %-characters are replaced |
| 114 |
to protect later % formatting. |
| 115 |
|
| 116 |
The result can be formatted by util.format_explanation() for |
| 117 |
pretty printing. |
| 118 |
""" |
| 119 |
hook_result = item.ihook.pytest_assertrepr_compare( |
| 120 |
config=item.config, op=op, left=left, right=right) |
| 121 |
for new_expl in hook_result: |
| 122 |
if new_expl: |
| 123 |
if (sum(len(p) for p in new_expl[1:]) > 80*8 and |
| 124 |
item.config.option.verbose < 2 and |
| 125 |
not _running_on_ci()): |
| 126 |
show_max = 10 |
| 127 |
truncated_lines = len(new_expl) - show_max |
| 128 |
new_expl[show_max:] = [py.builtin._totext( |
| 129 |
'Detailed information truncated (%d more lines)' |
| 130 |
', use "-vv" to show' % truncated_lines)] |
| 131 |
new_expl = [line.replace("\n", "\\n") for line in new_expl] |
| 132 |
res = py.builtin._totext("\n~").join(new_expl) |
| 133 |
if item.config.getvalue("assertmode") == "rewrite": |
| 134 |
res = res.replace("%", "%%") |
| 135 |
return res |
| 136 |
util._reprcompare = callbinrepr |
| 137 |
|
| 138 |
|
| 139 |
def pytest_runtest_teardown(item): |
| 140 |
util._reprcompare = None |
| 141 |
|
| 142 |
|
| 143 |
def pytest_sessionfinish(session): |
| 144 |
hook = session.config._assertstate.hook |
| 145 |
if hook is not None: |
| 146 |
hook.session = None |
| 147 |
|
| 148 |
|
| 149 |
def _load_modules(mode): |
| 150 |
"""Lazily import assertion related code.""" |
| 151 |
global rewrite, reinterpret |
| 152 |
from _pytest.assertion import reinterpret |
| 153 |
if mode == "rewrite": |
| 154 |
from _pytest.assertion import rewrite |
| 155 |
|
| 156 |
|
| 157 |
def warn_about_missing_assertion(mode): |
| 158 |
try: |
| 159 |
assert False |
| 160 |
except AssertionError: |
| 161 |
return |
| 162 |
except Exception as e: |
| 163 |
import sys |
| 164 |
assert sys.version_info.major == 3 and sys.version_info.minor >= 9 |
| 165 |
else: |
| 166 |
if mode == "rewrite": |
| 167 |
specifically = ("assertions which are not in test modules " |
| 168 |
"will be ignored") |
| 169 |
else: |
| 170 |
specifically = "failing tests may report as passing" |
| 171 |
|
| 172 |
sys.stderr.write("WARNING: " + specifically + |
| 173 |
" because assert statements are not executed " |
| 174 |
"by the underlying Python interpreter " |
| 175 |
"(are you using python -O?)\n") |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
pytest_assertrepr_compare = util.assertrepr_compare |