From d438a0bd83f562d78590934ab424431503a53905 Mon Sep 17 00:00:00 2001 From: Benjamin Peterson Date: Thu, 26 May 2011 14:34:27 -0500 Subject: [PATCH] introduce --assertmode option --- _pytest/assertion/__init__.py | 27 +++++++++++++++++++++------ _pytest/helpconfig.py | 3 --- testing/test_assertion.py | 28 +++++++++++++++++++++++++--- 3 files changed, 46 insertions(+), 12 deletions(-) diff --git a/_pytest/assertion/__init__.py b/_pytest/assertion/__init__.py index 41534db02..cfe978804 100644 --- a/_pytest/assertion/__init__.py +++ b/_pytest/assertion/__init__.py @@ -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): diff --git a/_pytest/helpconfig.py b/_pytest/helpconfig.py index b89b33b56..fa81f87e9 100644 --- a/_pytest/helpconfig.py +++ b/_pytest/helpconfig.py @@ -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.") diff --git a/testing/test_assertion.py b/testing/test_assertion.py index 24b665066..beb74b9ad 100644 --- a/testing/test_assertion.py +++ b/testing/test_assertion.py @@ -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("""