From ca093673fb8508c4dc7772e71b783ac62b6d5726 Mon Sep 17 00:00:00 2001 From: palaviv Date: Sun, 19 Jun 2016 21:24:47 +0300 Subject: [PATCH] pytest.raises accept cutom message only when used as context manager --- _pytest/python.py | 7 +++---- testing/python/raises.py | 11 +++++++---- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/_pytest/python.py b/_pytest/python.py index b2cf99efbe4..56ba90c55e5 100644 --- a/_pytest/python.py +++ b/_pytest/python.py @@ -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 diff --git a/testing/python/raises.py b/testing/python/raises.py index 1c8e89be9ae..93295966c7b 100644 --- a/testing/python/raises.py +++ b/testing/python/raises.py @@ -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"