-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_plugin.py
More file actions
371 lines (279 loc) · 9.39 KB
/
Copy pathtest_plugin.py
File metadata and controls
371 lines (279 loc) · 9.39 KB
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
from __future__ import annotations
import json
import pytest
def test_split_simple(pytester: pytest.Pytester) -> None:
pytester.makepyfile("""
def test_one():
assert True
def test_two():
assert False
""")
result = pytester.runpytest("--cdist-group=1/2")
result.assert_outcomes(passed=1, deselected=1)
result = pytester.runpytest("--cdist-group=2/2")
result.assert_outcomes(failed=1, deselected=1)
def test_split_with_preselect(pytester: pytest.Pytester) -> None:
pytester.makepyfile("""
def test_one():
assert True
def test_two():
assert False
def test_three():
assert True
""")
result = pytester.runpytest("--cdist-group=1/2", "-k", "two")
result.assert_outcomes(failed=1, deselected=2)
result = pytester.runpytest("--cdist-group=2/2", "-k", "two")
result.assert_outcomes(passed=0, deselected=3)
def test_justify_file(pytester: pytest.Pytester) -> None:
pytester.makepyfile("""
def test_one():
assert True
def test_two():
assert True
def test_three():
assert True
""")
result = pytester.runpytest("--cdist-group=1/2", "--cdist-justify-items=file")
result.assert_outcomes(passed=3)
result = pytester.runpytest("--cdist-group=2/2", "--cdist-justify-items=file")
result.assert_outcomes(passed=0, deselected=3)
def test_justify_scope(pytester: pytest.Pytester) -> None:
pytester.makepyfile("""
class TestSomething:
def test_one(self):
assert True
def test_two(self):
assert True
class TestSomethingElse:
def test_three(self):
assert True
""")
result = pytester.runpytest("--cdist-group=1/2", "--cdist-justify-items=scope")
result.assert_outcomes(passed=2, deselected=1)
result = pytester.runpytest("--cdist-group=2/2", "--cdist-justify-items=scope")
result.assert_outcomes(passed=1, deselected=2)
@pytest.mark.parametrize(
"cli_opt, ini_opt",
[
("--cdist-justify-items=file", None),
("--cdist-justify-items=file", "cdist-justify-items=none"),
("", "cdist-justify-items=file"),
],
)
def test_justify_cli_ini_cfg(
pytester: pytest.Pytester,
cli_opt: str,
ini_opt: str | None,
ini_tpl: str,
) -> None:
pytester.makepyfile("""
def test_one():
assert True
def test_two():
assert True
def test_three():
assert True
""")
if ini_opt is not None:
pytester.makeini(ini_tpl + f"\n{ini_opt}")
result = pytester.runpytest("--cdist-group=1/2", cli_opt)
result.assert_outcomes(passed=3)
result = pytester.runpytest("--cdist-group=2/2", cli_opt)
result.assert_outcomes(passed=0, deselected=3)
def test_justify_xdist_groups(pytester: pytest.Pytester) -> None:
pytester.makepyfile("""
import pytest
def test_no_group():
pass
@pytest.mark.xdist_group("one")
def test_one():
assert True
@pytest.mark.xdist_group("one")
def test_two():
assert True
@pytest.mark.xdist_group("two")
def test_three():
assert True
""")
result = pytester.runpytest("--cdist-group=1/2", "-n", "2")
result.assert_outcomes(passed=2)
result = pytester.runpytest("--cdist-group=2/2", "-n", "2")
# don't assert "deselect" here since it doesn't work properly with xdist
result.assert_outcomes(passed=2)
def test_report(pytester: pytest.Pytester) -> None:
pytester.makepyfile("""
def test_one():
assert True
def test_two():
assert True
""")
result = pytester.runpytest("--cdist-group=1/2", "--cdist-report")
result.assert_outcomes(passed=1, deselected=1)
result = pytester.runpytest("--cdist-group=2/2", "--cdist-report")
result.assert_outcomes(passed=1, deselected=1)
report_file_1 = pytester.path / "pytest_cdist_report_1.json"
report_file_2 = pytester.path / "pytest_cdist_report_2.json"
assert report_file_1.exists()
assert report_file_2.exists()
assert json.loads(report_file_1.read_text()) == {
"group": 1,
"total_groups": 2,
"collected": ["test_report.py::test_one", "test_report.py::test_two"],
"selected": ["test_report.py::test_one"],
}
assert json.loads(report_file_2.read_text()) == {
"group": 2,
"total_groups": 2,
"collected": ["test_report.py::test_one", "test_report.py::test_two"],
"selected": ["test_report.py::test_two"],
}
@pytest.mark.parametrize(
"cli_opt, ini_opt",
[
("--cdist-group-steal=2:50", None),
("", "cdist-group-steal=2:50"),
("--cdist-group-steal=2:50", "cdist-group-steal=2:50"),
],
)
def test_steal(
pytester: pytest.Pytester, cli_opt: str, ini_opt: str | None, ini_tpl: str
) -> None:
pytester.makepyfile("""
def test_one():
assert True
def test_two():
assert True
def test_three():
assert True
def test_four():
assert True
""")
if ini_opt is not None:
pytester.makeini(ini_tpl + f"\n{ini_opt}")
result = pytester.runpytest("--cdist-group=1/2", cli_opt)
result.assert_outcomes(passed=1, deselected=3)
result = pytester.runpytest("--cdist-group=2/2", cli_opt)
result.assert_outcomes(passed=3, deselected=1)
def test_steal_with_target(pytester: pytest.Pytester) -> None:
pytester.makepyfile("""
def test_one():
assert False
def test_two():
assert True
def test_three():
assert True
def test_four():
assert True
""")
# natural distribution would be
# 1: test_one, test_two
# 2: test_three
# 3: test_four
# telling group 3 to steal 50% of group 1 should result in
# 1: test_two
# 2: test_three
# 3: test_four, test_one
cli_opt = "--cdist-group-steal=g3:50:g1"
result = pytester.runpytest_inprocess("--cdist-group=1/3", cli_opt)
result.assert_outcomes(passed=1, deselected=3)
result = pytester.runpytest_inprocess("--cdist-group=2/3", cli_opt)
result.assert_outcomes(passed=1, deselected=3)
result = pytester.runpytest_inprocess("--cdist-group=3/3", cli_opt)
result.assert_outcomes(passed=1, failed=1, deselected=2)
def test_steal_multiple_target(pytester: pytest.Pytester) -> None:
pytester.makepyfile("""
def test_one():
assert True
def test_two():
assert True
def test_three():
assert True
def test_four():
assert True
def test_five():
assert True
def test_six():
assert True
""")
# natural distribution would be
# 1: 2
# 2: 2
# 3: 2
# first, we're telling group 2 to steal 50% of all other groups:
# 1: 1
# 2: 4
# 3: 1
# then, we're telling group 3 to steal 50% of group 2
# 1: 1
# 2: 2
# 3: 3
cli_opt = "--cdist-group-steal=g2:50,g3:50:g2"
result = pytester.runpytest("--cdist-group=1/3", cli_opt)
result.assert_outcomes(passed=1, deselected=5)
result = pytester.runpytest("--cdist-group=2/3", cli_opt)
result.assert_outcomes(passed=2, deselected=4)
result = pytester.runpytest("--cdist-group=3/3", cli_opt)
result.assert_outcomes(passed=3, deselected=3)
def test_steal_with_justify(pytester: pytest.Pytester) -> None:
pytester.makepyfile("""
class TestFoo:
def test_one(self):
assert True
def test_two(self):
assert True
""")
# Stealing should never break the scope
result = pytester.runpytest(
"--cdist-group=1/2",
"--cdist-justify-items=scope",
"--cdist-group-steal=2:60",
)
result.assert_outcomes(passed=2, deselected=0)
result = pytester.runpytest(
"--cdist-group=2/2",
"--cdist-justify-items=scope",
"--cdist-group-steal=2:60",
)
result.assert_outcomes(passed=0, deselected=2)
def test_raises_for_invalid_randomly_cfg(pytester: pytest.Pytester) -> None:
pytester.makeini("")
result = pytester.runpytest("--cdist-group=1/2")
assert (
"pytest-cdist is incompatible with the current pytest-randomly configuration"
in result.stderr.str()
)
@pytest.mark.parametrize("i", range(10))
def test_randomly_with_seed(pytester: pytest.Pytester, i: int) -> None:
pytester.makeini('[pytest]\naddopts="-p randomly"')
pytester.makepyfile("""
def test_one():
assert True
def test_two():
assert True
def test_three():
assert False
""")
result = pytester.runpytest_subprocess("--cdist-group=1/2", "--randomly-seed=123")
result.assert_outcomes(passed=1, failed=1, deselected=1)
result = pytester.runpytest_subprocess("--cdist-group=2/2", "--randomly-seed=123")
result.assert_outcomes(passed=1, failed=0, deselected=2)
@pytest.mark.parametrize("i", range(10))
def test_randomly_with_dont_reorganize(pytester: pytest.Pytester, i: int) -> None:
pytester.makeini('[pytest]\naddopts="-p randomly"')
pytester.makepyfile("""
def test_one():
assert True
def test_two():
assert True
def test_three():
assert False
""")
result = pytester.runpytest_subprocess(
"--cdist-group=1/2", "--randomly-dont-reorganize"
)
result.assert_outcomes(passed=2, failed=0, deselected=1)
result = pytester.runpytest_subprocess(
"--cdist-group=2/2", "--randomly-dont-reorganize"
)
result.assert_outcomes(passed=0, failed=1, deselected=2)