diff --git a/changelog/5412.removal.rst b/changelog/5412.removal.rst new file mode 100644 index 00000000000..a6f19700629 --- /dev/null +++ b/changelog/5412.removal.rst @@ -0,0 +1,2 @@ +``ExceptionInfo`` objects (returned by ``pytest.raises``) now have the same ``str`` representation as ``repr``, which +avoids some confusion when users use ``print(e)`` to inspect the object. diff --git a/src/_pytest/_code/code.py b/src/_pytest/_code/code.py index c4ed961ace4..9644c61ec50 100644 --- a/src/_pytest/_code/code.py +++ b/src/_pytest/_code/code.py @@ -534,13 +534,6 @@ def getrepr( ) return fmt.repr_excinfo(self) - def __str__(self): - if self._excinfo is None: - return repr(self) - entry = self.traceback[-1] - loc = ReprFileLocation(entry.path, entry.lineno + 1, self.exconly()) - return str(loc) - def match(self, regexp): """ Check whether the regular expression 'regexp' is found in the string diff --git a/testing/code/test_excinfo.py b/testing/code/test_excinfo.py index 3eac94a287e..f7787c282f7 100644 --- a/testing/code/test_excinfo.py +++ b/testing/code/test_excinfo.py @@ -333,18 +333,10 @@ def test_excinfo_exconly(): assert msg.endswith("world") -def test_excinfo_repr(): +def test_excinfo_repr_str(): excinfo = pytest.raises(ValueError, h) - s = repr(excinfo) - assert s == "" - - -def test_excinfo_str(): - excinfo = pytest.raises(ValueError, h) - s = str(excinfo) - assert s.startswith(__file__[:-9]) # pyc file and $py.class - assert s.endswith("ValueError") - assert len(s.split(":")) >= 3 # on windows it's 4 + assert repr(excinfo) == "" + assert str(excinfo) == "" def test_excinfo_for_later():