Debian Codegraph @

testing/web-platform/tests/tools/third_party/pytest/testing/test_setuponly.py

raw
1 # mypy: allow-untyped-defs
2 import sys
3
4 from _pytest.config import ExitCode
5 from _pytest.pytester import Pytester
6 import pytest
7
8
9 @pytest.fixture(params=["--setup-only", "--setup-plan", "--setup-show"], scope="module")
10 def mode(request):
11 return request.param
12
13
14 def test_show_only_active_fixtures(
15 pytester: Pytester, mode, dummy_yaml_custom_test
16 ) -> None:
17 pytester.makepyfile(
18 '''
19 import pytest
20 @pytest.fixture
21 def _arg0():
22 """hidden arg0 fixture"""
23 @pytest.fixture
24 def arg1():
25 """arg1 docstring"""
26 def test_arg1(arg1):
27 pass
28 '''
29 )
30
31 result = pytester.runpytest(mode)
32 assert result.ret == 0
33
34 result.stdout.fnmatch_lines(
35 ["*SETUP F arg1*", "*test_arg1 (fixtures used: arg1)*", "*TEARDOWN F arg1*"]
36 )
37 result.stdout.no_fnmatch_line("*_arg0*")
38
39
40 def test_show_different_scopes(pytester: Pytester, mode) -> None:
41 p = pytester.makepyfile(
42 '''
43 import pytest
44 @pytest.fixture
45 def arg_function():
46 """function scoped fixture"""
47 @pytest.fixture(scope='session')
48 def arg_session():
49 """session scoped fixture"""
50 def test_arg1(arg_session, arg_function):
51 pass
52 '''
53 )
54
55 result = pytester.runpytest(mode, p)
56 assert result.ret == 0
57
58 result.stdout.fnmatch_lines(
59 [
60 "SETUP S arg_session*",
61 "*SETUP F arg_function*",
62 "*test_arg1 (fixtures used: arg_function, arg_session)*",
63 "*TEARDOWN F arg_function*",
64 "TEARDOWN S arg_session*",
65 ]
66 )
67
68
69 def test_show_nested_fixtures(pytester: Pytester, mode) -> None:
70 pytester.makeconftest(
71 '''
72 import pytest
73 @pytest.fixture(scope='session')
74 def arg_same():
75 """session scoped fixture"""
76 '''
77 )
78 p = pytester.makepyfile(
79 '''
80 import pytest
81 @pytest.fixture(scope='function')
82 def arg_same(arg_same):
83 """function scoped fixture"""
84 def test_arg1(arg_same):
85 pass
86 '''
87 )
88
89 result = pytester.runpytest(mode, p)
90 assert result.ret == 0
91
92 result.stdout.fnmatch_lines(
93 [
94 "SETUP S arg_same*",
95 "*SETUP F arg_same (fixtures used: arg_same)*",
96 "*test_arg1 (fixtures used: arg_same)*",
97 "*TEARDOWN F arg_same*",
98 "TEARDOWN S arg_same*",
99 ]
100 )
101
102
103 def test_show_fixtures_with_autouse(pytester: Pytester, mode) -> None:
104 p = pytester.makepyfile(
105 '''
106 import pytest
107 @pytest.fixture
108 def arg_function():
109 """function scoped fixture"""
110 @pytest.fixture(scope='session', autouse=True)
111 def arg_session():
112 """session scoped fixture"""
113 def test_arg1(arg_function):
114 pass
115 '''
116 )
117
118 result = pytester.runpytest(mode, p)
119 assert result.ret == 0
120
121 result.stdout.fnmatch_lines(
122 [
123 "SETUP S arg_session*",
124 "*SETUP F arg_function*",
125 "*test_arg1 (fixtures used: arg_function, arg_session)*",
126 ]
127 )
128
129
130 def test_show_fixtures_with_parameters(pytester: Pytester, mode) -> None:
131 pytester.makeconftest(
132 '''
133 import pytest
134 @pytest.fixture(scope='session', params=['foo', 'bar'])
135 def arg_same():
136 """session scoped fixture"""
137 '''
138 )
139 p = pytester.makepyfile(
140 '''
141 import pytest
142 @pytest.fixture(scope='function')
143 def arg_other(arg_same):
144 """function scoped fixture"""
145 def test_arg1(arg_other):
146 pass
147 '''
148 )
149
150 result = pytester.runpytest(mode, p)
151 assert result.ret == 0
152
153 result.stdout.fnmatch_lines(
154 [
155 "SETUP S arg_same?'foo'?",
156 "TEARDOWN S arg_same?'foo'?",
157 "SETUP S arg_same?'bar'?",
158 "TEARDOWN S arg_same?'bar'?",
159 ]
160 )
161
162
163 def test_show_fixtures_with_parameter_ids(pytester: Pytester, mode) -> None:
164 pytester.makeconftest(
165 '''
166 import pytest
167 @pytest.fixture(
168 scope='session', params=['foo', 'bar'], ids=['spam', 'ham'])
169 def arg_same():
170 """session scoped fixture"""
171 '''
172 )
173 p = pytester.makepyfile(
174 '''
175 import pytest
176 @pytest.fixture(scope='function')
177 def arg_other(arg_same):
178 """function scoped fixture"""
179 def test_arg1(arg_other):
180 pass
181 '''
182 )
183
184 result = pytester.runpytest(mode, p)
185 assert result.ret == 0
186
187 result.stdout.fnmatch_lines(
188 ["SETUP S arg_same?'spam'?", "SETUP S arg_same?'ham'?"]
189 )
190
191
192 def test_show_fixtures_with_parameter_ids_function(pytester: Pytester, mode) -> None:
193 p = pytester.makepyfile(
194 """
195 import pytest
196 @pytest.fixture(params=['foo', 'bar'], ids=lambda p: p.upper())
197 def foobar():
198 pass
199 def test_foobar(foobar):
200 pass
201 """
202 )
203
204 result = pytester.runpytest(mode, p)
205 assert result.ret == 0
206
207 result.stdout.fnmatch_lines(
208 ["*SETUP F foobar?'FOO'?", "*SETUP F foobar?'BAR'?"]
209 )
210
211
212 def test_dynamic_fixture_request(pytester: Pytester) -> None:
213 p = pytester.makepyfile(
214 """
215 import pytest
216 @pytest.fixture()
217 def dynamically_requested_fixture():
218 pass
219 @pytest.fixture()
220 def dependent_fixture(request):
221 request.getfixturevalue('dynamically_requested_fixture')
222 def test_dyn(dependent_fixture):
223 pass
224 """
225 )
226
227 result = pytester.runpytest("--setup-only", p)
228 assert result.ret == 0
229
230 result.stdout.fnmatch_lines(
231 [
232 "*SETUP F dynamically_requested_fixture",
233 "*TEARDOWN F dynamically_requested_fixture",
234 ]
235 )
236
237
238 def test_capturing(pytester: Pytester) -> None:
239 p = pytester.makepyfile(
240 """
241 import pytest, sys
242 @pytest.fixture()
243 def one():
244 sys.stdout.write('this should be captured')
245 sys.stderr.write('this should also be captured')
246 @pytest.fixture()
247 def two(one):
248 assert 0
249 def test_capturing(two):
250 pass
251 """
252 )
253
254 result = pytester.runpytest("--setup-only", p)
255 result.stdout.fnmatch_lines(
256 ["this should be captured", "this should also be captured"]
257 )
258
259
260 def test_show_fixtures_and_execute_test(pytester: Pytester) -> None:
261 """Verify that setups are shown and tests are executed."""
262 p = pytester.makepyfile(
263 """
264 import pytest
265 @pytest.fixture
266 def arg():
267 assert True
268 def test_arg(arg):
269 assert False
270 """
271 )
272
273 result = pytester.runpytest("--setup-show", p)
274 assert result.ret == 1
275
276 result.stdout.fnmatch_lines(
277 ["*SETUP F arg*", "*test_arg (fixtures used: arg)F*", "*TEARDOWN F arg*"]
278 )
279
280
281 def test_setup_show_with_KeyboardInterrupt_in_test(pytester: Pytester) -> None:
282 p = pytester.makepyfile(
283 """
284 import pytest
285 @pytest.fixture
286 def arg():
287 pass
288 def test_arg(arg):
289 raise KeyboardInterrupt()
290 """
291 )
292 result = pytester.runpytest("--setup-show", p, no_reraise_ctrlc=True)
293 result.stdout.fnmatch_lines(
294 [
295 "*SETUP F arg*",
296 "*test_arg (fixtures used: arg)*",
297 "*TEARDOWN F arg*",
298 "*! KeyboardInterrupt !*",
299 "*= no tests ran in *",
300 ]
301 )
302 assert result.ret == ExitCode.INTERRUPTED
303
304
305 def test_show_fixture_action_with_bytes(pytester: Pytester) -> None:
306 # Issue 7126, BytesWarning when using --setup-show with bytes parameter
307 test_file = pytester.makepyfile(
308 """
309 import pytest
310
311 @pytest.mark.parametrize('data', [b'Hello World'])
312 def test_data(data):
313 pass
314 """
315 )
316 result = pytester.run(
317 sys.executable, "-bb", "-m", "pytest", "--setup-show", str(test_file)
318 )
319 assert result.ret == 0