From 0dcb4b9fec1d6eec84425f6c7410fd1ada81641b Mon Sep 17 00:00:00 2001 From: Isaac Virshup Date: Mon, 17 Jul 2023 18:25:02 +0200 Subject: [PATCH] Proof of concept --- src/_pytest/_code/code.py | 7 ++++++- testing/code/test_excinfo.py | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/src/_pytest/_code/code.py b/src/_pytest/_code/code.py index 42c5fa8bd..b73c8bbb3 100644 --- a/src/_pytest/_code/code.py +++ b/src/_pytest/_code/code.py @@ -704,7 +704,12 @@ class ExceptionInfo(Generic[E]): If it matches `True` is returned, otherwise an `AssertionError` is raised. """ __tracebackhide__ = True - value = str(self.value) + value = "\n".join( + [ + str(self.value), + *getattr(self.value, "__notes__", []), + ] + ) msg = f"Regex pattern did not match.\n Regex: {regexp!r}\n Input: {value!r}" if regexp == value: msg += "\n Did you mean to `re.escape()` the regex?" diff --git a/testing/code/test_excinfo.py b/testing/code/test_excinfo.py index e5c030c4d..079e270d4 100644 --- a/testing/code/test_excinfo.py +++ b/testing/code/test_excinfo.py @@ -1648,3 +1648,38 @@ def test_hidden_entries_of_chained_exceptions_are_not_shown(pytester: Pytester) ], consecutive=True, ) + + +@pytest.mark.skip("sys.version_info < (3,11)") +@pytest.mark.parametrize( + "error,notes,match", + [ + (Exception("test"), [], "test"), + (AssertionError("foo"), ["bar"], "bar"), + (AssertionError("foo"), ["bar", "baz"], "bar"), + (AssertionError("foo"), ["bar", "baz"], "baz"), + ], +) +def test_check_error_notes_success(error, notes, match): + for note in notes: + error.add_note(note) + + with pytest.raises(Exception, match=match): + raise error + + +@pytest.mark.skip("sys.version_info < (3,11)") +@pytest.mark.parametrize( + "error, notes, match", + [ + (Exception("test"), [], "foo"), + (AssertionError("foo"), ["bar"], "baz"), + ], +) +def test_check_error_notes_failure(error, notes, match): + for note in notes: + error.add_note(note) + + with pytest.raises(AssertionError): + with pytest.raises(type(error), match=match): + raise error