From d21886c005e0c5f9aa8e56213a23cb03b2919c96 Mon Sep 17 00:00:00 2001 From: palaviv Date: Thu, 16 Jun 2016 20:15:32 +0300 Subject: [PATCH 1/6] pytest.raises accpets custom message --- _pytest/python.py | 14 ++++++++++---- testing/python/raises.py | 17 +++++++++++++++++ 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/_pytest/python.py b/_pytest/python.py index 4262d6fa7f7..b2cf99efbe4 100644 --- a/_pytest/python.py +++ b/_pytest/python.py @@ -1412,8 +1412,13 @@ 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) + if not args: - return RaisesContext(expected_exception) + return RaisesContext(expected_exception, message) elif isinstance(args[0], str): code, = args assert isinstance(code, str) @@ -1434,11 +1439,12 @@ def raises(expected_exception, *args, **kwargs): func(*args[1:], **kwargs) except expected_exception: return _pytest._code.ExceptionInfo() - pytest.fail("DID NOT RAISE {0}".format(expected_exception)) + pytest.fail(message) class RaisesContext(object): - def __init__(self, expected_exception): + def __init__(self, expected_exception, message): self.expected_exception = expected_exception + self.message = message self.excinfo = None def __enter__(self): @@ -1448,7 +1454,7 @@ def __enter__(self): def __exit__(self, *tp): __tracebackhide__ = True if tp[0] is None: - pytest.fail("DID NOT RAISE") + pytest.fail(self.message) if sys.version_info < (2, 7): # py26: on __exit__() exc_value often does not contain the # exception value. diff --git a/testing/python/raises.py b/testing/python/raises.py index 0ea7f9bee3e..b42f4f54d46 100644 --- a/testing/python/raises.py +++ b/testing/python/raises.py @@ -76,3 +76,20 @@ 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)) + try: + with pytest.raises(ValueError): + pass + except pytest.raises.Exception as e: + e.msg == "DID NOT RAISE {0}".format(repr(ValueError)) + + 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: + e.msg == message From 8ddbca36c9671492ebc2fc7f2f16de0fd91e6181 Mon Sep 17 00:00:00 2001 From: palaviv Date: Thu, 16 Jun 2016 20:21:03 +0300 Subject: [PATCH 2/6] Add CHANGLOG entry --- CHANGELOG.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 45fde880f29..1dcc64f77e7 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -66,7 +66,8 @@ * Add ``build/`` and ``dist/`` to the default ``--norecursedirs`` list. Thanks `@mikofski`_ for the report and `@tomviner`_ for the PR (`#1544`_). -* +* pytest.raises accepts a custom message to raise when no exception accord. + Thanks `@palaviv`_ for the PR. .. _@milliams: https://github.com/milliams .. _@csaftoiu: https://github.com/csaftoiu From e6ff01ada3d4925b4d5cbebf1a237c897002edcb Mon Sep 17 00:00:00 2001 From: palaviv Date: Thu, 16 Jun 2016 21:09:15 +0300 Subject: [PATCH 3/6] CR fixes --- CHANGELOG.rst | 5 +++-- testing/python/raises.py | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 1dcc64f77e7..53e5768eb0f 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -66,8 +66,8 @@ * Add ``build/`` and ``dist/`` to the default ``--norecursedirs`` list. Thanks `@mikofski`_ for the report and `@tomviner`_ for the PR (`#1544`_). -* pytest.raises accepts a custom message to raise when no exception accord. - Thanks `@palaviv`_ for the PR. +* pytest.raises accepts a custom message to raise when no exception occurred. + Thanks `@palaviv`_ for the complete PR (`#1616`_). .. _@milliams: https://github.com/milliams .. _@csaftoiu: https://github.com/csaftoiu @@ -92,6 +92,7 @@ .. _#1520: https://github.com/pytest-dev/pytest/pull/1520 .. _#372: https://github.com/pytest-dev/pytest/issues/372 .. _#1544: https://github.com/pytest-dev/pytest/issues/1544 +.. _#1616: https://github.com/pytest-dev/pytest/pull/1616 **Bug Fixes** diff --git a/testing/python/raises.py b/testing/python/raises.py index b42f4f54d46..1c8e89be9ae 100644 --- a/testing/python/raises.py +++ b/testing/python/raises.py @@ -80,7 +80,7 @@ def test_no_raise_message(self): with pytest.raises(ValueError): pass except pytest.raises.Exception as e: - e.msg == "DID NOT RAISE {0}".format(repr(ValueError)) + assert e.msg == "DID NOT RAISE {0}".format(repr(ValueError)) def test_costum_raise_message(self): message = "TEST_MESSAGE" @@ -92,4 +92,4 @@ def test_costum_raise_message(self): with pytest.raises(ValueError, message=message): pass except pytest.raises.Exception as e: - e.msg == message + assert e.msg == message From ca093673fb8508c4dc7772e71b783ac62b6d5726 Mon Sep 17 00:00:00 2001 From: palaviv Date: Sun, 19 Jun 2016 21:24:47 +0300 Subject: [PATCH 4/6] 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" From c29130d400452107fa68cf2c40a1b47349c27ca0 Mon Sep 17 00:00:00 2001 From: palaviv Date: Sun, 19 Jun 2016 23:34:42 +0300 Subject: [PATCH 5/6] Updated documentation --- CHANGELOG.rst | 3 ++- _pytest/python.py | 8 ++++++++ doc/en/assert.rst | 7 +++++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 53e5768eb0f..569c6539147 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -66,7 +66,8 @@ * Add ``build/`` and ``dist/`` to the default ``--norecursedirs`` list. Thanks `@mikofski`_ for the report and `@tomviner`_ for the PR (`#1544`_). -* pytest.raises accepts a custom message to raise when no exception occurred. +* pytest.raises in the context manager form accepts a custom + message to raise when no exception occurred. Thanks `@palaviv`_ for the complete PR (`#1616`_). .. _@milliams: https://github.com/milliams diff --git a/_pytest/python.py b/_pytest/python.py index 56ba90c55e5..254459d9500 100644 --- a/_pytest/python.py +++ b/_pytest/python.py @@ -1337,6 +1337,14 @@ def raises(expected_exception, *args, **kwargs): >>> with raises(ZeroDivisionError): ... 1/0 + In the context manager form you may use the keyword argument + ``message`` to specify a custom failure message:: + + >>> with raises(ZeroDivisionError, message="Expecting ZeroDivisionError"): + ... pass + ... Failed: Expecting ZeroDivisionError + + .. note:: When using ``pytest.raises`` as a context manager, it's worthwhile to diff --git a/doc/en/assert.rst b/doc/en/assert.rst index 680d2279805..687dadb8528 100644 --- a/doc/en/assert.rst +++ b/doc/en/assert.rst @@ -85,6 +85,13 @@ and if you need to have access to the actual exception info you may use:: the actual exception raised. The main attributes of interest are ``.type``, ``.value`` and ``.traceback``. +In the context manager form you may use the keyword argument +``message`` to specify a custom failure message:: + + >>> with raises(ZeroDivisionError, message="Expecting ZeroDivisionError"): + ... pass + ... Failed: Expecting ZeroDivisionError + If you want to write test code that works on Python 2.4 as well, you may also use two other ways to test for an expected exception:: From f8d4cadc3d5d9145d7be1d462fd6b1135307fd5f Mon Sep 17 00:00:00 2001 From: palaviv Date: Sun, 19 Jun 2016 23:56:43 +0300 Subject: [PATCH 6/6] Added versionchanged directives --- _pytest/python.py | 2 ++ doc/en/assert.rst | 2 ++ 2 files changed, 4 insertions(+) diff --git a/_pytest/python.py b/_pytest/python.py index 254459d9500..526f4be09cb 100644 --- a/_pytest/python.py +++ b/_pytest/python.py @@ -1337,6 +1337,8 @@ def raises(expected_exception, *args, **kwargs): >>> with raises(ZeroDivisionError): ... 1/0 + .. versionchanged:: 2.10 + In the context manager form you may use the keyword argument ``message`` to specify a custom failure message:: diff --git a/doc/en/assert.rst b/doc/en/assert.rst index 687dadb8528..d867dfa9af6 100644 --- a/doc/en/assert.rst +++ b/doc/en/assert.rst @@ -85,6 +85,8 @@ and if you need to have access to the actual exception info you may use:: the actual exception raised. The main attributes of interest are ``.type``, ``.value`` and ``.traceback``. +.. versionchanged:: 2.10 + In the context manager form you may use the keyword argument ``message`` to specify a custom failure message::