From c29130d400452107fa68cf2c40a1b47349c27ca0 Mon Sep 17 00:00:00 2001 From: palaviv Date: Sun, 19 Jun 2016 23:34:42 +0300 Subject: [PATCH] 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 53e5768eb..569c65391 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 56ba90c55..254459d95 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 680d22798..687dadb85 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::