Skip to content

Commit 06fae84

Browse files
committed
add matching the error message to pytest.raises
1 parent 0931fe2 commit 06fae84

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

CHANGELOG.rst

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
3.0.7 (unreleased)
22
=======================
33

4-
*
4+
* ``pytest.raises`` can now assert that the error message contains a certain text.
5+
Thanks `@Kriechi`_ for the PR.
56

67
*
78

@@ -10,6 +11,9 @@
1011
*
1112

1213

14+
.. _@Kriechi: https://github.com/Kriechi
15+
16+
1317
3.0.6 (2017-01-22)
1418
==================
1519

_pytest/python.py

+15-2
Original file line numberDiff line numberDiff line change
@@ -1136,6 +1136,12 @@ def raises(expected_exception, *args, **kwargs):
11361136
...
11371137
>>> assert str(exc_info.value) == "value must be <= 10"
11381138
1139+
Or you can use the keyword argument ``match_info`` to assert that the
1140+
exception contains a certain text::
1141+
1142+
>>> with raises(ValueError, match_info='must be 0'):
1143+
.... if value != 0:
1144+
.... raise ValueError("value must be 0 or None")
11391145
11401146
Or you can specify a callable by passing a to-be-called lambda::
11411147
@@ -1191,11 +1197,15 @@ def raises(expected_exception, *args, **kwargs):
11911197
raise TypeError(msg % type(expected_exception))
11921198

11931199
message = "DID NOT RAISE {0}".format(expected_exception)
1200+
match_info = None
11941201

11951202
if not args:
11961203
if "message" in kwargs:
11971204
message = kwargs.pop("message")
1198-
return RaisesContext(expected_exception, message)
1205+
if "match_info" in kwargs:
1206+
match_info = kwargs.pop("match_info")
1207+
message += " with text '{0}'".format(match_info)
1208+
return RaisesContext(expected_exception, message, match_info)
11991209
elif isinstance(args[0], str):
12001210
code, = args
12011211
assert isinstance(code, str)
@@ -1219,9 +1229,10 @@ def raises(expected_exception, *args, **kwargs):
12191229
pytest.fail(message)
12201230

12211231
class RaisesContext(object):
1222-
def __init__(self, expected_exception, message):
1232+
def __init__(self, expected_exception, message, match_info):
12231233
self.expected_exception = expected_exception
12241234
self.message = message
1235+
self.match_info = match_info
12251236
self.excinfo = None
12261237

12271238
def __enter__(self):
@@ -1240,6 +1251,8 @@ def __exit__(self, *tp):
12401251
exc_type, value, traceback = tp
12411252
tp = exc_type, exc_type(value), traceback
12421253
self.excinfo.__init__(tp)
1254+
if self.match_info and not self.match_info.lower() in str(self.excinfo).lower():
1255+
pytest.fail(self.message)
12431256
suppress_exception = issubclass(self.excinfo.type, self.expected_exception)
12441257
if sys.version_info[0] == 2 and suppress_exception:
12451258
sys.exc_clear()

testing/python/raises.py

+13
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,16 @@ def __call__(self):
126126
for o in gc.get_objects():
127127
assert type(o) is not T
128128

129+
130+
def test_raises_match_info(self):
131+
msg = "with base 10"
132+
with pytest.raises(ValueError, match_info=msg):
133+
int('asdf')
134+
135+
try:
136+
with pytest.raises(ValueError, match_info=msg):
137+
int('asdf', base=16)
138+
except pytest.raises.Exception as e:
139+
assert e.msg == "DID NOT RAISE {0} with text '{1}'".format(repr(ValueError), msg)
140+
else:
141+
assert False, "Expected pytest.raises.Exception"

0 commit comments

Comments
 (0)