Skip to content

Commit cfb7bec

Browse files
committed
add matching the error message to pytest.raises
1 parent 3b47cb4 commit cfb7bec

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

CHANGELOG.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ New Features
1313
* ``pytest.warns`` now checks for subclass relationship rather than
1414
class equality. Thanks `@lesteve`_ for the PR (`#2166`_)
1515

16+
* ``pytest.raises`` now assert that the error message contains a certain text
17+
with the `match_info` keyword argument. Thanks `@Kriechi`_ for the PR.
18+
1619

1720
Changes
1821
-------
@@ -56,6 +59,7 @@ Changes
5659
.. _@fogo: https://github.com/fogo
5760
.. _@mandeep: https://github.com/mandeep
5861
.. _@unsignedint: https://github.com/unsignedint
62+
.. _@Kriechi: https://github.com/Kriechi
5963

6064
.. _#1512: https://github.com/pytest-dev/pytest/issues/1512
6165
.. _#1874: https://github.com/pytest-dev/pytest/pull/1874

_pytest/python.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1145,6 +1145,12 @@ def raises(expected_exception, *args, **kwargs):
11451145
...
11461146
>>> assert str(exc_info.value) == "value must be <= 10"
11471147
1148+
Or you can use the keyword argument ``match_info`` to assert that the
1149+
exception contains a certain text::
1150+
1151+
>>> with raises(ValueError, match_info='must be 0'):
1152+
... if value != 0:
1153+
... raise ValueError("value must be 0 or None")
11481154
11491155
Or you can specify a callable by passing a to-be-called lambda::
11501156
@@ -1194,11 +1200,15 @@ def raises(expected_exception, *args, **kwargs):
11941200
raise TypeError(msg % type(expected_exception))
11951201

11961202
message = "DID NOT RAISE {0}".format(expected_exception)
1203+
match_info = None
11971204

11981205
if not args:
11991206
if "message" in kwargs:
12001207
message = kwargs.pop("message")
1201-
return RaisesContext(expected_exception, message)
1208+
if "match_info" in kwargs:
1209+
match_info = kwargs.pop("match_info")
1210+
message += " with text '{0}'".format(match_info)
1211+
return RaisesContext(expected_exception, message, match_info)
12021212
elif isinstance(args[0], str):
12031213
code, = args
12041214
assert isinstance(code, str)
@@ -1222,9 +1232,10 @@ def raises(expected_exception, *args, **kwargs):
12221232
pytest.fail(message)
12231233

12241234
class RaisesContext(object):
1225-
def __init__(self, expected_exception, message):
1235+
def __init__(self, expected_exception, message, match_info):
12261236
self.expected_exception = expected_exception
12271237
self.message = message
1238+
self.match_info = match_info
12281239
self.excinfo = None
12291240

12301241
def __enter__(self):
@@ -1243,6 +1254,8 @@ def __exit__(self, *tp):
12431254
exc_type, value, traceback = tp
12441255
tp = exc_type, exc_type(value), traceback
12451256
self.excinfo.__init__(tp)
1257+
if self.match_info and not self.match_info.lower() in str(self.excinfo).lower():
1258+
pytest.fail(self.message)
12461259
suppress_exception = issubclass(self.excinfo.type, self.expected_exception)
12471260
if sys.version_info[0] == 2 and suppress_exception:
12481261
sys.exc_clear()

testing/python/raises.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,16 @@ def __call__(self):
118118
for o in gc.get_objects():
119119
assert type(o) is not T
120120

121+
122+
def test_raises_match_info(self):
123+
msg = "with base 10"
124+
with pytest.raises(ValueError, match_info=msg):
125+
int('asdf')
126+
127+
try:
128+
with pytest.raises(ValueError, match_info=msg):
129+
int('asdf', base=16)
130+
except pytest.raises.Exception as e:
131+
assert e.msg == "DID NOT RAISE {0} with text '{1}'".format(repr(ValueError), msg)
132+
else:
133+
assert False, "Expected pytest.raises.Exception"

0 commit comments

Comments
 (0)