diff --git a/changelog/13862.improvement.rst b/changelog/13862.improvement.rst new file mode 100644 index 00000000000..6a89cbc323b --- /dev/null +++ b/changelog/13862.improvement.rst @@ -0,0 +1 @@ +Improved the readability of "DID NOT RAISE" error messages by using the exception type's name instead of its `repr`. diff --git a/src/_pytest/raises.py b/src/_pytest/raises.py index 7c246fde280..85e54d301ca 100644 --- a/src/_pytest/raises.py +++ b/src/_pytest/raises.py @@ -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) + fail(f"DID NOT RAISE any of ({names})") assert self.excinfo is not None, ( "Internal error - should have been constructed in __enter__" diff --git a/testing/python/raises.py b/testing/python/raises.py index c9d57918a83..a65463bef53 100644 --- a/testing/python/raises.py +++ b/testing/python/raises.py @@ -194,7 +194,7 @@ 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" @@ -202,7 +202,7 @@ def test_no_raise_message(self) -> None: 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" @@ -333,7 +333,7 @@ class ClassLooksIterableException(Exception, metaclass=Meta): with pytest.raises( Failed, - match=r"DID NOT RAISE ", + match=r"DID NOT RAISE ClassLooksIterableException", ): pytest.raises(ClassLooksIterableException, lambda: None) diff --git a/testing/python/raises_group.py b/testing/python/raises_group.py index e5e3b5cd2dc..1a4fdf51ede 100644 --- a/testing/python/raises_group.py +++ b/testing/python/raises_group.py @@ -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 ")): + with pytest.raises(Failed, match=wrap_escape("DID NOT RAISE ValueError")): with RaisesExc(ValueError): ... @@ -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 (, )" - ), + match=wrap_escape("DID NOT RAISE any of (ValueError, TypeError)"), ): with RaisesExc((ValueError, TypeError)): ...