Skip to content

Commit

Permalink
pytest.raises accept cutom message only when used as context manager
Browse files Browse the repository at this point in the history
  • Loading branch information
palaviv committed Jun 19, 2016
1 parent e6ff01a commit ca09367
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
7 changes: 3 additions & 4 deletions _pytest/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -1412,12 +1412,11 @@ def raises(expected_exception, *args, **kwargs):
elif not isclass(expected_exception):
raise TypeError(msg % type(expected_exception))

if "message" in kwargs:
message = kwargs.pop("message")
else:
message = "DID NOT RAISE {0}".format(expected_exception)
message = "DID NOT RAISE {0}".format(expected_exception)

if not args:
if "message" in kwargs:
message = kwargs.pop("message")
return RaisesContext(expected_exception, message)
elif isinstance(args[0], str):
code, = args
Expand Down
11 changes: 7 additions & 4 deletions testing/python/raises.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,20 +76,23 @@ def test_no_raise_message(self):
pytest.raises(ValueError, int, '0')
except pytest.raises.Exception as e:
assert e.msg == "DID NOT RAISE {0}".format(repr(ValueError))
else:
assert False, "Expected pytest.raises.Exception"

try:
with pytest.raises(ValueError):
pass
except pytest.raises.Exception as e:
assert e.msg == "DID NOT RAISE {0}".format(repr(ValueError))
else:
assert False, "Expected pytest.raises.Exception"

def test_costum_raise_message(self):
message = "TEST_MESSAGE"
try:
pytest.raises(ValueError, int, '0', message=message)
except pytest.raises.Exception as e:
assert e.msg == message
try:
with pytest.raises(ValueError, message=message):
pass
except pytest.raises.Exception as e:
assert e.msg == message
else:
assert False, "Expected pytest.raises.Exception"

0 comments on commit ca09367

Please sign in to comment.