-
Notifications
You must be signed in to change notification settings - Fork 54
/
test_warnings.py
290 lines (247 loc) · 7.97 KB
/
test_warnings.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# -*- coding: utf8 -*-
from __future__ import unicode_literals
import sys
import pytest
WARNINGS_SUMMARY_HEADER = "warnings summary"
@pytest.fixture
def pyfile_with_warnings(testdir, request):
"""
Create a test file which calls a function in a module which generates warnings.
"""
testdir.syspathinsert()
test_name = request.function.__name__
module_name = test_name.lstrip("test_") + "_module"
testdir.makepyfile(
**{
module_name: """
import warnings
def foo():
warnings.warn(UserWarning("user warning"))
warnings.warn(RuntimeWarning("runtime warning"))
return 1
""",
test_name: """
import {module_name}
def test_func():
assert {module_name}.foo() == 1
""".format(
module_name=module_name
),
}
)
@pytest.mark.filterwarnings("always")
def test_normal_flow(testdir, pyfile_with_warnings):
"""
Check that the warnings section is displayed, containing test node ids followed by
all warnings generated by that test node.
"""
result = testdir.runpytest()
result.stdout.fnmatch_lines(
[
"*== %s ==*" % WARNINGS_SUMMARY_HEADER,
"*test_normal_flow.py::test_func",
"*normal_flow_module.py:3: UserWarning: user warning",
'* warnings.warn(UserWarning("user warning"))',
"*normal_flow_module.py:4: RuntimeWarning: runtime warning",
'* warnings.warn(RuntimeWarning("runtime warning"))',
"* 1 passed, 2 warnings*",
]
)
assert result.stdout.str().count("test_normal_flow.py::test_func") == 1
@pytest.mark.filterwarnings("always")
def test_setup_teardown_warnings(testdir, pyfile_with_warnings):
testdir.makepyfile(
"""
import warnings
import pytest
@pytest.fixture
def fix():
warnings.warn(UserWarning("warning during setup"))
yield
warnings.warn(UserWarning("warning during teardown"))
def test_func(fix):
pass
"""
)
result = testdir.runpytest()
result.stdout.fnmatch_lines(
[
"*== %s ==*" % WARNINGS_SUMMARY_HEADER,
"*test_setup_teardown_warnings.py:6: UserWarning: warning during setup",
'*warnings.warn(UserWarning("warning during setup"))',
"*test_setup_teardown_warnings.py:8: UserWarning: warning during teardown",
'*warnings.warn(UserWarning("warning during teardown"))',
"* 1 passed, 2 warnings*",
]
)
@pytest.mark.parametrize("method", ["cmdline", "ini"])
def test_as_errors(testdir, pyfile_with_warnings, method):
args = ("-W", "error") if method == "cmdline" else ()
if method == "ini":
testdir.makeini(
"""
[pytest]
filterwarnings= error
"""
)
result = testdir.runpytest(*args)
result.stdout.fnmatch_lines(
[
"E UserWarning: user warning",
"as_errors_module.py:3: UserWarning",
"* 1 failed in *",
]
)
@pytest.mark.parametrize("method", ["cmdline", "ini"])
def test_ignore(testdir, pyfile_with_warnings, method):
args = ("-W", "ignore") if method == "cmdline" else ()
if method == "ini":
testdir.makeini(
"""
[pytest]
filterwarnings= ignore
"""
)
result = testdir.runpytest(*args)
result.stdout.fnmatch_lines(["* 1 passed in *"])
assert WARNINGS_SUMMARY_HEADER not in result.stdout.str()
@pytest.mark.skipif(
sys.version_info < (3, 0), reason="warnings message is unicode is ok in python3"
)
@pytest.mark.filterwarnings("always")
def test_unicode(testdir, pyfile_with_warnings):
testdir.makepyfile(
"""
# -*- coding: utf8 -*-
import warnings
import pytest
@pytest.fixture
def fix():
warnings.warn(u"测试")
yield
def test_func(fix):
pass
"""
)
result = testdir.runpytest()
result.stdout.fnmatch_lines(
[
"*== %s ==*" % WARNINGS_SUMMARY_HEADER,
"*test_unicode.py:8: UserWarning: \u6d4b\u8bd5*",
"* 1 passed, 1 warnings*",
]
)
@pytest.mark.skipif(
sys.version_info >= (3, 0),
reason="warnings message is broken as it is not str instance",
)
def test_py2_unicode(testdir, pyfile_with_warnings):
if (
getattr(sys, "pypy_version_info", ())[:2] == (5, 9)
and sys.platform.startswith("win")
):
pytest.xfail("fails with unicode error on PyPy2 5.9 and Windows (#2905)")
testdir.makepyfile(
"""
# -*- coding: utf8 -*-
import warnings
import pytest
@pytest.fixture
def fix():
warnings.warn(u"测试")
yield
@pytest.mark.filterwarnings('always')
def test_func(fix):
pass
"""
)
result = testdir.runpytest()
result.stdout.fnmatch_lines(
[
"*== %s ==*" % WARNINGS_SUMMARY_HEADER,
"*test_py2_unicode.py:8: UserWarning: \\u6d4b\\u8bd5",
'*warnings.warn(u"\u6d4b\u8bd5")',
"*warnings.py:*: UnicodeWarning: Warning is using unicode non*",
"* 1 passed, 2 warnings*",
]
)
def test_py2_unicode_ascii(testdir):
"""Ensure that our warning about 'unicode warnings containing non-ascii messages'
does not trigger with ascii-convertible messages"""
testdir.makeini("[pytest]")
testdir.makepyfile(
"""
import pytest
import warnings
@pytest.mark.filterwarnings('always')
def test_func():
warnings.warn(u"hello")
"""
)
result = testdir.runpytest()
result.stdout.fnmatch_lines(
[
"*== %s ==*" % WARNINGS_SUMMARY_HEADER,
'*warnings.warn(u"hello")',
"* 1 passed, 1 warnings in*",
]
)
def test_works_with_filterwarnings(testdir):
"""Ensure our warnings capture does not mess with pre-installed filters (#2430)."""
testdir.makepyfile(
"""
import warnings
class MyWarning(Warning):
pass
warnings.filterwarnings("error", category=MyWarning)
class TestWarnings(object):
def test_my_warning(self):
try:
warnings.warn(MyWarning("warn!"))
assert False
except MyWarning:
assert True
"""
)
result = testdir.runpytest()
result.stdout.fnmatch_lines(["*== 1 passed in *"])
@pytest.mark.parametrize("default_config", ["ini", "cmdline"])
def test_filterwarnings_mark(testdir, default_config):
"""
Test ``filterwarnings`` mark works and takes precedence over command line and ini options.
"""
if default_config == "ini":
testdir.makeini(
"""
[pytest]
filterwarnings = always
"""
)
testdir.makepyfile(
"""
import warnings
import pytest
@pytest.mark.filterwarnings('ignore::RuntimeWarning')
def test_ignore_runtime_warning():
warnings.warn(RuntimeWarning())
@pytest.mark.filterwarnings('error')
def test_warning_error():
warnings.warn(RuntimeWarning())
def test_show_warning():
warnings.warn(RuntimeWarning())
"""
)
result = testdir.runpytest("-W always" if default_config == "cmdline" else "")
result.stdout.fnmatch_lines(["*= 1 failed, 2 passed, 1 warnings in *"])
def test_non_string_warning_argument(testdir):
"""Non-str argument passed to warning breaks pytest (#2956)"""
testdir.makepyfile(
"""
import warnings
import pytest
def test():
warnings.warn(UserWarning(1, u'foo'))
"""
)
result = testdir.runpytest("-W", "always")
result.stdout.fnmatch_lines(["*= 1 passed, 1 warnings in *"])