2010-10-11 05:45:45 +08:00
|
|
|
|
2015-07-29 07:01:11 +08:00
|
|
|
Asserting Warnings
|
2010-10-11 05:45:45 +08:00
|
|
|
=====================================================
|
|
|
|
|
2015-07-29 07:01:11 +08:00
|
|
|
.. _warns:
|
2013-06-03 22:07:14 +08:00
|
|
|
|
2015-07-29 07:01:11 +08:00
|
|
|
Asserting warnings with the warns function
|
|
|
|
-----------------------------------------------
|
2010-10-11 05:45:45 +08:00
|
|
|
|
2015-07-29 07:01:11 +08:00
|
|
|
.. versionadded:: 2.8
|
|
|
|
|
|
|
|
You can check that code raises a particular warning using ``pytest.warns``,
|
|
|
|
which works in a similar manner to :ref:`raises <assertraises>`::
|
|
|
|
|
|
|
|
import warnings
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
def test_warning():
|
|
|
|
with pytest.warns(UserWarning):
|
|
|
|
warnings.warn("my warning", UserWarning)
|
|
|
|
|
|
|
|
The test will fail if the warning in question is not raised.
|
|
|
|
|
|
|
|
You can also call ``pytest.warns`` on a function or code string::
|
|
|
|
|
|
|
|
pytest.warns(expected_warning, func, *args, **kwargs)
|
|
|
|
pytest.warns(expected_warning, "func(*args, **kwargs)")
|
|
|
|
|
|
|
|
The function also returns a list of all raised warnings (as
|
|
|
|
``warnings.WarningMessage`` objects), which you can query for
|
|
|
|
additional information::
|
|
|
|
|
|
|
|
with pytest.warns(RuntimeWarning) as record:
|
|
|
|
warnings.warn("another warning", RuntimeWarning)
|
|
|
|
|
|
|
|
# check that only one warning was raised
|
|
|
|
assert len(record) == 1
|
|
|
|
# check that the message matches
|
|
|
|
assert record[0].message.args[0] == "another warning"
|
|
|
|
|
|
|
|
Alternatively, you can examine raised warnings in detail using the
|
|
|
|
:ref:`recwarn <recwarn>` fixture (see below).
|
|
|
|
|
2015-12-05 23:53:27 +08:00
|
|
|
.. note::
|
|
|
|
``DeprecationWarning`` and ``PendingDeprecationWarning`` are treated
|
|
|
|
differently; see :ref:`ensuring_function_triggers`.
|
|
|
|
|
2015-07-29 07:01:11 +08:00
|
|
|
.. _recwarn:
|
|
|
|
|
|
|
|
Recording warnings
|
|
|
|
------------------------
|
|
|
|
|
|
|
|
You can record raised warnings either using ``pytest.warns`` or with
|
|
|
|
the ``recwarn`` fixture.
|
|
|
|
|
|
|
|
To record with ``pytest.warns`` without asserting anything about the warnings,
|
|
|
|
pass ``None`` as the expected warning type::
|
|
|
|
|
|
|
|
with pytest.warns(None) as record:
|
|
|
|
warnings.warn("user", UserWarning)
|
|
|
|
warnings.warn("runtime", RuntimeWarning)
|
|
|
|
|
|
|
|
assert len(record) == 2
|
|
|
|
assert str(record[0].message) == "user"
|
|
|
|
assert str(record[1].message) == "runtime"
|
|
|
|
|
|
|
|
The ``recwarn`` fixture will record warnings for the whole function::
|
|
|
|
|
|
|
|
import warnings
|
2010-10-11 05:45:45 +08:00
|
|
|
|
|
|
|
def test_hello(recwarn):
|
2015-07-29 07:01:11 +08:00
|
|
|
warnings.warn("hello", UserWarning)
|
|
|
|
assert len(recwarn) == 1
|
|
|
|
w = recwarn.pop(UserWarning)
|
|
|
|
assert issubclass(w.category, UserWarning)
|
|
|
|
assert str(w.message) == "hello"
|
2010-10-11 05:45:45 +08:00
|
|
|
assert w.filename
|
|
|
|
assert w.lineno
|
|
|
|
|
2015-07-29 07:01:11 +08:00
|
|
|
Both ``recwarn`` and ``pytest.warns`` return the same interface for recorded
|
|
|
|
warnings: a WarningsRecorder instance. To view the recorded warnings, you can
|
|
|
|
iterate over this instance, call ``len`` on it to get the number of recorded
|
|
|
|
warnings, or index into it to get a particular recorded warning. It also
|
|
|
|
provides these methods:
|
2010-10-11 05:45:45 +08:00
|
|
|
|
2015-07-29 07:01:11 +08:00
|
|
|
.. autoclass:: _pytest.recwarn.WarningsRecorder()
|
|
|
|
:members:
|
2015-07-10 08:50:38 +08:00
|
|
|
|
2015-07-29 07:01:11 +08:00
|
|
|
Each recorded warning has the attributes ``message``, ``category``,
|
|
|
|
``filename``, ``lineno``, ``file``, and ``line``. The ``category`` is the
|
|
|
|
class of the warning. The ``message`` is the warning itself; calling
|
|
|
|
``str(message)`` will return the actual message of the warning.
|
2010-10-11 05:45:45 +08:00
|
|
|
|
2015-12-05 23:53:27 +08:00
|
|
|
.. note::
|
|
|
|
``DeprecationWarning`` and ``PendingDeprecationWarning`` are treated
|
|
|
|
differently; see :ref:`ensuring_function_triggers`.
|
2013-06-03 22:07:14 +08:00
|
|
|
|
|
|
|
.. _ensuring_function_triggers:
|
|
|
|
|
2011-09-06 17:43:42 +08:00
|
|
|
Ensuring a function triggers a deprecation warning
|
2010-10-11 05:45:45 +08:00
|
|
|
-------------------------------------------------------
|
|
|
|
|
|
|
|
You can also call a global helper for checking
|
2015-12-05 23:53:27 +08:00
|
|
|
that a certain function call triggers a ``DeprecationWarning`` or
|
|
|
|
``PendingDeprecationWarning``::
|
2010-10-11 05:45:45 +08:00
|
|
|
|
2010-11-18 05:12:16 +08:00
|
|
|
import pytest
|
2010-10-11 05:45:45 +08:00
|
|
|
|
|
|
|
def test_global():
|
2010-11-18 05:12:16 +08:00
|
|
|
pytest.deprecated_call(myfunction, 17)
|
2015-07-29 07:01:11 +08:00
|
|
|
|
2015-12-05 23:53:27 +08:00
|
|
|
By default, ``DeprecationWarning`` and ``PendingDeprecationWarning`` will not be
|
|
|
|
caught when using ``pytest.warns`` or ``recwarn`` because default Python warnings filters hide
|
|
|
|
them. If you wish to record them in your own code, use the
|
2015-07-29 07:01:11 +08:00
|
|
|
command ``warnings.simplefilter('always')``::
|
|
|
|
|
|
|
|
import warnings
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
def test_deprecation(recwarn):
|
|
|
|
warnings.simplefilter('always')
|
|
|
|
warnings.warn("deprecated", DeprecationWarning)
|
|
|
|
assert len(recwarn) == 1
|
|
|
|
assert recwarn.pop(DeprecationWarning)
|