introduce --assertmode option

This commit is contained in:
Benjamin Peterson 2011-05-26 14:34:27 -05:00
parent d3645758ea
commit d438a0bd83
3 changed files with 46 additions and 12 deletions

View File

@ -6,6 +6,7 @@ import imp
import marshal
import struct
import sys
import pytest
from _pytest.monkeypatch import monkeypatch
from _pytest.assertion import reinterpret, util
@ -18,26 +19,40 @@ else:
def pytest_addoption(parser):
group = parser.getgroup("debugconfig")
group._addoption('--assertmode', action="store", dest="assertmode",
choices=("on", "old", "off", "default"), default="default",
metavar="on|old|off",
help="Control assertion debugging tools")
group._addoption('--no-assert', action="store_true", default=False,
dest="noassert",
help="disable python assert expression reinterpretation."),
dest="noassert", help="DEPRECATED equivalent to --assertmode=off")
group._addoption('--nomagic', action="store_true", default=False,
dest="nomagic",
help="DEPRECATED equivalent to --assertmode=off")
def pytest_configure(config):
global rewrite_asserts
m = monkeypatch()
config._cleanup.append(m.undo)
warn_about_missing_assertion()
if not config.getvalue("noassert") and not config.getvalue("nomagic"):
mode = config.getvalue("assertmode")
if config.getvalue("noassert") or config.getvalue("nomagic"):
if mode not in ("off", "default"):
raise pytest.UsageError("assertion options conflict")
mode = "off"
elif mode == "default":
mode = "on"
if mode != "off":
def callbinrepr(op, left, right):
hook_result = config.hook.pytest_assertrepr_compare(
config=config, op=op, left=left, right=right)
for new_expl in hook_result:
if new_expl:
return '\n~'.join(new_expl)
m = monkeypatch()
config._cleanup.append(m.undo)
m.setattr(py.builtin.builtins, 'AssertionError',
reinterpret.AssertionError)
m.setattr(util, '_reprcompare', callbinrepr)
else:
if mode != "on":
rewrite_asserts = None
def _write_pyc(co, source_path):

View File

@ -16,9 +16,6 @@ def pytest_addoption(parser):
group.addoption('--traceconfig',
action="store_true", dest="traceconfig", default=False,
help="trace considerations of conftest.py files."),
group._addoption('--nomagic',
action="store_true", dest="nomagic", default=False,
help="don't reinterpret asserts, no traceback cutting. ")
group.addoption('--debug',
action="store_true", dest="debug", default=False,
help="generate and show internal debugging information.")

View File

@ -160,7 +160,7 @@ def test_sequence_comparison_uses_repr(testdir):
])
def test_functional(testdir):
def test_assertion_options(testdir):
testdir.makepyfile("""
def test_hello():
x = 3
@ -168,8 +168,30 @@ def test_functional(testdir):
""")
result = testdir.runpytest()
assert "3 == 4" in result.stdout.str()
result = testdir.runpytest("--no-assert")
assert "3 == 4" not in result.stdout.str()
off_options = (("--no-assert",),
("--nomagic",),
("--no-assert", "--nomagic"),
("--assertmode=off",),
("--assertmode=off", "--no-assert"),
("--assertmode=off", "--nomagic"),
("--assertmode=off," "--no-assert", "--nomagic"))
for opt in off_options:
result = testdir.runpytest(*opt)
assert "3 == 4" not in result.stdout.str()
for mode in "on", "old":
for other_opt in off_options[:3]:
opt = ("--assertmode=" + mode,) + other_opt
result = testdir.runpytest(*opt)
assert result.ret == 3
assert "assertion options conflict" in result.stderr.str()
def test_old_assert_mode(testdir):
testdir.makepyfile("""
def test_in_old_mode():
assert "@py_builtins" not in globals()
""")
result = testdir.runpytest("--assertmode=old")
assert result.ret == 0
def test_triple_quoted_string_issue113(testdir):
testdir.makepyfile("""