Skip to content

Commit 2e7d6a6

Browse files
committed
Fix test_assertrewrite in verbose mode
Fixes #4879.
1 parent ea7357b commit 2e7d6a6

File tree

1 file changed

+52
-17
lines changed

1 file changed

+52
-17
lines changed

testing/test_assertrewrite.py

Lines changed: 52 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ def test_dont_rewrite_plugin(self, testdir):
127127
result = testdir.runpytest_subprocess()
128128
assert "warnings" not in "".join(result.outlines)
129129

130-
def test_name(self):
130+
def test_name(self, request):
131131
def f():
132132
assert False
133133

@@ -147,17 +147,41 @@ def f():
147147
def f():
148148
assert sys == 42
149149

150-
assert getmsg(f, {"sys": sys}) == "assert sys == 42"
150+
verbose = request.config.getoption("verbose")
151+
msg = getmsg(f, {"sys": sys})
152+
if verbose > 0:
153+
assert msg == (
154+
"assert <module 'sys' (built-in)> == 42\n"
155+
" -<module 'sys' (built-in)>\n"
156+
" +42"
157+
)
158+
else:
159+
assert msg == "assert sys == 42"
151160

152161
def f():
153-
assert cls == 42 # noqa
162+
assert cls == 42 # noqa: F821
154163

155164
class X(object):
156165
pass
157166

158-
assert getmsg(f, {"cls": X}) == "assert cls == 42"
159-
160-
def test_dont_rewrite_if_hasattr_fails(self):
167+
msg = getmsg(f, {"cls": X}).splitlines()
168+
if verbose > 0:
169+
if six.PY2:
170+
assert msg == [
171+
"assert <class 'test_assertrewrite.X'> == 42",
172+
" -<class 'test_assertrewrite.X'>",
173+
" +42",
174+
]
175+
else:
176+
assert msg == [
177+
"assert <class 'test_...e.<locals>.X'> == 42",
178+
" -<class 'test_assertrewrite.TestAssertionRewrite.test_name.<locals>.X'>",
179+
" +42",
180+
]
181+
else:
182+
assert msg == ["assert cls == 42"]
183+
184+
def test_dont_rewrite_if_hasattr_fails(self, request):
161185
class Y(object):
162186
""" A class whos getattr fails, but not with `AttributeError` """
163187

@@ -173,10 +197,16 @@ def __init__(self):
173197
def f():
174198
assert cls().foo == 2 # noqa
175199

176-
message = getmsg(f, {"cls": Y})
177-
assert "assert 3 == 2" in message
178-
assert "+ where 3 = Y.foo" in message
179-
assert "+ where Y = cls()" in message
200+
# XXX: looks like the "where" should also be there in verbose mode?!
201+
message = getmsg(f, {"cls": Y}).splitlines()
202+
if request.config.getoption("verbose") > 0:
203+
assert message == ["assert 3 == 2", " -3", " +2"]
204+
else:
205+
assert message == [
206+
"assert 3 == 2",
207+
" + where 3 = Y.foo",
208+
" + where Y = cls()",
209+
]
180210

181211
def test_assert_already_has_message(self):
182212
def f():
@@ -552,15 +582,16 @@ def f():
552582

553583
getmsg(f, must_pass=True)
554584

555-
def test_len(self):
585+
def test_len(self, request):
556586
def f():
557587
values = list(range(10))
558588
assert len(values) == 11
559589

560-
assert getmsg(f).startswith(
561-
"""assert 10 == 11
562-
+ where 10 = len(["""
563-
)
590+
msg = getmsg(f)
591+
if request.config.getoption("verbose") > 0:
592+
assert msg == "assert 10 == 11\n -10\n +11"
593+
else:
594+
assert msg == "assert 10 == 11\n + where 10 = len([0, 1, 2, 3, 4, 5, ...])"
564595

565596
def test_custom_reprcompare(self, monkeypatch):
566597
def my_reprcompare(op, left, right):
@@ -608,7 +639,7 @@ def f():
608639

609640
assert getmsg(f).startswith("assert '%test' == 'test'")
610641

611-
def test_custom_repr(self):
642+
def test_custom_repr(self, request):
612643
def f():
613644
class Foo(object):
614645
a = 1
@@ -619,7 +650,11 @@ def __repr__(self):
619650
f = Foo()
620651
assert 0 == f.a
621652

622-
assert r"where 1 = \n{ \n~ \n}.a" in util._format_lines([getmsg(f)])[0]
653+
lines = util._format_lines([getmsg(f)])
654+
if request.config.getoption("verbose") > 0:
655+
assert lines == ["assert 0 == 1\n -0\n +1"]
656+
else:
657+
assert lines == ["assert 0 == 1\n + where 1 = \\n{ \\n~ \\n}.a"]
623658

624659
def test_custom_repr_non_ascii(self):
625660
def f():

0 commit comments

Comments
 (0)