-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
show_fixtures_per_test.py
325 lines (285 loc) · 9.43 KB
/
show_fixtures_per_test.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
from _pytest.pytester import Pytester
def test_should_show_no_ouput_when_zero_items(pytester: Pytester) -> None:
result = pytester.runpytest("--fixtures-per-test")
result.stdout.no_fnmatch_line("*fixtures used by*")
assert result.ret == 0
def test_fixtures_in_module(pytester: Pytester) -> None:
p = pytester.makepyfile(
'''
import pytest
@pytest.fixture
def _arg0():
"""hidden arg0 fixture"""
@pytest.fixture
def arg1():
"""arg1 docstring"""
def test_arg1(arg1):
pass
'''
)
result = pytester.runpytest("--fixtures-per-test", p)
assert result.ret == 0
result.stdout.fnmatch_lines(
[
"*fixtures used by test_arg1*",
"*(test_fixtures_in_module.py:9)*",
"arg1 -- test_fixtures_in_module.py:6",
" arg1 docstring",
]
)
result.stdout.no_fnmatch_line("*_arg0*")
def test_fixtures_in_conftest(pytester: Pytester) -> None:
pytester.makeconftest(
'''
import pytest
@pytest.fixture
def arg1():
"""arg1 docstring"""
@pytest.fixture
def arg2():
"""arg2 docstring"""
@pytest.fixture
def arg3(arg1, arg2):
"""arg3
docstring
"""
'''
)
p = pytester.makepyfile(
"""
def test_arg2(arg2):
pass
def test_arg3(arg3):
pass
"""
)
result = pytester.runpytest("--fixtures-per-test", p)
assert result.ret == 0
result.stdout.fnmatch_lines(
[
"*fixtures used by test_arg2*",
"*(test_fixtures_in_conftest.py:2)*",
"arg2 -- conftest.py:6",
" arg2 docstring",
"*fixtures used by test_arg3*",
"*(test_fixtures_in_conftest.py:4)*",
"arg1 -- conftest.py:3",
" arg1 docstring",
"arg2 -- conftest.py:6",
" arg2 docstring",
"arg3 -- conftest.py:9",
" arg3",
]
)
def test_should_show_fixtures_used_by_test(pytester: Pytester) -> None:
pytester.makeconftest(
'''
import pytest
@pytest.fixture
def arg1():
"""arg1 from conftest"""
@pytest.fixture
def arg2():
"""arg2 from conftest"""
'''
)
p = pytester.makepyfile(
'''
import pytest
@pytest.fixture
def arg1():
"""arg1 from testmodule"""
def test_args(arg1, arg2):
pass
'''
)
result = pytester.runpytest("--fixtures-per-test", p)
assert result.ret == 0
result.stdout.fnmatch_lines(
[
"*fixtures used by test_args*",
"*(test_should_show_fixtures_used_by_test.py:6)*",
"arg1 -- test_should_show_fixtures_used_by_test.py:3",
" arg1 from testmodule",
"arg2 -- conftest.py:6",
" arg2 from conftest",
]
)
def test_verbose_include_private_fixtures_and_loc(pytester: Pytester) -> None:
pytester.makeconftest(
'''
import pytest
@pytest.fixture
def _arg1():
"""_arg1 from conftest"""
@pytest.fixture
def arg2(_arg1):
"""arg2 from conftest"""
'''
)
p = pytester.makepyfile(
'''
import pytest
@pytest.fixture
def arg3():
"""arg3 from testmodule"""
def test_args(arg2, arg3):
pass
'''
)
result = pytester.runpytest("--fixtures-per-test", "-v", p)
assert result.ret == 0
result.stdout.fnmatch_lines(
[
"*fixtures used by test_args*",
"*(test_verbose_include_private_fixtures_and_loc.py:6)*",
"_arg1 -- conftest.py:3",
" _arg1 from conftest",
"arg2 -- conftest.py:6",
" arg2 from conftest",
"arg3 -- test_verbose_include_private_fixtures_and_loc.py:3",
" arg3 from testmodule",
]
)
def test_doctest_items(pytester: Pytester) -> None:
pytester.makepyfile(
'''
def foo():
"""
>>> 1 + 1
2
"""
'''
)
pytester.maketxtfile(
"""
>>> 1 + 1
2
"""
)
result = pytester.runpytest(
"--fixtures-per-test", "--doctest-modules", "--doctest-glob=*.txt", "-v"
)
assert result.ret == 0
result.stdout.fnmatch_lines(["*collected 2 items*"])
def test_multiline_docstring_in_module(pytester: Pytester) -> None:
p = pytester.makepyfile(
'''
import pytest
@pytest.fixture
def arg1():
"""Docstring content that spans across multiple lines,
through second line,
and through third line.
Docstring content that extends into a second paragraph.
Docstring content that extends into a third paragraph.
"""
def test_arg1(arg1):
pass
'''
)
result = pytester.runpytest("--fixtures-per-test", p)
assert result.ret == 0
result.stdout.fnmatch_lines(
[
"*fixtures used by test_arg1*",
"*(test_multiline_docstring_in_module.py:13)*",
"arg1 -- test_multiline_docstring_in_module.py:3",
" Docstring content that spans across multiple lines,",
" through second line,",
" and through third line.",
]
)
def test_verbose_include_multiline_docstring(pytester: Pytester) -> None:
p = pytester.makepyfile(
'''
import pytest
@pytest.fixture
def arg1():
"""Docstring content that spans across multiple lines,
through second line,
and through third line.
Docstring content that extends into a second paragraph.
Docstring content that extends into a third paragraph.
"""
def test_arg1(arg1):
pass
'''
)
result = pytester.runpytest("--fixtures-per-test", "-v", p)
assert result.ret == 0
result.stdout.fnmatch_lines(
[
"*fixtures used by test_arg1*",
"*(test_verbose_include_multiline_docstring.py:13)*",
"arg1 -- test_verbose_include_multiline_docstring.py:3",
" Docstring content that spans across multiple lines,",
" through second line,",
" and through third line.",
" ",
" Docstring content that extends into a second paragraph.",
" ",
" Docstring content that extends into a third paragraph.",
]
)
def test_should_not_show_pseudo_fixtures(pytester: Pytester) -> None:
"""A fixture is considered pseudo if it was directly created using the
``@pytest.mark.parametrize`` decorator as part of internal pytest
mechanisms (such as to manage batch execution). These fixtures should not
be included in the output because they don't satisfy user expectations for
how fixtures are created and used."""
p = pytester.makepyfile(
"""
import pytest
@pytest.mark.parametrize("x", [1])
def test_pseudo_fixture(x):
pass
"""
)
result = pytester.runpytest("--fixtures-per-test", p)
result.stdout.no_fnmatch_line("*fixtures used by*")
assert result.ret == 0
def test_should_show_parametrized_fixtures_used_by_test(pytester: Pytester) -> None:
"""A fixture with parameters should be included if it was created using
the @pytest.fixture decorator, including those that are indirectly
parametrized."""
p = pytester.makepyfile(
'''
import pytest
@pytest.fixture(params=['a', 'b'])
def directly(request):
"""parametrized fixture"""
return request.param
@pytest.fixture
def indirectly(request):
"""indirectly parametrized fixture"""
return request.param
def test_directly_parametrized_fixture(directly):
pass
@pytest.mark.parametrize("indirectly", ["a", "b"], indirect=True)
def test_indirectly_parametrized_fixture(indirectly):
pass
'''
)
result = pytester.runpytest("--fixtures-per-test", p)
assert result.ret == 0
result.stdout.fnmatch_lines(
[
"*fixtures used by test_directly_parametrized_fixture*",
"*(test_should_show_parametrized_fixtures_used_by_test.py:14)*",
"directly -- test_should_show_parametrized_fixtures_used_by_test.py:4",
" parametrized fixture",
"*fixtures used by test_directly_parametrized_fixture*",
"*(test_should_show_parametrized_fixtures_used_by_test.py:14)*",
"directly -- test_should_show_parametrized_fixtures_used_by_test.py:4",
" parametrized fixture",
"*fixtures used by test_indirectly_parametrized_fixture*",
"*(test_should_show_parametrized_fixtures_used_by_test.py:17)*",
"indirectly -- test_should_show_parametrized_fixtures_used_by_test.py:9",
" indirectly parametrized fixture",
"*fixtures used by test_indirectly_parametrized_fixture*",
"*(test_should_show_parametrized_fixtures_used_by_test.py:17)*",
"indirectly -- test_should_show_parametrized_fixtures_used_by_test.py:9",
" indirectly parametrized fixture",
]
)