@@ -5,32 +5,18 @@ Warnings Capture
5
5
6
6
.. versionadded :: 3.1
7
7
8
- .. warning ::
9
- pytest captures all warnings between tests, which prevents custom warning
10
- filters in existing test suites from working. If this causes problems to your test suite,
11
- this plugin can be disabled in your ``pytest.ini `` file with:
12
-
13
- .. code-block :: ini
14
-
15
- [pytest]
16
- addopts = -p no:warnings
17
-
18
- There's an ongoing discussion about this on `#2430
19
- <https://github.com/pytest-dev/pytest/issues/2430> `_.
20
-
21
-
22
- Starting from version ``3.1 ``, pytest now automatically catches all warnings during test execution
8
+ Starting from version ``3.1 ``, pytest now automatically catches warnings during test execution
23
9
and displays them at the end of the session::
24
10
25
11
# content of test_show_warnings.py
26
12
import warnings
27
13
28
- def deprecated_function ():
29
- warnings.warn("this function is deprecated, use another_function()", DeprecationWarning )
14
+ def api_v1 ():
15
+ warnings.warn(UserWarning("api v1, should use functions from v2") )
30
16
return 1
31
17
32
18
def test_one():
33
- assert deprecated_function () == 1
19
+ assert api_v1 () == 1
34
20
35
21
Running pytest now produces this output::
36
22
@@ -39,48 +25,50 @@ Running pytest now produces this output::
39
25
platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y
40
26
rootdir: $REGENDOC_TMPDIR, inifile:
41
27
collected 1 items
42
-
28
+
43
29
test_show_warnings.py .
44
-
30
+
45
31
======= warnings summary ========
46
32
test_show_warnings.py::test_one
47
33
$REGENDOC_TMPDIR/test_show_warnings.py:4: DeprecationWarning: this function is deprecated, use another_function()
48
34
warnings.warn("this function is deprecated, use another_function()", DeprecationWarning)
49
-
35
+
50
36
-- Docs: http://doc.pytest.org/en/latest/warnings.html
51
37
======= 1 passed, 1 warnings in 0.12 seconds ========
52
38
39
+ Pytest by default catches all warnings except for ``DeprecationWarning `` and ``PendingDeprecationWarning ``.
40
+
53
41
The ``-W `` flag can be passed to control which warnings will be displayed or even turn
54
42
them into errors::
55
43
56
- $ pytest -q test_show_warnings.py -W error::DeprecationWarning
44
+ $ pytest -q test_show_warnings.py -W error::UserWarning
57
45
F
58
46
======= FAILURES ========
59
47
_______ test_one ________
60
-
48
+
61
49
def test_one():
62
50
> assert deprecated_function() == 1
63
-
64
- test_show_warnings.py:8:
65
- _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
66
-
51
+
52
+ test_show_warnings.py:8:
53
+ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
54
+
67
55
def deprecated_function():
68
56
> warnings.warn("this function is deprecated, use another_function()", DeprecationWarning)
69
57
E DeprecationWarning: this function is deprecated, use another_function()
70
-
58
+
71
59
test_show_warnings.py:4: DeprecationWarning
72
60
1 failed in 0.12 seconds
73
61
74
62
The same option can be set in the ``pytest.ini `` file using the ``filterwarnings `` ini option.
75
- For example, the configuration below will ignore all deprecation warnings, but will transform
63
+ For example, the configuration below will ignore all user warnings, but will transform
76
64
all other warnings into errors.
77
65
78
66
.. code-block :: ini
79
67
80
68
[pytest]
81
69
filterwarnings =
82
70
error
83
- ignore::DeprecationWarning
71
+ ignore::UserWarning
84
72
85
73
86
74
When a warning matches more than one option in the list, the action for the last matching option
@@ -90,13 +78,39 @@ Both ``-W`` command-line option and ``filterwarnings`` ini option are based on P
90
78
`-W option `_ and `warnings.simplefilter `_, so please refer to those sections in the Python
91
79
documentation for other examples and advanced usage.
92
80
81
+ .. note ::
82
+
83
+ ``DeprecationWarning `` and ``PendingDeprecationWarning `` are hidden by the standard library
84
+ by default so you have to explicitly configure them to be displayed in your ``pytest.ini ``:
85
+
86
+ .. code-block :: ini
87
+
88
+ [pytest]
89
+ filterwarnings =
90
+ once::DeprecationWarning
91
+ once::PendingDeprecationWarning
92
+
93
+
93
94
*Credits go to Florian Schulze for the reference implementation in the * `pytest-warnings `_
94
95
*plugin. *
95
96
96
97
.. _`-W option` : https://docs.python.org/3/using/cmdline.html?highlight=#cmdoption-W
97
98
.. _warnings.simplefilter : https://docs.python.org/3/library/warnings.html#warnings.simplefilter
98
99
.. _`pytest-warnings` : https://github.com/fschulze/pytest-warnings
99
100
101
+
102
+ Disabling warning capture
103
+ -------------------------
104
+
105
+ This feature is enabled by default but can be disabled entirely in your ``pytest.ini `` file with:
106
+
107
+ .. code-block :: ini
108
+
109
+ [pytest]
110
+ addopts = -p no:warnings
111
+
112
+ Or passing ``-p no:warnings `` in the command-line.
113
+
100
114
.. _`asserting warnings` :
101
115
102
116
.. _assertwarnings :
0 commit comments