Skip to content

Commit

Permalink
Merge pull request #9784 from pytest-dev/backport-9768-to-7.1.x
Browse files Browse the repository at this point in the history
[7.1.x] testing: fix tests when run under `-v` or `-vv`
  • Loading branch information
bluetech committed Mar 17, 2022
2 parents a65c47a + 30d995e commit 6f2c1ec
Showing 1 changed file with 9 additions and 39 deletions.
48 changes: 9 additions & 39 deletions testing/test_assertrewrite.py
Expand Up @@ -204,16 +204,8 @@ def f3() -> None:
def f4() -> None:
assert sys == 42 # type: ignore[comparison-overlap]

verbose = request.config.getoption("verbose")
msg = getmsg(f4, {"sys": sys})
if verbose > 0:
assert msg == (
"assert <module 'sys' (built-in)> == 42\n"
" +<module 'sys' (built-in)>\n"
" -42"
)
else:
assert msg == "assert sys == 42"
assert msg == "assert sys == 42"

def f5() -> None:
assert cls == 42 # type: ignore[name-defined] # noqa: F821
Expand All @@ -224,20 +216,7 @@ class X:
msg = getmsg(f5, {"cls": X})
assert msg is not None
lines = msg.splitlines()
if verbose > 1:
assert lines == [
f"assert {X!r} == 42",
f" +{X!r}",
" -42",
]
elif verbose > 0:
assert lines == [
"assert <class 'test_...e.<locals>.X'> == 42",
f" +{X!r}",
" -42",
]
else:
assert lines == ["assert cls == 42"]
assert lines == ["assert cls == 42"]

def test_assertrepr_compare_same_width(self, request) -> None:
"""Should use same width/truncation with same initial width."""
Expand Down Expand Up @@ -279,14 +258,11 @@ def f() -> None:
msg = getmsg(f, {"cls": Y})
assert msg is not None
lines = msg.splitlines()
if request.config.getoption("verbose") > 0:
assert lines == ["assert 3 == 2", " +3", " -2"]
else:
assert lines == [
"assert 3 == 2",
" + where 3 = Y.foo",
" + where Y = cls()",
]
assert lines == [
"assert 3 == 2",
" + where 3 = Y.foo",
" + where Y = cls()",
]

def test_assert_already_has_message(self) -> None:
def f():
Expand Down Expand Up @@ -663,10 +639,7 @@ def f():
assert len(values) == 11

msg = getmsg(f)
if request.config.getoption("verbose") > 0:
assert msg == "assert 10 == 11\n +10\n -11"
else:
assert msg == "assert 10 == 11\n + where 10 = len([0, 1, 2, 3, 4, 5, ...])"
assert msg == "assert 10 == 11\n + where 10 = len([0, 1, 2, 3, 4, 5, ...])"

def test_custom_reprcompare(self, monkeypatch) -> None:
def my_reprcompare1(op, left, right) -> str:
Expand Down Expand Up @@ -732,10 +705,7 @@ def __repr__(self):
msg = getmsg(f)
assert msg is not None
lines = util._format_lines([msg])
if request.config.getoption("verbose") > 0:
assert lines == ["assert 0 == 1\n +0\n -1"]
else:
assert lines == ["assert 0 == 1\n + where 1 = \\n{ \\n~ \\n}.a"]
assert lines == ["assert 0 == 1\n + where 1 = \\n{ \\n~ \\n}.a"]

def test_custom_repr_non_ascii(self) -> None:
def f() -> None:
Expand Down

0 comments on commit 6f2c1ec

Please sign in to comment.