rename and simplify the assert option:

cmdline usage is now: --assert=rewrite/reinterp/plain
there is no conflict detection (don't think that's neccessary)
This commit is contained in:
holger krekel 2011-07-05 17:29:53 +02:00
parent 407ca5b120
commit 46950ef19a
5 changed files with 32 additions and 41 deletions

View File

@ -1,2 +1,2 @@
# #
__version__ = '2.1.0.dev5' __version__ = '2.1.0.dev6'

View File

@ -9,18 +9,18 @@ from _pytest.assertion import util
def pytest_addoption(parser): def pytest_addoption(parser):
group = parser.getgroup("debugconfig") group = parser.getgroup("debugconfig")
group.addoption('--assertmode', action="store", dest="assertmode", group.addoption('--assert', action="store", dest="assertmode",
choices=("rewrite", "reinterp", "off", "default"), choices=("rewrite", "reinterp", "plain",),
default="default", metavar="off|reinterp|rewrite", default="rewrite", metavar="MODE",
help="""control assertion debugging tools. help="""control assertion debugging tools.
'off' performs no assertion debugging. 'plain' performs no assertion debugging.
'reinterp' reinterprets the expressions in asserts to glean information. 'reinterp' reinterprets assert statements after they failed to provide assertion expression information.
'rewrite' (the default) rewrites the assert statements in test modules on import 'rewrite' (the default) rewrites assert statements in test modules on import
to provide sub-expression results.""") to provide assert expression information. """)
group.addoption('--no-assert', action="store_true", default=False, group.addoption('--no-assert', action="store_true", default=False,
dest="noassert", help="DEPRECATED equivalent to --assertmode=off") dest="noassert", help="DEPRECATED equivalent to --assert=plain")
group.addoption('--nomagic', action="store_true", default=False, group.addoption('--nomagic', action="store_true", default=False,
dest="nomagic", help="DEPRECATED equivalent to --assertmode=off") dest="nomagic", help="DEPRECATED equivalent to --assert=plain")
class AssertionState: class AssertionState:
"""State for the assertion plugin.""" """State for the assertion plugin."""
@ -33,17 +33,13 @@ class AssertionState:
def pytest_configure(config): def pytest_configure(config):
mode = config.getvalue("assertmode") mode = config.getvalue("assertmode")
if config.getvalue("noassert") or config.getvalue("nomagic"): if config.getvalue("noassert") or config.getvalue("nomagic"):
if mode not in ("off", "default"): mode = "plain"
raise pytest.UsageError("assertion options conflict")
mode = "off"
elif mode == "default":
mode = "rewrite"
if mode == "rewrite": if mode == "rewrite":
try: try:
import ast import ast
except ImportError: except ImportError:
mode = "reinterp" mode = "reinterp"
if mode != "off": if mode != "plain":
_load_modules(mode) _load_modules(mode)
def callbinrepr(op, left, right): def callbinrepr(op, left, right):
hook_result = config.hook.pytest_assertrepr_compare( hook_result = config.hook.pytest_assertrepr_compare(
@ -104,7 +100,8 @@ def warn_about_missing_assertion(mode):
specifically = "failing tests may report as passing" specifically = "failing tests may report as passing"
sys.stderr.write("WARNING: " + specifically + sys.stderr.write("WARNING: " + specifically +
" because assertions are turned off " " because assert statements are not executed "
"by the underlying Python interpreter "
"(are you using python -O?)\n") "(are you using python -O?)\n")
pytest_assertrepr_compare = util.assertrepr_compare pytest_assertrepr_compare = util.assertrepr_compare

View File

@ -23,7 +23,7 @@ you will see the return value of the function call::
$ py.test test_assert1.py $ py.test test_assert1.py
============================= test session starts ============================== ============================= test session starts ==============================
platform darwin -- Python 2.7.0 -- pytest-2.1.0.dev4 platform linux2 -- Python 2.6.6 -- pytest-2.1.0.dev6
collecting ... collected 1 items collecting ... collected 1 items
test_assert1.py F test_assert1.py F
@ -37,7 +37,7 @@ you will see the return value of the function call::
E + where 3 = f() E + where 3 = f()
test_assert1.py:5: AssertionError test_assert1.py:5: AssertionError
=========================== 1 failed in 0.05 seconds =========================== =========================== 1 failed in 0.01 seconds ===========================
py.test has support for showing the values of the most common subexpressions py.test has support for showing the values of the most common subexpressions
including calls, attributes, comparisons, and binary and unary including calls, attributes, comparisons, and binary and unary
@ -105,7 +105,7 @@ if you run this module::
$ py.test test_assert2.py $ py.test test_assert2.py
============================= test session starts ============================== ============================= test session starts ==============================
platform darwin -- Python 2.7.0 -- pytest-2.1.0.dev4 platform linux2 -- Python 2.6.6 -- pytest-2.1.0.dev6
collecting ... collected 1 items collecting ... collected 1 items
test_assert2.py F test_assert2.py F
@ -124,7 +124,7 @@ if you run this module::
E '5' E '5'
test_assert2.py:5: AssertionError test_assert2.py:5: AssertionError
=========================== 1 failed in 0.05 seconds =========================== =========================== 1 failed in 0.01 seconds ===========================
Special comparisons are done for a number of cases: Special comparisons are done for a number of cases:
@ -181,7 +181,7 @@ the conftest file::
E vals: 1 != 2 E vals: 1 != 2
test_foocompare.py:8: AssertionError test_foocompare.py:8: AssertionError
1 failed in 0.05 seconds 1 failed in 0.01 seconds
.. _assert-details: .. _assert-details:
.. _`assert introspection`: .. _`assert introspection`:
@ -212,8 +212,8 @@ rewritten.
py.test rewrites test modules on import. It does this by using an import hook py.test rewrites test modules on import. It does this by using an import hook
to write a new pyc files. Most of the time this works transparently. However, to write a new pyc files. Most of the time this works transparently. However,
if you are messing with import yourself, the import hook may interfere. If if you are messing with import yourself, the import hook may interfere. If
this is the case, simply use ``--assertmode=reinterp`` or this is the case, simply use ``--assert=reinterp`` or
``--assertmode=off``. Additionally, rewriting will fail silently if it cannot ``--assert=plain``. Additionally, rewriting will fail silently if it cannot
write new pycs, i.e. in a read-only filesystem or a zipfile. write new pycs, i.e. in a read-only filesystem or a zipfile.
If an assert statement has not been rewritten or the Python version is less than If an assert statement has not been rewritten or the Python version is less than
@ -221,7 +221,7 @@ If an assert statement has not been rewritten or the Python version is less than
py.test walks the frame of the function containing the assert statement to py.test walks the frame of the function containing the assert statement to
discover sub-expression results of the failing assert statement. You can force discover sub-expression results of the failing assert statement. You can force
py.test to always use assertion reinterpretation by passing the py.test to always use assertion reinterpretation by passing the
``--assertmode=reinterp`` option. ``--assert=reinterp`` option.
Assert reinterpretation has a caveat not present with assert rewriting: If Assert reinterpretation has a caveat not present with assert rewriting: If
evaluating the assert expression has side effects you may get a warning that the evaluating the assert expression has side effects you may get a warning that the
@ -238,11 +238,11 @@ easy to rewrite the assertion and avoid any trouble::
content = f.read() content = f.read()
assert content != '...' assert content != '...'
All assert introspection can be turned off by passing ``--assertmode=off``. All assert introspection can be turned off by passing ``--assert=plain``.
.. versionadded:: 2.1 .. versionadded:: 2.1
Add assert rewriting as an alternate introspection technique. Add assert rewriting as an alternate introspection technique.
.. versionchanged:: 2.1 .. versionchanged:: 2.1
Introduce the ``--assertmode`` option. Deprecate ``--no-assert`` and Introduce the ``--assert`` option. Deprecate ``--no-assert`` and
``--nomagic``. ``--nomagic``.

View File

@ -22,7 +22,7 @@ def main():
name='pytest', name='pytest',
description='py.test: simple powerful testing with Python', description='py.test: simple powerful testing with Python',
long_description = long_description, long_description = long_description,
version='2.1.0.dev5', version='2.1.0.dev6',
url='http://pytest.org', url='http://pytest.org',
license='MIT license', license='MIT license',
platforms=['unix', 'linux', 'osx', 'cygwin', 'win32'], platforms=['unix', 'linux', 'osx', 'cygwin', 'win32'],

View File

@ -179,26 +179,20 @@ def test_assertion_options(testdir):
off_options = (("--no-assert",), off_options = (("--no-assert",),
("--nomagic",), ("--nomagic",),
("--no-assert", "--nomagic"), ("--no-assert", "--nomagic"),
("--assertmode=off",), ("--assert=plain",),
("--assertmode=off", "--no-assert"), ("--assert=plain", "--no-assert"),
("--assertmode=off", "--nomagic"), ("--assert=plain", "--nomagic"),
("--assertmode=off," "--no-assert", "--nomagic")) ("--assert=plain", "--no-assert", "--nomagic"))
for opt in off_options: for opt in off_options:
result = testdir.runpytest(*opt) result = testdir.runpytest(*opt)
assert "3 == 4" not in result.stdout.str() assert "3 == 4" not in result.stdout.str()
for mode in "rewrite", "reinterp":
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): def test_old_assert_mode(testdir):
testdir.makepyfile(""" testdir.makepyfile("""
def test_in_old_mode(): def test_in_old_mode():
assert "@py_builtins" not in globals() assert "@py_builtins" not in globals()
""") """)
result = testdir.runpytest("--assertmode=reinterp") result = testdir.runpytest("--assert=reinterp")
assert result.ret == 0 assert result.ret == 0
def test_triple_quoted_string_issue113(testdir): def test_triple_quoted_string_issue113(testdir):
@ -246,11 +240,11 @@ def test_warn_missing(testdir):
p1 = testdir.makepyfile("") p1 = testdir.makepyfile("")
result = testdir.run(sys.executable, "-OO", "-m", "pytest", "-h") result = testdir.run(sys.executable, "-OO", "-m", "pytest", "-h")
result.stderr.fnmatch_lines([ result.stderr.fnmatch_lines([
"*WARNING*assertion*", "*WARNING*assert statements are not executed*",
]) ])
result = testdir.run(sys.executable, "-OO", "-m", "pytest", "--no-assert") result = testdir.run(sys.executable, "-OO", "-m", "pytest", "--no-assert")
result.stderr.fnmatch_lines([ result.stderr.fnmatch_lines([
"*WARNING*assertion*", "*WARNING*assert statements are not executed*",
]) ])
def test_load_fake_pyc(testdir): def test_load_fake_pyc(testdir):