Improve warnings docs (#6366)

Improve warnings docs
This commit is contained in:
Bruno Oliveira 2019-12-26 19:52:15 -03:00 committed by GitHub
commit b532d15fe7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 14 additions and 15 deletions

View File

@ -198,7 +198,7 @@ the regular expression ``".*U.*mode is deprecated"``.
Ensuring code triggers a deprecation warning Ensuring code triggers a deprecation warning
-------------------------------------------- --------------------------------------------
You can also call a global helper for checking You can also use :func:`pytest.deprecated_call` for checking
that a certain function call triggers a ``DeprecationWarning`` or that a certain function call triggers a ``DeprecationWarning`` or
``PendingDeprecationWarning``: ``PendingDeprecationWarning``:
@ -207,13 +207,18 @@ that a certain function call triggers a ``DeprecationWarning`` or
import pytest import pytest
def test_global(): def test_myfunction_deprecated():
pytest.deprecated_call(myfunction, 17) with pytest.deprecated_call():
myfunction(17)
This test will fail if ``myfunction`` does not issue a deprecation warning
when called with a ``17`` argument.
By default, ``DeprecationWarning`` and ``PendingDeprecationWarning`` will not be By default, ``DeprecationWarning`` and ``PendingDeprecationWarning`` will not be
caught when using ``pytest.warns`` or ``recwarn`` because default Python warnings filters hide caught when using :func:`pytest.warns` or :ref:`recwarn <recwarn>` because
them. If you wish to record them in your own code, use the the default Python warnings filters hide
command ``warnings.simplefilter('always')``: them. If you wish to record them in your own code, use
``warnings.simplefilter('always')``:
.. code-block:: python .. code-block:: python
@ -223,19 +228,13 @@ command ``warnings.simplefilter('always')``:
def test_deprecation(recwarn): def test_deprecation(recwarn):
warnings.simplefilter("always") warnings.simplefilter("always")
warnings.warn("deprecated", DeprecationWarning) myfunction(17)
assert len(recwarn) == 1 assert len(recwarn) == 1
assert recwarn.pop(DeprecationWarning) assert recwarn.pop(DeprecationWarning)
You can also use it as a contextmanager:
.. code-block:: python
def test_global():
with pytest.deprecated_call():
myobject.deprecated_method()
The :ref:`recwarn <recwarn>` fixture automatically ensures to reset the warnings
filter at the end of the test, so no global state is leaked.
.. _`asserting warnings`: .. _`asserting warnings`: