mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
Issue #10775: assertRaises, assertRaisesRegex, assertWarns, and assertWarnsRegex now accept a keyword argument 'msg' when used as context managers. Initial patch by Winston Ewert.
This commit is contained in:
parent
136148e97d
commit
b4dc2502ad
5 changed files with 158 additions and 45 deletions
|
@ -860,10 +860,11 @@ Test cases
|
|||
| <TestCase.assertNotIsInstance>` | | |
|
||||
+-----------------------------------------+-----------------------------+---------------+
|
||||
|
||||
All the assert methods (except :meth:`assertRaises`,
|
||||
:meth:`assertRaisesRegex`, :meth:`assertWarns`, :meth:`assertWarnsRegex`)
|
||||
accept a *msg* argument that, if specified, is used as the error message on
|
||||
failure (see also :data:`longMessage`).
|
||||
All the assert methods accept a *msg* argument that, if specified, is used
|
||||
as the error message on failure (see also :data:`longMessage`).
|
||||
Note that the *msg* keyword argument can be passed to :meth:`assertRaises`,
|
||||
:meth:`assertRaisesRegex`, :meth:`assertWarns`, :meth:`assertWarnsRegex`
|
||||
only when they are used as a context manager.
|
||||
|
||||
.. method:: assertEqual(first, second, msg=None)
|
||||
|
||||
|
@ -957,7 +958,7 @@ Test cases
|
|||
+---------------------------------------------------------+--------------------------------------+------------+
|
||||
|
||||
.. method:: assertRaises(exception, callable, *args, **kwds)
|
||||
assertRaises(exception)
|
||||
assertRaises(exception, msg=None)
|
||||
|
||||
Test that an exception is raised when *callable* is called with any
|
||||
positional or keyword arguments that are also passed to
|
||||
|
@ -966,12 +967,16 @@ Test cases
|
|||
To catch any of a group of exceptions, a tuple containing the exception
|
||||
classes may be passed as *exception*.
|
||||
|
||||
If only the *exception* argument is given, returns a context manager so
|
||||
that the code under test can be written inline rather than as a function::
|
||||
If only the *exception* and possibly the *msg* arguments are given,
|
||||
return a context manager so that the code under test can be written
|
||||
inline rather than as a function::
|
||||
|
||||
with self.assertRaises(SomeException):
|
||||
do_something()
|
||||
|
||||
When used as a context manager, :meth:`assertRaises` accepts the
|
||||
additional keyword argument *msg*.
|
||||
|
||||
The context manager will store the caught exception object in its
|
||||
:attr:`exception` attribute. This can be useful if the intention
|
||||
is to perform additional checks on the exception raised::
|
||||
|
@ -988,9 +993,12 @@ Test cases
|
|||
.. versionchanged:: 3.2
|
||||
Added the :attr:`exception` attribute.
|
||||
|
||||
.. versionchanged:: 3.3
|
||||
Added the *msg* keyword argument when used as a context manager.
|
||||
|
||||
|
||||
.. method:: assertRaisesRegex(exception, regex, callable, *args, **kwds)
|
||||
assertRaisesRegex(exception, regex)
|
||||
assertRaisesRegex(exception, regex, msg=None)
|
||||
|
||||
Like :meth:`assertRaises` but also tests that *regex* matches
|
||||
on the string representation of the raised exception. *regex* may be
|
||||
|
@ -1007,12 +1015,16 @@ Test cases
|
|||
|
||||
.. versionadded:: 3.1
|
||||
under the name ``assertRaisesRegexp``.
|
||||
|
||||
.. versionchanged:: 3.2
|
||||
Renamed to :meth:`assertRaisesRegex`.
|
||||
|
||||
.. versionchanged:: 3.3
|
||||
Added the *msg* keyword argument when used as a context manager.
|
||||
|
||||
|
||||
.. method:: assertWarns(warning, callable, *args, **kwds)
|
||||
assertWarns(warning)
|
||||
assertWarns(warning, msg=None)
|
||||
|
||||
Test that a warning is triggered when *callable* is called with any
|
||||
positional or keyword arguments that are also passed to
|
||||
|
@ -1021,12 +1033,16 @@ Test cases
|
|||
To catch any of a group of warnings, a tuple containing the warning
|
||||
classes may be passed as *warnings*.
|
||||
|
||||
If only the *warning* argument is given, returns a context manager so
|
||||
that the code under test can be written inline rather than as a function::
|
||||
If only the *warning* and possibly the *msg* arguments are given,
|
||||
returns a context manager so that the code under test can be written
|
||||
inline rather than as a function::
|
||||
|
||||
with self.assertWarns(SomeWarning):
|
||||
do_something()
|
||||
|
||||
When used as a context manager, :meth:`assertRaises` accepts the
|
||||
additional keyword argument *msg*.
|
||||
|
||||
The context manager will store the caught warning object in its
|
||||
:attr:`warning` attribute, and the source line which triggered the
|
||||
warnings in the :attr:`filename` and :attr:`lineno` attributes.
|
||||
|
@ -1044,9 +1060,12 @@ Test cases
|
|||
|
||||
.. versionadded:: 3.2
|
||||
|
||||
.. versionchanged:: 3.3
|
||||
Added the *msg* keyword argument when used as a context manager.
|
||||
|
||||
|
||||
.. method:: assertWarnsRegex(warning, regex, callable, *args, **kwds)
|
||||
assertWarnsRegex(warning, regex)
|
||||
assertWarnsRegex(warning, regex, msg=None)
|
||||
|
||||
Like :meth:`assertWarns` but also tests that *regex* matches on the
|
||||
message of the triggered warning. *regex* may be a regular expression
|
||||
|
@ -1064,6 +1083,8 @@ Test cases
|
|||
|
||||
.. versionadded:: 3.2
|
||||
|
||||
.. versionchanged:: 3.3
|
||||
Added the *msg* keyword argument when used as a context manager.
|
||||
|
||||
|
||||
There are also other methods used to perform more specific checks, such as:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue