Raise unexpected exceptions with pytest.raises() using match=
This commit is contained in:
parent
9849022eb2
commit
3cbf0c8ec0
|
@ -617,6 +617,6 @@ class RaisesContext(object):
|
||||||
suppress_exception = issubclass(self.excinfo.type, self.expected_exception)
|
suppress_exception = issubclass(self.excinfo.type, self.expected_exception)
|
||||||
if sys.version_info[0] == 2 and suppress_exception:
|
if sys.version_info[0] == 2 and suppress_exception:
|
||||||
sys.exc_clear()
|
sys.exc_clear()
|
||||||
if self.match_expr:
|
if self.match_expr and suppress_exception:
|
||||||
self.excinfo.match(self.match_expr)
|
self.excinfo.match(self.match_expr)
|
||||||
return suppress_exception
|
return suppress_exception
|
||||||
|
|
|
@ -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.
|
|
@ -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.
|
|
|
@ -132,3 +132,13 @@ class TestRaises(object):
|
||||||
with pytest.raises(AssertionError, match=expr):
|
with pytest.raises(AssertionError, match=expr):
|
||||||
with pytest.raises(ValueError, match=msg):
|
with pytest.raises(ValueError, match=msg):
|
||||||
int('asdf', base=10)
|
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')
|
||||||
|
|
Loading…
Reference in New Issue