Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/13862.improvement.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improved the readability of "DID NOT RAISE" error messages by using the exception type's name instead of its `repr`.
9 changes: 5 additions & 4 deletions src/_pytest/raises.py
Original file line number Diff line number Diff line change
Expand Up @@ -704,10 +704,11 @@ def __exit__(
if exc_type is None:
if not self.expected_exceptions:
fail("DID NOT RAISE any exception")
if len(self.expected_exceptions) > 1:
fail(f"DID NOT RAISE any of {self.expected_exceptions!r}")

fail(f"DID NOT RAISE {self.expected_exceptions[0]!r}")
if len(self.expected_exceptions) == 1:
fail(f"DID NOT RAISE {self.expected_exceptions[0].__name__}")
else:
names = ", ".join(f"{x.__name__}" for x in self.expected_exceptions)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: This can be simplified as it's already a string:

Suggested change
names = ", ".join(f"{x.__name__}" for x in self.expected_exceptions)
names = ", ".join(x.__name__ for x in self.expected_exceptions)

fail(f"DID NOT RAISE any of ({names})")

assert self.excinfo is not None, (
"Internal error - should have been constructed in __enter__"
Expand Down
6 changes: 3 additions & 3 deletions testing/python/raises.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,15 +194,15 @@ def test_no_raise_message(self) -> None:
try:
pytest.raises(ValueError, int, "0")
except pytest.fail.Exception as e:
assert e.msg == f"DID NOT RAISE {ValueError!r}"
assert e.msg == "DID NOT RAISE ValueError"
else:
assert False, "Expected pytest.raises.Exception"

try:
with pytest.raises(ValueError):
pass
except pytest.fail.Exception as e:
assert e.msg == f"DID NOT RAISE {ValueError!r}"
assert e.msg == "DID NOT RAISE ValueError"
else:
assert False, "Expected pytest.raises.Exception"

Expand Down Expand Up @@ -333,7 +333,7 @@ class ClassLooksIterableException(Exception, metaclass=Meta):

with pytest.raises(
Failed,
match=r"DID NOT RAISE <class 'raises(\..*)*ClassLooksIterableException'>",
match=r"DID NOT RAISE ClassLooksIterableException",
):
pytest.raises(ClassLooksIterableException, lambda: None)

Expand Down
6 changes: 2 additions & 4 deletions testing/python/raises_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -1096,7 +1096,7 @@ def test_raisesexc() -> None:

# FIXME: leaving this one formatted differently for now to not change
# tests in python/raises.py
with pytest.raises(Failed, match=wrap_escape("DID NOT RAISE <class 'ValueError'>")):
with pytest.raises(Failed, match=wrap_escape("DID NOT RAISE ValueError")):
with RaisesExc(ValueError):
...

Expand All @@ -1107,9 +1107,7 @@ def test_raisesexc() -> None:
with pytest.raises(
# FIXME: do we want repr(type) or type.__name__ ?
Failed,
match=wrap_escape(
"DID NOT RAISE any of (<class 'ValueError'>, <class 'TypeError'>)"
),
match=wrap_escape("DID NOT RAISE any of (ValueError, TypeError)"),
):
with RaisesExc((ValueError, TypeError)):
...
Expand Down