pytest.raises accept cutom message only when used as context manager

This commit is contained in:
palaviv 2016-06-19 21:24:47 +03:00
parent e6ff01ada3
commit ca093673fb
2 changed files with 10 additions and 8 deletions

View File

@ -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

View File

@ -76,20 +76,23 @@ class TestRaises:
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"