2010-09-18 20:03:28 +08:00
|
|
|
import sys
|
|
|
|
|
2010-11-18 05:12:16 +08:00
|
|
|
import py, pytest
|
2010-11-13 18:10:45 +08:00
|
|
|
import _pytest.assertion as plugin
|
2011-05-27 01:01:34 +08:00
|
|
|
from _pytest.assertion import reinterpret, util
|
2010-09-09 05:21:52 +08:00
|
|
|
|
2010-11-18 05:12:16 +08:00
|
|
|
needsnewassert = pytest.mark.skipif("sys.version_info < (2,6)")
|
2010-10-03 00:47:39 +08:00
|
|
|
|
|
|
|
def interpret(expr):
|
2011-05-26 06:54:02 +08:00
|
|
|
return reinterpret.reinterpret(expr, py.code.Frame(sys._getframe(1)))
|
2010-10-03 00:47:39 +08:00
|
|
|
|
|
|
|
class TestBinReprIntegration:
|
|
|
|
pytestmark = needsnewassert
|
|
|
|
|
|
|
|
def pytest_funcarg__hook(self, request):
|
|
|
|
class MockHook(object):
|
|
|
|
def __init__(self):
|
|
|
|
self.called = False
|
|
|
|
self.args = tuple()
|
|
|
|
self.kwargs = dict()
|
|
|
|
|
|
|
|
def __call__(self, op, left, right):
|
|
|
|
self.called = True
|
|
|
|
self.op = op
|
|
|
|
self.left = left
|
|
|
|
self.right = right
|
|
|
|
mockhook = MockHook()
|
|
|
|
monkeypatch = request.getfuncargvalue("monkeypatch")
|
2011-05-27 01:01:34 +08:00
|
|
|
monkeypatch.setattr(util, '_reprcompare', mockhook)
|
2010-10-03 00:47:39 +08:00
|
|
|
return mockhook
|
|
|
|
|
2010-10-03 01:00:47 +08:00
|
|
|
def test_pytest_assertrepr_compare_called(self, hook):
|
2010-10-03 00:47:39 +08:00
|
|
|
interpret('assert 0 == 1')
|
|
|
|
assert hook.called
|
|
|
|
|
|
|
|
|
2010-10-03 01:00:47 +08:00
|
|
|
def test_pytest_assertrepr_compare_args(self, hook):
|
2010-10-03 00:47:39 +08:00
|
|
|
interpret('assert [0, 1] == [0, 2]')
|
|
|
|
assert hook.op == '=='
|
|
|
|
assert hook.left == [0, 1]
|
|
|
|
assert hook.right == [0, 2]
|
|
|
|
|
|
|
|
def test_configure_unconfigure(self, testdir, hook):
|
2011-05-27 01:01:34 +08:00
|
|
|
assert hook == util._reprcompare
|
2010-10-03 00:47:39 +08:00
|
|
|
config = testdir.parseconfig()
|
|
|
|
plugin.pytest_configure(config)
|
2011-05-27 01:01:34 +08:00
|
|
|
assert hook != util._reprcompare
|
2011-04-17 18:20:13 +08:00
|
|
|
from _pytest.config import pytest_unconfigure
|
|
|
|
pytest_unconfigure(config)
|
2011-05-27 01:01:34 +08:00
|
|
|
assert hook == util._reprcompare
|
2010-10-03 00:47:39 +08:00
|
|
|
|
2010-10-05 00:49:30 +08:00
|
|
|
def callequal(left, right):
|
|
|
|
return plugin.pytest_assertrepr_compare('==', left, right)
|
|
|
|
|
2010-10-03 01:00:47 +08:00
|
|
|
class TestAssert_reprcompare:
|
2010-10-03 00:47:39 +08:00
|
|
|
def test_different_types(self):
|
2010-10-05 00:49:30 +08:00
|
|
|
assert callequal([0, 1], 'foo') is None
|
2010-10-03 00:47:39 +08:00
|
|
|
|
|
|
|
def test_summary(self):
|
2010-10-05 00:49:30 +08:00
|
|
|
summary = callequal([0, 1], [0, 2])[0]
|
2010-10-03 00:47:39 +08:00
|
|
|
assert len(summary) < 65
|
|
|
|
|
|
|
|
def test_text_diff(self):
|
2010-10-05 00:49:30 +08:00
|
|
|
diff = callequal('spam', 'eggs')[1:]
|
2010-10-03 00:47:39 +08:00
|
|
|
assert '- spam' in diff
|
|
|
|
assert '+ eggs' in diff
|
|
|
|
|
|
|
|
def test_multiline_text_diff(self):
|
|
|
|
left = 'foo\nspam\nbar'
|
|
|
|
right = 'foo\neggs\nbar'
|
2010-10-05 00:49:30 +08:00
|
|
|
diff = callequal(left, right)
|
2010-10-03 00:47:39 +08:00
|
|
|
assert '- spam' in diff
|
|
|
|
assert '+ eggs' in diff
|
2010-09-09 05:21:52 +08:00
|
|
|
|
2010-10-03 00:47:39 +08:00
|
|
|
def test_list(self):
|
2010-10-05 00:49:30 +08:00
|
|
|
expl = callequal([0, 1], [0, 2])
|
2010-10-03 00:47:39 +08:00
|
|
|
assert len(expl) > 1
|
2010-09-18 20:03:28 +08:00
|
|
|
|
2010-10-03 00:47:39 +08:00
|
|
|
def test_list_different_lenghts(self):
|
2010-10-05 00:49:30 +08:00
|
|
|
expl = callequal([0, 1], [0, 1, 2])
|
2010-10-03 00:47:39 +08:00
|
|
|
assert len(expl) > 1
|
2010-10-05 00:49:30 +08:00
|
|
|
expl = callequal([0, 1, 2], [0, 1])
|
2010-10-03 00:47:39 +08:00
|
|
|
assert len(expl) > 1
|
2010-09-18 20:03:28 +08:00
|
|
|
|
2010-10-03 00:47:39 +08:00
|
|
|
def test_dict(self):
|
2010-10-05 00:49:30 +08:00
|
|
|
expl = callequal({'a': 0}, {'a': 1})
|
2010-10-03 00:47:39 +08:00
|
|
|
assert len(expl) > 1
|
2010-09-18 20:03:28 +08:00
|
|
|
|
2010-10-03 00:47:39 +08:00
|
|
|
def test_set(self):
|
2010-10-05 00:49:30 +08:00
|
|
|
expl = callequal(set([0, 1]), set([0, 2]))
|
|
|
|
assert len(expl) > 1
|
|
|
|
|
|
|
|
def test_list_tuples(self):
|
|
|
|
expl = callequal([], [(1,2)])
|
2010-10-03 00:47:39 +08:00
|
|
|
assert len(expl) > 1
|
2010-10-05 00:49:30 +08:00
|
|
|
expl = callequal([(1,2)], [])
|
|
|
|
assert len(expl) > 1
|
|
|
|
|
|
|
|
def test_list_bad_repr(self):
|
|
|
|
class A:
|
|
|
|
def __repr__(self):
|
|
|
|
raise ValueError(42)
|
|
|
|
expl = callequal([], [A()])
|
|
|
|
assert 'ValueError' in "".join(expl)
|
|
|
|
expl = callequal({}, {'1': A()})
|
|
|
|
assert 'faulty' in "".join(expl)
|
2010-09-18 20:03:28 +08:00
|
|
|
|
2010-10-09 13:35:28 +08:00
|
|
|
def test_one_repr_empty(self):
|
|
|
|
"""
|
|
|
|
the faulty empty string repr did trigger
|
|
|
|
a unbound local error in _diff_text
|
|
|
|
"""
|
|
|
|
class A(str):
|
|
|
|
def __repr__(self):
|
|
|
|
return ''
|
|
|
|
expl = callequal(A(), '')
|
|
|
|
assert not expl
|
|
|
|
|
2011-02-16 07:24:18 +08:00
|
|
|
def test_repr_no_exc(self):
|
|
|
|
expl = ' '.join(callequal('foo', 'bar'))
|
|
|
|
assert 'raised in repr()' not in expl
|
|
|
|
|
2011-07-05 21:21:08 +08:00
|
|
|
@needsnewassert
|
2011-07-04 08:28:48 +08:00
|
|
|
def test_rewritten(testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
def test_rewritten():
|
|
|
|
assert "@py_builtins" in globals()
|
|
|
|
""")
|
|
|
|
assert testdir.runpytest().ret == 0
|
2011-05-27 09:06:11 +08:00
|
|
|
|
2010-12-10 09:03:26 +08:00
|
|
|
def test_reprcompare_notin():
|
|
|
|
detail = plugin.pytest_assertrepr_compare('not in', 'foo', 'aaafoobbb')[1:]
|
2011-01-27 18:36:12 +08:00
|
|
|
assert detail == ["'foo' is contained here:", ' aaafoobbb', '? +++']
|
2010-12-10 09:03:26 +08:00
|
|
|
|
2010-10-03 00:47:39 +08:00
|
|
|
@needsnewassert
|
2010-10-03 01:00:47 +08:00
|
|
|
def test_pytest_assertrepr_compare_integration(testdir):
|
2010-10-03 00:47:39 +08:00
|
|
|
testdir.makepyfile("""
|
|
|
|
def test_hello():
|
|
|
|
x = set(range(100))
|
|
|
|
y = x.copy()
|
|
|
|
y.remove(50)
|
|
|
|
assert x == y
|
|
|
|
""")
|
|
|
|
result = testdir.runpytest()
|
|
|
|
result.stdout.fnmatch_lines([
|
|
|
|
"*def test_hello():*",
|
|
|
|
"*assert x == y*",
|
|
|
|
"*E*Extra items*left*",
|
|
|
|
"*E*50*",
|
|
|
|
])
|
2010-09-18 20:03:28 +08:00
|
|
|
|
2010-10-05 00:49:30 +08:00
|
|
|
@needsnewassert
|
|
|
|
def test_sequence_comparison_uses_repr(testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
def test_hello():
|
|
|
|
x = set("hello x")
|
|
|
|
y = set("hello y")
|
|
|
|
assert x == y
|
|
|
|
""")
|
|
|
|
result = testdir.runpytest()
|
|
|
|
result.stdout.fnmatch_lines([
|
|
|
|
"*def test_hello():*",
|
|
|
|
"*assert x == y*",
|
|
|
|
"*E*Extra items*left*",
|
|
|
|
"*E*'x'*",
|
|
|
|
"*E*Extra items*right*",
|
|
|
|
"*E*'y'*",
|
|
|
|
])
|
|
|
|
|
|
|
|
|
2011-05-27 03:34:27 +08:00
|
|
|
def test_assertion_options(testdir):
|
2009-09-06 22:59:39 +08:00
|
|
|
testdir.makepyfile("""
|
|
|
|
def test_hello():
|
|
|
|
x = 3
|
|
|
|
assert x == 4
|
|
|
|
""")
|
|
|
|
result = testdir.runpytest()
|
2010-07-27 03:15:15 +08:00
|
|
|
assert "3 == 4" in result.stdout.str()
|
2011-05-27 03:34:27 +08:00
|
|
|
off_options = (("--no-assert",),
|
|
|
|
("--nomagic",),
|
|
|
|
("--no-assert", "--nomagic"),
|
2011-07-05 23:29:53 +08:00
|
|
|
("--assert=plain",),
|
|
|
|
("--assert=plain", "--no-assert"),
|
|
|
|
("--assert=plain", "--nomagic"),
|
|
|
|
("--assert=plain", "--no-assert", "--nomagic"))
|
2011-05-27 03:34:27 +08:00
|
|
|
for opt in off_options:
|
|
|
|
result = testdir.runpytest(*opt)
|
|
|
|
assert "3 == 4" not in result.stdout.str()
|
|
|
|
|
|
|
|
def test_old_assert_mode(testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
def test_in_old_mode():
|
|
|
|
assert "@py_builtins" not in globals()
|
|
|
|
""")
|
2011-07-05 23:29:53 +08:00
|
|
|
result = testdir.runpytest("--assert=reinterp")
|
2011-05-27 03:34:27 +08:00
|
|
|
assert result.ret == 0
|
2009-09-06 22:59:39 +08:00
|
|
|
|
2010-07-29 18:55:39 +08:00
|
|
|
def test_triple_quoted_string_issue113(testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
def test_hello():
|
|
|
|
assert "" == '''
|
|
|
|
'''""")
|
|
|
|
result = testdir.runpytest("--fulltrace")
|
|
|
|
result.stdout.fnmatch_lines([
|
|
|
|
"*1 failed*",
|
|
|
|
])
|
|
|
|
assert 'SyntaxError' not in result.stdout.str()
|
|
|
|
|
2009-09-06 22:59:39 +08:00
|
|
|
def test_traceback_failure(testdir):
|
|
|
|
p1 = testdir.makepyfile("""
|
|
|
|
def g():
|
|
|
|
return 2
|
|
|
|
def f(x):
|
|
|
|
assert x == g()
|
|
|
|
def test_onefails():
|
|
|
|
f(3)
|
|
|
|
""")
|
|
|
|
result = testdir.runpytest(p1)
|
|
|
|
result.stdout.fnmatch_lines([
|
2010-07-27 03:15:15 +08:00
|
|
|
"*test_traceback_failure.py F",
|
2009-09-06 22:59:39 +08:00
|
|
|
"====* FAILURES *====",
|
2010-07-27 03:15:15 +08:00
|
|
|
"____*____",
|
2009-09-06 22:59:39 +08:00
|
|
|
"",
|
|
|
|
" def test_onefails():",
|
|
|
|
"> f(3)",
|
|
|
|
"",
|
|
|
|
"*test_*.py:6: ",
|
|
|
|
"_ _ _ *",
|
|
|
|
#"",
|
|
|
|
" def f(x):",
|
|
|
|
"> assert x == g()",
|
|
|
|
"E assert 3 == 2",
|
|
|
|
"E + where 2 = g()",
|
|
|
|
"",
|
|
|
|
"*test_traceback_failure.py:4: AssertionError"
|
|
|
|
])
|
|
|
|
|
2011-09-21 14:12:37 +08:00
|
|
|
@pytest.mark.skipif("sys.version_info < (2,5) or '__pypy__' in sys.builtin_module_names or sys.platform.startswith('java')" )
|
2010-12-09 18:00:31 +08:00
|
|
|
def test_warn_missing(testdir):
|
|
|
|
p1 = testdir.makepyfile("")
|
|
|
|
result = testdir.run(sys.executable, "-OO", "-m", "pytest", "-h")
|
|
|
|
result.stderr.fnmatch_lines([
|
2011-07-05 23:29:53 +08:00
|
|
|
"*WARNING*assert statements are not executed*",
|
2010-12-09 18:00:31 +08:00
|
|
|
])
|
|
|
|
result = testdir.run(sys.executable, "-OO", "-m", "pytest", "--no-assert")
|
|
|
|
result.stderr.fnmatch_lines([
|
2011-07-05 23:29:53 +08:00
|
|
|
"*WARNING*assert statements are not executed*",
|
2010-12-09 18:00:31 +08:00
|
|
|
])
|