mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
A couple small grammar fixes in test.rst, and rewrite the
check_warnings docs to be clearer.
This commit is contained in:
parent
4fcf7d445c
commit
6c9fc4cf2b
1 changed files with 46 additions and 38 deletions
|
@ -235,15 +235,15 @@ The :mod:`test.test_support` module defines the following constants:
|
||||||
|
|
||||||
.. data:: TESTFN
|
.. data:: TESTFN
|
||||||
|
|
||||||
Set to the name that a temporary file could use. Any temporary file that is
|
Set to a name that is safe to use as the name of a temporary file. Any
|
||||||
created should be closed and unlinked (removed).
|
temporary file that is created should be closed and unlinked (removed).
|
||||||
|
|
||||||
The :mod:`test.test_support` module defines the following functions:
|
The :mod:`test.test_support` module defines the following functions:
|
||||||
|
|
||||||
|
|
||||||
.. function:: forget(module_name)
|
.. function:: forget(module_name)
|
||||||
|
|
||||||
Remove the module named *module_name* from ``sys.modules`` and deletes any
|
Remove the module named *module_name* from ``sys.modules`` and delete any
|
||||||
byte-compiled files of the module.
|
byte-compiled files of the module.
|
||||||
|
|
||||||
|
|
||||||
|
@ -286,49 +286,55 @@ The :mod:`test.test_support` module defines the following functions:
|
||||||
This will run all tests defined in the named module.
|
This will run all tests defined in the named module.
|
||||||
|
|
||||||
|
|
||||||
.. function:: check_warnings(*filters, quiet=None)
|
.. function:: check_warnings(*filters, quiet=True)
|
||||||
|
|
||||||
A convenience wrapper for ``warnings.catch_warnings()`` that makes
|
A convenience wrapper for :func:`warnings.catch_warnings()` that makes it
|
||||||
it easier to test that a warning was correctly raised with a single
|
easier to test that a warning was correctly raised. It is approximately
|
||||||
assertion. It is approximately equivalent to calling
|
equivalent to calling ``warnings.catch_warnings(record=True)`` with
|
||||||
``warnings.catch_warnings(record=True)``.
|
:meth:`warnings.simplefilter` set to ``always`` and with the option to
|
||||||
|
automatically validate the results that are recorded.
|
||||||
|
|
||||||
It accepts 2-tuples ``("message regexp", WarningCategory)`` as positional
|
``check_warnings`` accepts 2-tuples of the form ``("message regexp",
|
||||||
arguments. If there's some ``*filters`` defined, or if the optional keyword
|
WarningCategory)`` as positional arguments. If one or more *filters* are
|
||||||
argument ``quiet`` is :const:`False`, it checks if the warnings are
|
provided, or if the optional keyword argument *quiet* is :const:`False`,
|
||||||
effective. If some filter did not catch any warning, the test fails. If some
|
it checks to make sure the warnings are as expected: each specified filter
|
||||||
warnings are not caught, the test fails, too. To disable these checks, set
|
must match at least one of the warnings raised by the enclosed code or the
|
||||||
argument ``quiet`` to :const:`True`.
|
test fails, and if any warnings are raised that do not match any of the
|
||||||
|
specified filters the test fails. To disable the first of these checks,
|
||||||
|
set *quiet* to :const:`True`.
|
||||||
|
|
||||||
Without argument, it defaults to::
|
If no arguments are specified, it defaults to::
|
||||||
|
|
||||||
check_warnings(("", Warning), quiet=True)
|
check_warnings(("", Warning), quiet=True)
|
||||||
|
|
||||||
Additionally, on entry to the context manager, a :class:`WarningRecorder`
|
In this case all warnings are caught and no errors are raised.
|
||||||
instance is returned. The underlying warnings list is available via the
|
|
||||||
recorder object's :attr:`warnings` attribute, while the attributes of the
|
|
||||||
last raised warning are also accessible directly on the object. If no
|
|
||||||
warning has been raised, then the latter attributes will all be
|
|
||||||
:const:`None`.
|
|
||||||
|
|
||||||
A :meth:`reset` method is also provided on the recorder object. This
|
On entry to the context manager, a :class:`WarningRecorder` instance is
|
||||||
method simply clears the warnings list.
|
returned. The underlying warnings list from
|
||||||
|
:func:`~warnings.catch_warnings` is available via the recorder object's
|
||||||
|
:attr:`warnings` attribute. As a convenience, the attributes of the object
|
||||||
|
representing the most recent warning can also be accessed directly through
|
||||||
|
the recorder object (see example below). If no warning has been raised,
|
||||||
|
then any of the attributes that would otherwise be expected on an object
|
||||||
|
representing a warning will return :const:`None`.
|
||||||
|
|
||||||
The context manager may be used like this::
|
The recorder object also has a :meth:`reset` method, which clears the
|
||||||
|
warnings list.
|
||||||
|
|
||||||
import warnings
|
The context manager is designed to be used like this::
|
||||||
|
|
||||||
with check_warnings(quiet=False):
|
|
||||||
exec('assert(False, "Hey!")')
|
|
||||||
warnings.warn(UserWarning("Hide me!"))
|
|
||||||
|
|
||||||
with check_warnings(("assertion is always true", SyntaxWarning),
|
with check_warnings(("assertion is always true", SyntaxWarning),
|
||||||
("", UserWarning)):
|
("", UserWarning)):
|
||||||
exec('assert(False, "Hey!")')
|
exec('assert(False, "Hey!")')
|
||||||
warnings.warn(UserWarning("Hide me!"))
|
warnings.warn(UserWarning("Hide me!"))
|
||||||
|
|
||||||
|
In this case if either warning was not raised, or some other warning was
|
||||||
|
raised, :func:`check_warnings` would raise an error.
|
||||||
|
|
||||||
|
When a test needs to look more deeply into the warnings, rather than
|
||||||
|
just checking whether or not they occurred, code like this can be used::
|
||||||
|
|
||||||
with check_warnings(quiet=True) as w:
|
with check_warnings(quiet=True) as w:
|
||||||
warnings.simplefilter("always")
|
|
||||||
warnings.warn("foo")
|
warnings.warn("foo")
|
||||||
assert str(w.args[0]) == "foo"
|
assert str(w.args[0]) == "foo"
|
||||||
warnings.warn("bar")
|
warnings.warn("bar")
|
||||||
|
@ -338,21 +344,23 @@ The :mod:`test.test_support` module defines the following functions:
|
||||||
w.reset()
|
w.reset()
|
||||||
assert len(w.warnings) == 0
|
assert len(w.warnings) == 0
|
||||||
|
|
||||||
|
Here all warnings will be caught, and the test code tests the captured
|
||||||
|
warnings directly.
|
||||||
|
|
||||||
.. versionadded:: 2.6
|
.. versionadded:: 2.6
|
||||||
.. versionchanged:: 2.7
|
.. versionchanged:: 2.7
|
||||||
New optional attributes ``*filters`` and ``quiet``.
|
New optional arguments *filters* and *quiet*.
|
||||||
|
|
||||||
|
|
||||||
.. function:: check_py3k_warnings(*filters, quiet=False)
|
.. function:: check_py3k_warnings(*filters, quiet=False)
|
||||||
|
|
||||||
Same as :func:`check_warnings` but for Python 3 compatibility warnings.
|
Similar to :func:`check_warnings`, but for Python 3 compatibility warnings.
|
||||||
If ``sys.py3kwarning == 1``, it checks if the warning is effectively raised.
|
If ``sys.py3kwarning == 1``, it checks if the warning is effectively raised.
|
||||||
If ``sys.py3kwarning == 0``, it checks that no warning is raised.
|
If ``sys.py3kwarning == 0``, it checks that no warning is raised. It
|
||||||
|
accepts 2-tuples of the form ``("message regexp", WarningCategory)`` as
|
||||||
It accepts 2-tuples ``("message regexp", WarningCategory)`` as positional
|
positional arguments. When the optional keyword argument *quiet* is
|
||||||
arguments. When the optional keyword argument ``quiet`` is :const:`True`, it
|
:const:`True`, it does not fail if a filter catches nothing. Without
|
||||||
does not fail if a filter catches nothing. Without argument, it defaults
|
arguments, it defaults to::
|
||||||
to::
|
|
||||||
|
|
||||||
check_py3k_warnings(("", DeprecationWarning), quiet=False)
|
check_py3k_warnings(("", DeprecationWarning), quiet=False)
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue