From 9849022eb2421c9e468609cbbbedf63a8a042702 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Wed, 14 Feb 2018 19:20:00 +0100 Subject: [PATCH 1/2] Remove "matching '...'" part from the pytest.raises message When a test with pytest.raises(ValueError, match='foo') doesn't raise, the following error is printed: Failed: DID NOT RAISE matching 'foo' This error message is confusing as it implies a ValueError was raised, but the message wasn't matching 'foo'. I first considered rewording it somehow to preserve the match pattern in it, but I don't think that's worthwhile as the pattern should usually be apparent from the stacktrace anyways (hard-coded, as parametrization, or with --showlocals for more sophisticated cases). --- _pytest/python_api.py | 1 - changelog/3222.trivial | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) create mode 100644 changelog/3222.trivial diff --git a/_pytest/python_api.py b/_pytest/python_api.py index e6f002849d5..5bbf3ac7ad7 100644 --- a/_pytest/python_api.py +++ b/_pytest/python_api.py @@ -571,7 +571,6 @@ def raises(expected_exception, *args, **kwargs): message = kwargs.pop("message") if "match" in kwargs: match_expr = kwargs.pop("match") - message += " matching '{0}'".format(match_expr) return RaisesContext(expected_exception, message, match_expr) elif isinstance(args[0], str): code, = args diff --git a/changelog/3222.trivial b/changelog/3222.trivial new file mode 100644 index 00000000000..fbbbd551016 --- /dev/null +++ b/changelog/3222.trivial @@ -0,0 +1 @@ +The "matching '...'" part got removed from ``pytest.raises()`` error messages as it falsely implies that an exception was raised but it didn't match. \ No newline at end of file From 3cbf0c8ec028e4fdba2e66327a2d4f01025804c4 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Thu, 15 Feb 2018 12:11:56 +0100 Subject: [PATCH 2/2] Raise unexpected exceptions with pytest.raises() using match= --- _pytest/python_api.py | 2 +- changelog/3222.bugfix | 1 + changelog/3222.trivial | 1 - testing/python/raises.py | 10 ++++++++++ 4 files changed, 12 insertions(+), 2 deletions(-) create mode 100644 changelog/3222.bugfix delete mode 100644 changelog/3222.trivial diff --git a/_pytest/python_api.py b/_pytest/python_api.py index 5bbf3ac7ad7..89ef9e0a953 100644 --- a/_pytest/python_api.py +++ b/_pytest/python_api.py @@ -617,6 +617,6 @@ def __exit__(self, *tp): suppress_exception = issubclass(self.excinfo.type, self.expected_exception) if sys.version_info[0] == 2 and suppress_exception: sys.exc_clear() - if self.match_expr: + if self.match_expr and suppress_exception: self.excinfo.match(self.match_expr) return suppress_exception diff --git a/changelog/3222.bugfix b/changelog/3222.bugfix new file mode 100644 index 00000000000..40a8e99e9d2 --- /dev/null +++ b/changelog/3222.bugfix @@ -0,0 +1 @@ +Errors shown when a ``pytest.raises()`` with ``match=`` fails are now cleaner on what happened: When no exception was raised, the "matching '...'" part got removed as it falsely implies that an exception was raised but it didn't match. When a wrong exception was raised, it's now thrown (like ``pytest.raised()`` without ``match=`` would) instead of complaining about the unmatched text. \ No newline at end of file diff --git a/changelog/3222.trivial b/changelog/3222.trivial deleted file mode 100644 index fbbbd551016..00000000000 --- a/changelog/3222.trivial +++ /dev/null @@ -1 +0,0 @@ -The "matching '...'" part got removed from ``pytest.raises()`` error messages as it falsely implies that an exception was raised but it didn't match. \ No newline at end of file diff --git a/testing/python/raises.py b/testing/python/raises.py index 321ee349ee6..183259f6b72 100644 --- a/testing/python/raises.py +++ b/testing/python/raises.py @@ -132,3 +132,13 @@ def test_raises_match(self): with pytest.raises(AssertionError, match=expr): with pytest.raises(ValueError, match=msg): int('asdf', base=10) + + def test_raises_match_wrong_type(self): + """Raising an exception with the wrong type and match= given. + + pytest should throw the unexpected exception - the pattern match is not + really relevant if we got a different exception. + """ + with pytest.raises(ValueError): + with pytest.raises(IndexError, match='nomatch'): + int('asdf')