Debian Codegraph @

testing/test_setuponly.py

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