introduce --assertmode option
This commit is contained in:
parent
d3645758ea
commit
d438a0bd83
|
@ -6,6 +6,7 @@ import imp
|
||||||
import marshal
|
import marshal
|
||||||
import struct
|
import struct
|
||||||
import sys
|
import sys
|
||||||
|
import pytest
|
||||||
from _pytest.monkeypatch import monkeypatch
|
from _pytest.monkeypatch import monkeypatch
|
||||||
from _pytest.assertion import reinterpret, util
|
from _pytest.assertion import reinterpret, util
|
||||||
|
|
||||||
|
@ -18,26 +19,40 @@ else:
|
||||||
|
|
||||||
def pytest_addoption(parser):
|
def pytest_addoption(parser):
|
||||||
group = parser.getgroup("debugconfig")
|
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,
|
group._addoption('--no-assert', action="store_true", default=False,
|
||||||
dest="noassert",
|
dest="noassert", help="DEPRECATED equivalent to --assertmode=off")
|
||||||
help="disable python assert expression reinterpretation."),
|
group._addoption('--nomagic', action="store_true", default=False,
|
||||||
|
dest="nomagic",
|
||||||
|
help="DEPRECATED equivalent to --assertmode=off")
|
||||||
|
|
||||||
|
|
||||||
def pytest_configure(config):
|
def pytest_configure(config):
|
||||||
global rewrite_asserts
|
global rewrite_asserts
|
||||||
m = monkeypatch()
|
|
||||||
config._cleanup.append(m.undo)
|
|
||||||
warn_about_missing_assertion()
|
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):
|
def callbinrepr(op, left, right):
|
||||||
hook_result = config.hook.pytest_assertrepr_compare(
|
hook_result = config.hook.pytest_assertrepr_compare(
|
||||||
config=config, op=op, left=left, right=right)
|
config=config, op=op, left=left, right=right)
|
||||||
for new_expl in hook_result:
|
for new_expl in hook_result:
|
||||||
if new_expl:
|
if new_expl:
|
||||||
return '\n~'.join(new_expl)
|
return '\n~'.join(new_expl)
|
||||||
|
m = monkeypatch()
|
||||||
|
config._cleanup.append(m.undo)
|
||||||
m.setattr(py.builtin.builtins, 'AssertionError',
|
m.setattr(py.builtin.builtins, 'AssertionError',
|
||||||
reinterpret.AssertionError)
|
reinterpret.AssertionError)
|
||||||
m.setattr(util, '_reprcompare', callbinrepr)
|
m.setattr(util, '_reprcompare', callbinrepr)
|
||||||
else:
|
if mode != "on":
|
||||||
rewrite_asserts = None
|
rewrite_asserts = None
|
||||||
|
|
||||||
def _write_pyc(co, source_path):
|
def _write_pyc(co, source_path):
|
||||||
|
|
|
@ -16,9 +16,6 @@ def pytest_addoption(parser):
|
||||||
group.addoption('--traceconfig',
|
group.addoption('--traceconfig',
|
||||||
action="store_true", dest="traceconfig", default=False,
|
action="store_true", dest="traceconfig", default=False,
|
||||||
help="trace considerations of conftest.py files."),
|
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',
|
group.addoption('--debug',
|
||||||
action="store_true", dest="debug", default=False,
|
action="store_true", dest="debug", default=False,
|
||||||
help="generate and show internal debugging information.")
|
help="generate and show internal debugging information.")
|
||||||
|
|
|
@ -160,7 +160,7 @@ def test_sequence_comparison_uses_repr(testdir):
|
||||||
])
|
])
|
||||||
|
|
||||||
|
|
||||||
def test_functional(testdir):
|
def test_assertion_options(testdir):
|
||||||
testdir.makepyfile("""
|
testdir.makepyfile("""
|
||||||
def test_hello():
|
def test_hello():
|
||||||
x = 3
|
x = 3
|
||||||
|
@ -168,8 +168,30 @@ def test_functional(testdir):
|
||||||
""")
|
""")
|
||||||
result = testdir.runpytest()
|
result = testdir.runpytest()
|
||||||
assert "3 == 4" in result.stdout.str()
|
assert "3 == 4" in result.stdout.str()
|
||||||
result = testdir.runpytest("--no-assert")
|
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()
|
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):
|
def test_triple_quoted_string_issue113(testdir):
|
||||||
testdir.makepyfile("""
|
testdir.makepyfile("""
|
||||||
|
|
Loading…
Reference in New Issue