You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Although not recommended, you can use the ``--disable-warnings`` command-line option to suppress the
82
128
warning summary entirely from the test run output.
@@ -103,10 +149,14 @@ DeprecationWarning and PendingDeprecationWarning
103
149
.. versionadded:: 3.8
104
150
.. versionchanged:: 3.9
105
151
106
-
By default pytest will display ``DeprecationWarning`` and ``PendingDeprecationWarning``.
152
+
By default pytest will display ``DeprecationWarning`` and ``PendingDeprecationWarning`` warnings from
153
+
user code and third-party libraries, as recommended by `PEP-0506 <https://www.python.org/dev/peps/pep-0565>`_.
154
+
This helps users keep their code modern and avoid breakages when deprecated warnings are effectively removed.
107
155
108
156
Sometimes it is useful to hide some specific deprecation warnings that happen in code that you have no control over
109
-
(such as third-party libraries), in which case you might use the standard warning filters options (ini or marks).
157
+
(such as third-party libraries), in which case you might use the warning filters options (ini or marks) to ignore
158
+
those warnings.
159
+
110
160
For example:
111
161
112
162
.. code-block:: ini
@@ -116,83 +166,57 @@ For example:
116
166
ignore:.*U.*mode is deprecated:DeprecationWarning
117
167
118
168
169
+
This will ignore all warnings of type ``DeprecationWarning`` where the start of the message matches
170
+
the regular expression ``".*U.*mode is deprecated"``.
171
+
119
172
.. note::
120
173
If warnings are configured at the interpreter level, using
121
174
the `PYTHONWARNINGS <https://docs.python.org/3/using/cmdline.html#envvar-PYTHONWARNINGS>`_ environment variable or the
122
175
``-W`` command-line option, pytest will not configure any filters by default.
123
176
124
-
.. note::
125
-
This feature makes pytest more compliant with `PEP-0506 <https://www.python.org/dev/peps/pep-0565/#recommended-filter-settings-for-test-runners>`_ which suggests that those warnings should
126
-
be shown by default by test runners, but pytest doesn't follow ``PEP-0506`` completely because resetting all
127
-
warning filters like suggested in the PEP will break existing test suites that configure warning filters themselves
177
+
Also pytest doesn't follow ``PEP-0506`` suggestion of resetting all warning filters because
178
+
it might break test suites that configure warning filters themselves
128
179
by calling ``warnings.simplefilter`` (see issue `#2430 <https://github.com/pytest-dev/pytest/issues/2430>`_
129
180
for an example of that).
130
181
131
182
132
-
.. _`filterwarnings`:
133
-
134
-
``@pytest.mark.filterwarnings``
135
-
-------------------------------
136
-
137
-
.. versionadded:: 3.2
138
-
139
-
You can use the ``@pytest.mark.filterwarnings`` to add warning filters to specific test items,
140
-
allowing you to have finer control of which warnings should be captured at test, class or
141
-
even module level:
142
-
143
-
.. code-block:: python
144
-
145
-
import warnings
146
-
147
-
148
-
defapi_v1():
149
-
warnings.warn(UserWarning("api v1, should use functions from v2"))
150
-
return1
151
-
152
-
153
-
@pytest.mark.filterwarnings("ignore:api v1")
154
-
deftest_one():
155
-
assert api_v1() ==1
156
-
157
-
158
-
Filters applied using a mark take precedence over filters passed on the command line or configured
159
-
by the ``filterwarnings`` ini option.
183
+
.. _`ensuring a function triggers a deprecation warning`:
160
184
161
-
You may apply a filter to all tests of a class by using the ``filterwarnings`` mark as a class
162
-
decorator or to all tests in a module by setting the ``pytestmark`` variable:
185
+
.. _ensuring_function_triggers:
163
186
164
-
.. code-block:: python
187
+
Ensuring code triggers a deprecation warning
188
+
--------------------------------------------
165
189
166
-
# turns all warnings into errors for this module
167
-
pytestmark = pytest.mark.filterwarnings("error")
190
+
You can also call a global helper for checking
191
+
that a certain function call triggers a ``DeprecationWarning`` or
192
+
``PendingDeprecationWarning``::
168
193
194
+
import pytest
169
195
170
-
.. note::
196
+
def test_global():
197
+
pytest.deprecated_call(myfunction, 17)
171
198
172
-
Except for these features, pytest does not change the python warning filter; it only captures
173
-
and displays the warnings which are issued with respect to the currently configured filter,
174
-
including changes to the filter made by test functions or by the system under test.
199
+
By default, ``DeprecationWarning`` and ``PendingDeprecationWarning`` will not be
200
+
caught when using ``pytest.warns`` or ``recwarn`` because default Python warnings filters hide
201
+
them. If you wish to record them in your own code, use the
202
+
command ``warnings.simplefilter('always')``::
175
203
176
-
.. note::
204
+
import warnings
205
+
import pytest
177
206
178
-
``DeprecationWarning`` and ``PendingDeprecationWarning`` are hidden by the standard library
179
-
by default so you have to explicitly configure them to be displayed in your ``pytest.ini``:
207
+
def test_deprecation(recwarn):
208
+
warnings.simplefilter('always')
209
+
warnings.warn("deprecated", DeprecationWarning)
210
+
assert len(recwarn) == 1
211
+
assert recwarn.pop(DeprecationWarning)
180
212
181
-
.. code-block:: ini
213
+
You can also use it as a contextmanager::
182
214
183
-
[pytest]
184
-
filterwarnings =
185
-
once::DeprecationWarning
186
-
once::PendingDeprecationWarning
215
+
def test_global():
216
+
with pytest.deprecated_call():
217
+
myobject.deprecated_method()
187
218
188
219
189
-
*Credits go to Florian Schulze for the reference implementation in the* `pytest-warnings`_
0 commit comments