Skip to content

Commit b486e12

Browse files
authored
Merge pull request #3222 from The-Compiler/match-msg
Remove "matching '...'" part from the pytest.raises message
2 parents 9d879be + 3cbf0c8 commit b486e12

File tree

3 files changed

+12
-2
lines changed

3 files changed

+12
-2
lines changed

_pytest/python_api.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,6 @@ def raises(expected_exception, *args, **kwargs):
571571
message = kwargs.pop("message")
572572
if "match" in kwargs:
573573
match_expr = kwargs.pop("match")
574-
message += " matching '{0}'".format(match_expr)
575574
return RaisesContext(expected_exception, message, match_expr)
576575
elif isinstance(args[0], str):
577576
code, = args
@@ -618,6 +617,6 @@ def __exit__(self, *tp):
618617
suppress_exception = issubclass(self.excinfo.type, self.expected_exception)
619618
if sys.version_info[0] == 2 and suppress_exception:
620619
sys.exc_clear()
621-
if self.match_expr:
620+
if self.match_expr and suppress_exception:
622621
self.excinfo.match(self.match_expr)
623622
return suppress_exception

changelog/3222.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
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.

testing/python/raises.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,3 +132,13 @@ def test_raises_match(self):
132132
with pytest.raises(AssertionError, match=expr):
133133
with pytest.raises(ValueError, match=msg):
134134
int('asdf', base=10)
135+
136+
def test_raises_match_wrong_type(self):
137+
"""Raising an exception with the wrong type and match= given.
138+
139+
pytest should throw the unexpected exception - the pattern match is not
140+
really relevant if we got a different exception.
141+
"""
142+
with pytest.raises(ValueError):
143+
with pytest.raises(IndexError, match='nomatch'):
144+
int('asdf')

0 commit comments

Comments
 (0)