Skip to content

Commit

Permalink
Obey verbosity level when printing 'msg' part of assertions (#12662) (#…
Browse files Browse the repository at this point in the history
…12687)

Seems like we just missed that case when more fine-grained verbosity levels were added.

Fixes #6682, #12307

(cherry picked from commit bc1e17e)

Co-authored-by: Bruno Oliveira <[email protected]>
  • Loading branch information
patchback[bot] and nicoddemus authored Aug 5, 2024
1 parent 32f9951 commit dcff37a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog/6682.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed bug where the verbosity levels where not being respected when printing the "msg" part of failed assertion (as in ``assert condition, msg``).
2 changes: 1 addition & 1 deletion src/_pytest/assertion/rewrite.py
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ def _format_assertmsg(obj: object) -> str:
# However in either case we want to preserve the newline.
replaces = [("\n", "\n~"), ("%", "%%")]
if not isinstance(obj, str):
obj = saferepr(obj)
obj = saferepr(obj, _get_maxsize_for_saferepr(util._config))
replaces.append(("\\n", "\n~"))

for r1, r2 in replaces:
Expand Down
28 changes: 28 additions & 0 deletions testing/test_assertrewrite.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,34 @@ def test_assertion_messages_bytes(self, pytester: Pytester) -> None:
assert result.ret == 1
result.stdout.fnmatch_lines(["*AssertionError: b'ohai!'", "*assert False"])

def test_assertion_message_verbosity(self, pytester: Pytester) -> None:
"""
Obey verbosity levels when printing the "message" part of assertions, when they are
non-strings (#6682).
"""
pytester.makepyfile(
"""
class LongRepr:
def __repr__(self):
return "A" * 500
def test_assertion_verbosity():
assert False, LongRepr()
"""
)
# Normal verbosity: assertion message gets abbreviated.
result = pytester.runpytest()
assert result.ret == 1
result.stdout.re_match_lines(
[r".*AssertionError: A+\.\.\.A+$", ".*assert False"]
)

# High-verbosity: do not abbreviate the assertion message.
result = pytester.runpytest("-vv")
assert result.ret == 1
result.stdout.re_match_lines([r".*AssertionError: A+$", ".*assert False"])

def test_boolop(self) -> None:
def f1() -> None:
f = g = False
Expand Down

0 comments on commit dcff37a

Please sign in to comment.