2013-11-29 08:29:14 +08:00
|
|
|
# -*- coding: utf-8 -*-
|
2010-09-18 20:03:28 +08:00
|
|
|
import sys
|
2014-09-27 09:29:47 +08:00
|
|
|
import textwrap
|
2010-09-18 20:03:28 +08:00
|
|
|
|
2010-11-18 05:12:16 +08:00
|
|
|
import py, pytest
|
2010-11-13 18:10:45 +08:00
|
|
|
import _pytest.assertion as plugin
|
2013-10-12 21:39:22 +08:00
|
|
|
from _pytest.assertion import reinterpret
|
2014-08-24 00:14:25 +08:00
|
|
|
from _pytest.assertion import util
|
2014-09-27 09:29:47 +08:00
|
|
|
|
2010-11-18 05:12:16 +08:00
|
|
|
needsnewassert = pytest.mark.skipif("sys.version_info < (2,6)")
|
2014-09-27 09:29:47 +08:00
|
|
|
PY3 = sys.version_info >= (3, 0)
|
2010-10-03 00:47:39 +08:00
|
|
|
|
2013-03-28 09:39:01 +08:00
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def mock_config():
|
|
|
|
class Config(object):
|
|
|
|
verbose = False
|
|
|
|
def getoption(self, name):
|
|
|
|
if name == 'verbose':
|
|
|
|
return self.verbose
|
|
|
|
raise KeyError('Not mocked out: %s' % name)
|
|
|
|
return Config()
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2012-06-25 23:35:33 +08:00
|
|
|
def test_pytest_assertrepr_compare_called(self, testdir):
|
|
|
|
testdir.makeconftest("""
|
|
|
|
l = []
|
|
|
|
def pytest_assertrepr_compare(op, left, right):
|
|
|
|
l.append((op, left, right))
|
|
|
|
def pytest_funcarg__l(request):
|
|
|
|
return l
|
|
|
|
""")
|
|
|
|
testdir.makepyfile("""
|
|
|
|
def test_hello():
|
|
|
|
assert 0 == 1
|
|
|
|
def test_check(l):
|
|
|
|
assert l == [("==", 0, 1)]
|
|
|
|
""")
|
|
|
|
result = testdir.runpytest("-v")
|
|
|
|
result.stdout.fnmatch_lines([
|
|
|
|
"*test_hello*FAIL*",
|
|
|
|
"*test_check*PASS*",
|
|
|
|
])
|
2010-10-03 00:47:39 +08:00
|
|
|
|
2013-03-28 09:39:01 +08:00
|
|
|
def callequal(left, right, verbose=False):
|
|
|
|
config = mock_config()
|
|
|
|
config.verbose = verbose
|
|
|
|
return plugin.pytest_assertrepr_compare(config, '==', left, right)
|
|
|
|
|
2010-10-05 00:49:30 +08:00
|
|
|
|
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
|
|
|
|
|
2013-03-28 09:39:01 +08:00
|
|
|
def test_text_skipping(self):
|
|
|
|
lines = callequal('a'*50 + 'spam', 'a'*50 + 'eggs')
|
|
|
|
assert 'Skipping' in lines[1]
|
|
|
|
for line in lines:
|
|
|
|
assert 'a'*50 not in line
|
|
|
|
|
|
|
|
def test_text_skipping_verbose(self):
|
|
|
|
lines = callequal('a'*50 + 'spam', 'a'*50 + 'eggs', verbose=True)
|
|
|
|
assert '- ' + 'a'*50 + 'spam' in lines
|
|
|
|
assert '+ ' + 'a'*50 + 'eggs' in lines
|
|
|
|
|
2010-10-03 00:47:39 +08:00
|
|
|
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
|
|
|
|
2014-09-27 09:29:47 +08:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
['left', 'right', 'expected'], [
|
|
|
|
([0, 1], [0, 2], """
|
|
|
|
Full diff:
|
|
|
|
- [0, 1]
|
|
|
|
? ^
|
|
|
|
+ [0, 2]
|
|
|
|
? ^
|
|
|
|
"""),
|
|
|
|
({0: 1}, {0: 2}, """
|
|
|
|
Full diff:
|
|
|
|
- {0: 1}
|
|
|
|
? ^
|
|
|
|
+ {0: 2}
|
|
|
|
? ^
|
|
|
|
"""),
|
|
|
|
(set([0, 1]), set([0, 2]), """
|
|
|
|
Full diff:
|
|
|
|
- set([0, 1])
|
|
|
|
? ^
|
|
|
|
+ set([0, 2])
|
|
|
|
? ^
|
|
|
|
""" if not PY3 else """
|
|
|
|
Full diff:
|
|
|
|
- {0, 1}
|
|
|
|
? ^
|
|
|
|
+ {0, 2}
|
|
|
|
? ^
|
|
|
|
""")
|
|
|
|
]
|
|
|
|
)
|
|
|
|
def test_iterable_full_diff(self, left, right, expected):
|
|
|
|
"""Test the full diff assertion failure explanation.
|
|
|
|
|
|
|
|
When verbose is False, then just a -v notice to get the diff is rendered,
|
|
|
|
when verbose is True, then ndiff of the pprint is returned.
|
|
|
|
"""
|
|
|
|
expl = callequal(left, right, verbose=False)
|
|
|
|
assert expl[-1] == 'Use -v to get the full diff'
|
|
|
|
expl = '\n'.join(callequal(left, right, verbose=True))
|
|
|
|
assert expl.endswith(textwrap.dedent(expected).strip())
|
|
|
|
|
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
|
|
|
|
2013-08-01 20:48:34 +08:00
|
|
|
def test_dict_omitting(self):
|
|
|
|
lines = callequal({'a': 0, 'b': 1}, {'a': 1, 'b': 1})
|
|
|
|
assert lines[1].startswith('Omitting 1 identical item')
|
|
|
|
assert 'Common items' not in lines
|
|
|
|
for line in lines[1:]:
|
|
|
|
assert 'b' not in line
|
|
|
|
|
|
|
|
def test_dict_omitting_verbose(self):
|
|
|
|
lines = callequal({'a': 0, 'b': 1}, {'a': 1, 'b': 1}, verbose=True)
|
|
|
|
assert lines[1].startswith('Common items:')
|
|
|
|
assert 'Omitting' not in lines[1]
|
|
|
|
assert lines[2] == "{'b': 1}"
|
|
|
|
|
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
|
|
|
|
|
2013-04-29 03:59:10 +08:00
|
|
|
def test_frozenzet(self):
|
|
|
|
expl = callequal(frozenset([0, 1]), set([0, 2]))
|
2013-08-01 20:48:34 +08:00
|
|
|
assert len(expl) > 1
|
|
|
|
|
|
|
|
def test_Sequence(self):
|
2013-08-01 21:38:03 +08:00
|
|
|
col = py.builtin._tryimport(
|
|
|
|
"collections.abc",
|
|
|
|
"collections",
|
|
|
|
"sys")
|
|
|
|
if not hasattr(col, "MutableSequence"):
|
|
|
|
pytest.skip("cannot import MutableSequence")
|
|
|
|
MutableSequence = col.MutableSequence
|
|
|
|
|
2013-08-01 20:48:34 +08:00
|
|
|
class TestSequence(MutableSequence): # works with a Sequence subclass
|
|
|
|
def __init__(self, iterable):
|
|
|
|
self.elements = list(iterable)
|
|
|
|
|
|
|
|
def __getitem__(self, item):
|
|
|
|
return self.elements[item]
|
|
|
|
|
|
|
|
def __len__(self):
|
|
|
|
return len(self.elements)
|
|
|
|
|
|
|
|
def __setitem__(self, item, value):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def __delitem__(self, item):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def insert(self, item, index):
|
|
|
|
pass
|
|
|
|
|
|
|
|
expl = callequal(TestSequence([0, 1]), list([0, 2]))
|
2013-04-29 03:59:10 +08:00
|
|
|
assert len(expl) > 1
|
|
|
|
|
2010-10-05 00:49:30 +08:00
|
|
|
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
|
|
|
|
|
2013-11-29 08:29:14 +08:00
|
|
|
def test_unicode(self):
|
|
|
|
left = py.builtin._totext('£€', 'utf-8')
|
|
|
|
right = py.builtin._totext('£', 'utf-8')
|
|
|
|
expl = callequal(left, right)
|
|
|
|
assert expl[0] == py.builtin._totext("'£€' == '£'", 'utf-8')
|
|
|
|
assert expl[1] == py.builtin._totext('- £€', 'utf-8')
|
|
|
|
assert expl[2] == py.builtin._totext('+ £', 'utf-8')
|
|
|
|
|
2014-01-29 08:42:58 +08:00
|
|
|
def test_mojibake(self):
|
|
|
|
# issue 429
|
|
|
|
left = 'e'
|
|
|
|
right = '\xc3\xa9'
|
|
|
|
if not isinstance(left, py.builtin.bytes):
|
|
|
|
left = py.builtin.bytes(left, 'utf-8')
|
|
|
|
right = py.builtin.bytes(right, 'utf-8')
|
|
|
|
expl = callequal(left, right)
|
|
|
|
for line in expl:
|
|
|
|
assert isinstance(line, py.builtin.text)
|
|
|
|
msg = py.builtin._totext('\n').join(expl)
|
|
|
|
assert msg
|
|
|
|
|
2013-11-29 08:29:14 +08:00
|
|
|
|
2014-04-03 00:35:22 +08:00
|
|
|
class TestFormatExplanation:
|
|
|
|
|
2014-08-24 00:14:25 +08:00
|
|
|
def test_special_chars_full(self, testdir):
|
2014-04-03 00:35:22 +08:00
|
|
|
# Issue 453, for the bug this would raise IndexError
|
|
|
|
testdir.makepyfile("""
|
|
|
|
def test_foo():
|
2014-06-01 05:51:05 +08:00
|
|
|
assert '\\n}' == ''
|
2014-04-03 00:35:22 +08:00
|
|
|
""")
|
|
|
|
result = testdir.runpytest()
|
|
|
|
assert result.ret == 1
|
|
|
|
result.stdout.fnmatch_lines([
|
|
|
|
"*AssertionError*",
|
|
|
|
])
|
|
|
|
|
2014-08-24 00:14:25 +08:00
|
|
|
def test_fmt_simple(self):
|
|
|
|
expl = 'assert foo'
|
|
|
|
assert util.format_explanation(expl) == 'assert foo'
|
|
|
|
|
|
|
|
def test_fmt_where(self):
|
|
|
|
expl = '\n'.join(['assert 1',
|
|
|
|
'{1 = foo',
|
|
|
|
'} == 2'])
|
|
|
|
res = '\n'.join(['assert 1 == 2',
|
|
|
|
' + where 1 = foo'])
|
|
|
|
assert util.format_explanation(expl) == res
|
|
|
|
|
|
|
|
def test_fmt_and(self):
|
|
|
|
expl = '\n'.join(['assert 1',
|
|
|
|
'{1 = foo',
|
|
|
|
'} == 2',
|
|
|
|
'{2 = bar',
|
|
|
|
'}'])
|
|
|
|
res = '\n'.join(['assert 1 == 2',
|
|
|
|
' + where 1 = foo',
|
|
|
|
' + and 2 = bar'])
|
|
|
|
assert util.format_explanation(expl) == res
|
|
|
|
|
|
|
|
def test_fmt_where_nested(self):
|
|
|
|
expl = '\n'.join(['assert 1',
|
|
|
|
'{1 = foo',
|
|
|
|
'{foo = bar',
|
|
|
|
'}',
|
|
|
|
'} == 2'])
|
|
|
|
res = '\n'.join(['assert 1 == 2',
|
|
|
|
' + where 1 = foo',
|
|
|
|
' + where foo = bar'])
|
|
|
|
assert util.format_explanation(expl) == res
|
|
|
|
|
|
|
|
def test_fmt_newline(self):
|
|
|
|
expl = '\n'.join(['assert "foo" == "bar"',
|
|
|
|
'~- foo',
|
|
|
|
'~+ bar'])
|
|
|
|
res = '\n'.join(['assert "foo" == "bar"',
|
|
|
|
' - foo',
|
|
|
|
' + bar'])
|
|
|
|
assert util.format_explanation(expl) == res
|
|
|
|
|
|
|
|
def test_fmt_newline_escaped(self):
|
|
|
|
expl = '\n'.join(['assert foo == bar',
|
|
|
|
'baz'])
|
|
|
|
res = 'assert foo == bar\\nbaz'
|
|
|
|
assert util.format_explanation(expl) == res
|
|
|
|
|
|
|
|
def test_fmt_newline_before_where(self):
|
|
|
|
expl = '\n'.join(['the assertion message here',
|
|
|
|
'>assert 1',
|
|
|
|
'{1 = foo',
|
|
|
|
'} == 2',
|
|
|
|
'{2 = bar',
|
|
|
|
'}'])
|
|
|
|
res = '\n'.join(['the assertion message here',
|
|
|
|
'assert 1 == 2',
|
|
|
|
' + where 1 = foo',
|
|
|
|
' + and 2 = bar'])
|
|
|
|
assert util.format_explanation(expl) == res
|
|
|
|
|
|
|
|
def test_fmt_multi_newline_before_where(self):
|
|
|
|
expl = '\n'.join(['the assertion',
|
|
|
|
'~message here',
|
|
|
|
'>assert 1',
|
|
|
|
'{1 = foo',
|
|
|
|
'} == 2',
|
|
|
|
'{2 = bar',
|
|
|
|
'}'])
|
|
|
|
res = '\n'.join(['the assertion',
|
|
|
|
' message here',
|
|
|
|
'assert 1 == 2',
|
|
|
|
' + where 1 = foo',
|
|
|
|
' + and 2 = bar'])
|
|
|
|
assert util.format_explanation(expl) == res
|
|
|
|
|
2014-04-03 00:35:22 +08:00
|
|
|
|
2013-02-13 05:43:33 +08:00
|
|
|
def test_python25_compile_issue257(testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
def test_rewritten():
|
|
|
|
assert 1 == 2
|
|
|
|
# some comment
|
|
|
|
""")
|
|
|
|
result = testdir.runpytest()
|
|
|
|
assert result.ret == 1
|
|
|
|
result.stdout.fnmatch_lines("""
|
|
|
|
*E*assert 1 == 2*
|
|
|
|
*1 failed*
|
|
|
|
""")
|
|
|
|
|
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
|
|
|
|
2013-03-28 09:39:01 +08:00
|
|
|
def test_reprcompare_notin(mock_config):
|
|
|
|
detail = plugin.pytest_assertrepr_compare(
|
|
|
|
mock_config, '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'*",
|
|
|
|
])
|
|
|
|
|
2012-06-27 23:26:55 +08:00
|
|
|
|
2012-08-01 15:10:40 +08:00
|
|
|
@pytest.mark.xfail("sys.version_info < (2,6)")
|
2012-06-27 23:26:55 +08:00
|
|
|
def test_assert_compare_truncate_longmessage(testdir):
|
|
|
|
testdir.makepyfile(r"""
|
|
|
|
def test_long():
|
|
|
|
a = list(range(200))
|
|
|
|
b = a[::2]
|
|
|
|
a = '\n'.join(map(str, a))
|
|
|
|
b = '\n'.join(map(str, b))
|
|
|
|
assert a == b
|
|
|
|
""")
|
|
|
|
|
|
|
|
result = testdir.runpytest()
|
|
|
|
result.stdout.fnmatch_lines([
|
2012-11-30 19:18:12 +08:00
|
|
|
"*truncated*use*-vv*",
|
2012-06-27 23:26:55 +08:00
|
|
|
])
|
|
|
|
|
|
|
|
|
|
|
|
result = testdir.runpytest('-vv')
|
|
|
|
result.stdout.fnmatch_lines([
|
|
|
|
"*- 197",
|
|
|
|
])
|
|
|
|
|
|
|
|
|
2011-10-16 18:51:15 +08:00
|
|
|
@needsnewassert
|
|
|
|
def test_assertrepr_loaded_per_dir(testdir):
|
|
|
|
testdir.makepyfile(test_base=['def test_base(): assert 1 == 2'])
|
|
|
|
a = testdir.mkdir('a')
|
|
|
|
a_test = a.join('test_a.py')
|
|
|
|
a_test.write('def test_a(): assert 1 == 2')
|
|
|
|
a_conftest = a.join('conftest.py')
|
|
|
|
a_conftest.write('def pytest_assertrepr_compare(): return ["summary a"]')
|
|
|
|
b = testdir.mkdir('b')
|
|
|
|
b_test = b.join('test_b.py')
|
|
|
|
b_test.write('def test_b(): assert 1 == 2')
|
|
|
|
b_conftest = b.join('conftest.py')
|
|
|
|
b_conftest.write('def pytest_assertrepr_compare(): return ["summary b"]')
|
|
|
|
result = testdir.runpytest()
|
|
|
|
result.stdout.fnmatch_lines([
|
|
|
|
'*def test_base():*',
|
|
|
|
'*E*assert 1 == 2*',
|
|
|
|
'*def test_a():*',
|
|
|
|
'*E*assert summary a*',
|
|
|
|
'*def test_b():*',
|
|
|
|
'*E*assert summary b*'])
|
|
|
|
|
2010-10-05 00:49:30 +08:00
|
|
|
|
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
|
|
|
|
""")
|
2015-04-28 17:54:46 +08:00
|
|
|
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:
|
2015-04-28 17:54:46 +08:00
|
|
|
result = testdir.runpytest_subprocess(*opt)
|
2011-05-27 03:34:27 +08:00
|
|
|
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()
|
|
|
|
""")
|
2015-04-28 17:54:46 +08:00
|
|
|
result = testdir.runpytest_subprocess("--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)
|
|
|
|
""")
|
2014-06-29 19:32:53 +08:00
|
|
|
result = testdir.runpytest(p1, "--tb=long")
|
2009-09-06 22:59:39 +08:00
|
|
|
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"
|
|
|
|
])
|
|
|
|
|
2014-06-29 19:32:53 +08:00
|
|
|
result = testdir.runpytest(p1) # "auto"
|
|
|
|
result.stdout.fnmatch_lines([
|
|
|
|
"*test_traceback_failure.py F",
|
|
|
|
"====* FAILURES *====",
|
|
|
|
"____*____",
|
|
|
|
"",
|
|
|
|
" 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):
|
2013-10-12 21:39:22 +08:00
|
|
|
testdir.makepyfile("")
|
2010-12-09 18:00:31 +08:00
|
|
|
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
|
|
|
])
|
2013-04-30 18:05:58 +08:00
|
|
|
|
|
|
|
def test_recursion_source_decode(testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
def test_something():
|
|
|
|
pass
|
|
|
|
""")
|
|
|
|
testdir.makeini("""
|
|
|
|
[pytest]
|
|
|
|
python_files = *.py
|
|
|
|
""")
|
2013-08-01 23:32:19 +08:00
|
|
|
result = testdir.runpytest("--collect-only")
|
2013-04-30 18:05:58 +08:00
|
|
|
result.stdout.fnmatch_lines("""
|
|
|
|
<Module*>
|
|
|
|
""")
|
2013-12-12 13:41:48 +08:00
|
|
|
|
|
|
|
def test_AssertionError_message(testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
def test_hello():
|
|
|
|
x,y = 1,2
|
|
|
|
assert 0, (x,y)
|
|
|
|
""")
|
|
|
|
result = testdir.runpytest()
|
|
|
|
result.stdout.fnmatch_lines("""
|
|
|
|
*def test_hello*
|
|
|
|
*assert 0, (x,y)*
|
|
|
|
*AssertionError: (1, 2)*
|
|
|
|
""")
|
2015-06-18 04:31:31 +08:00
|
|
|
|
|
|
|
@pytest.mark.skipif(PY3, reason='This bug does not exist on PY3')
|
|
|
|
def test_set_with_unsortable_elements():
|
|
|
|
# issue #718
|
|
|
|
class UnsortableKey(object):
|
|
|
|
def __init__(self, name):
|
|
|
|
self.name = name
|
|
|
|
|
|
|
|
def __lt__(self, other):
|
|
|
|
raise RuntimeError()
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return 'repr({0})'.format(self.name)
|
|
|
|
|
|
|
|
def __eq__(self, other):
|
|
|
|
return self.name == other.name
|
|
|
|
|
|
|
|
def __hash__(self):
|
|
|
|
return hash(self.name)
|
|
|
|
|
|
|
|
left_set = set(UnsortableKey(str(i)) for i in range(1, 3))
|
|
|
|
right_set = set(UnsortableKey(str(i)) for i in range(2, 4))
|
|
|
|
expl = callequal(left_set, right_set, verbose=True)
|
|
|
|
# skip first line because it contains the "construction" of the set, which does not have a guaranteed order
|
|
|
|
expl = expl[1:]
|
|
|
|
dedent = textwrap.dedent("""
|
|
|
|
Extra items in the left set:
|
|
|
|
repr(1)
|
|
|
|
Extra items in the right set:
|
|
|
|
repr(3)
|
|
|
|
Full diff (fallback to calling repr on each item):
|
|
|
|
- repr(1)
|
|
|
|
repr(2)
|
|
|
|
+ repr(3)
|
|
|
|
""").strip()
|
|
|
|
assert '\n'.join(expl) == dedent
|