-
Notifications
You must be signed in to change notification settings - Fork 27
/
test_repeat.py
271 lines (249 loc) · 10.7 KB
/
test_repeat.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
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://www.mozilla.org/en-US/MPL/2.0/.
import pytest
pytest_plugins = "pytester",
class TestRepeat:
def test_no_repeat(self, testdir):
testdir.makepyfile("""
def test_no_repeat(request):
fixtures = request.fixturenames
assert "__pytest_repeat_step_number" not in fixtures
""")
result = testdir.runpytest('-v', '--count', '1')
result.stdout.fnmatch_lines([
'*test_no_repeat.py::test_no_repeat PASSED*',
'*1 passed*',
])
assert result.ret == 0
def test_can_repeat(self, testdir):
testdir.makepyfile("""
def test_repeat():
pass
""")
result = testdir.runpytest('--count', '2')
result.stdout.fnmatch_lines(['*2 passed*'])
assert result.ret == 0
def test_mark_repeat_decorator_is_registered(self, testdir):
result = testdir.runpytest('--markers')
result.stdout.fnmatch_lines([
'@pytest.mark.repeat(n): run the given test function `n` times.'])
assert result.ret == 0
def test_mark_repeat_decorator(self, testdir):
testdir.makepyfile("""
import pytest
@pytest.mark.repeat(3)
def test_mark_repeat_decorator():
pass
""")
result = testdir.runpytest()
result.stdout.fnmatch_lines(['*3 passed*'])
assert result.ret == 0
def test_mark_repeat_decorator_repeat_once(self, testdir):
testdir.makepyfile("""
import pytest
@pytest.mark.repeat(1)
def test_mark_repeat_decorator_repeat_once():
pass
""")
result = testdir.runpytest('--count', '10')
result.stdout.fnmatch_lines(['*1 passed*'])
assert result.ret == 0
def test_parametrize(self, testdir):
testdir.makepyfile("""
import pytest
@pytest.mark.parametrize('x', ['a', 'b', 'c'])
def test_repeat(x):
pass
""")
result = testdir.runpytest('-v', '--count', '2')
result.stdout.fnmatch_lines([
'*test_parametrize.py::test_repeat[[]a-1-2[]] PASSED*',
'*test_parametrize.py::test_repeat[[]a-2-2[]] PASSED*',
'*test_parametrize.py::test_repeat[[]b-1-2[]] PASSED*',
'*test_parametrize.py::test_repeat[[]b-2-2[]] PASSED*',
'*test_parametrize.py::test_repeat[[]c-1-2[]] PASSED*',
'*test_parametrize.py::test_repeat[[]c-2-2[]] PASSED*',
'*6 passed*',
])
assert result.ret == 0
def test_parametrized_fixture(self, testdir):
testdir.makepyfile("""
import pytest
@pytest.fixture(params=['a', 'b', 'c'])
def parametrized_fixture(request):
return request.param
def test_repeat(parametrized_fixture):
pass
""")
result = testdir.runpytest('--count', '2')
result.stdout.fnmatch_lines(['*6 passed*'])
assert result.ret == 0
def test_step_number(self, testdir):
testdir.makepyfile("""
import pytest
expected_steps = iter(range(5))
def test_repeat(__pytest_repeat_step_number):
assert next(expected_steps) == __pytest_repeat_step_number
if __pytest_repeat_step_number == 4:
assert not list(expected_steps)
""")
result = testdir.runpytest('-v', '--count', '5')
result.stdout.fnmatch_lines([
'*test_step_number.py::test_repeat[[]1-5[]] PASSED*',
'*test_step_number.py::test_repeat[[]2-5[]] PASSED*',
'*test_step_number.py::test_repeat[[]3-5[]] PASSED*',
'*test_step_number.py::test_repeat[[]4-5[]] PASSED*',
'*test_step_number.py::test_repeat[[]5-5[]] PASSED*',
'*5 passed*',
])
assert result.ret == 0
def test_invalid_option(self, testdir):
testdir.makepyfile("""
def test_repeat():
pass
""")
result = testdir.runpytest('--count', 'a')
assert result.ret == 4
def test_unittest_test(self, testdir):
testdir.makepyfile("""
from unittest import TestCase
class ClassStyleTest(TestCase):
def test_this(self):
assert 1
""")
result = testdir.runpytest('-v', '--count', '2')
result.stdout.fnmatch_lines([
'*test_unittest_test.py::ClassStyleTest::test_this PASSED*',
'*1 passed*',
])
@pytest.mark.parametrize(['scope', 'lines'], [
('session', [
'*test_1.py::test_repeat1[[]1-2[]] PASSED*',
'*test_1.py::test_repeat2[[]1-2[]] PASSED*',
'*test_2.py::test_repeat3[[]1-2[]] PASSED*',
'*test_2.py::test_repeat4[[]1-2[]] PASSED*',
'*test_3.py::TestRepeat1::test_repeat5[[]1-2[]] PASSED*',
'*test_3.py::TestRepeat1::test_repeat6[[]1-2[]] PASSED*',
'*test_3.py::TestRepeat2::test_repeat7[[]1-2[]] PASSED*',
'*test_3.py::TestRepeat2::test_repeat8[[]1-2[]] PASSED*',
'*test_1.py::test_repeat1[[]2-2[]] PASSED*',
'*test_1.py::test_repeat2[[]2-2[]] PASSED*',
'*test_2.py::test_repeat3[[]2-2[]] PASSED*',
'*test_2.py::test_repeat4[[]2-2[]] PASSED*',
'*test_3.py::TestRepeat1::test_repeat5[[]2-2[]] PASSED*',
'*test_3.py::TestRepeat1::test_repeat6[[]2-2[]] PASSED*',
'*test_3.py::TestRepeat2::test_repeat7[[]2-2[]] PASSED*',
'*test_3.py::TestRepeat2::test_repeat8[[]2-2[]] PASSED*',
'*16 passed*',
]),
('module', [
'*test_1.py::test_repeat1[[]1-2[]] PASSED*',
'*test_1.py::test_repeat2[[]1-2[]] PASSED*',
'*test_1.py::test_repeat1[[]2-2[]] PASSED*',
'*test_1.py::test_repeat2[[]2-2[]] PASSED*',
'*test_2.py::test_repeat3[[]1-2[]] PASSED*',
'*test_2.py::test_repeat4[[]1-2[]] PASSED*',
'*test_2.py::test_repeat3[[]2-2[]] PASSED*',
'*test_2.py::test_repeat4[[]2-2[]] PASSED*',
'*test_3.py::TestRepeat1::test_repeat5[[]1-2[]] PASSED*',
'*test_3.py::TestRepeat1::test_repeat6[[]1-2[]] PASSED*',
'*test_3.py::TestRepeat2::test_repeat7[[]1-2[]] PASSED*',
'*test_3.py::TestRepeat2::test_repeat8[[]1-2[]] PASSED*',
'*test_3.py::TestRepeat1::test_repeat5[[]2-2[]] PASSED*',
'*test_3.py::TestRepeat1::test_repeat6[[]2-2[]] PASSED*',
'*test_3.py::TestRepeat2::test_repeat7[[]2-2[]] PASSED*',
'*test_3.py::TestRepeat2::test_repeat8[[]2-2[]] PASSED*',
'*16 passed*',
]),
('class', [
'*test_1.py::test_repeat1[[]1-2[]] PASSED*',
'*test_1.py::test_repeat2[[]1-2[]] PASSED*',
'*test_1.py::test_repeat1[[]2-2[]] PASSED*',
'*test_1.py::test_repeat2[[]2-2[]] PASSED*',
'*test_2.py::test_repeat3[[]1-2[]] PASSED*',
'*test_2.py::test_repeat4[[]1-2[]] PASSED*',
'*test_2.py::test_repeat3[[]2-2[]] PASSED*',
'*test_2.py::test_repeat4[[]2-2[]] PASSED*',
'*test_3.py::TestRepeat1::test_repeat5[[]1-2[]] PASSED*',
'*test_3.py::TestRepeat1::test_repeat6[[]1-2[]] PASSED*',
'*test_3.py::TestRepeat1::test_repeat5[[]2-2[]] PASSED*',
'*test_3.py::TestRepeat1::test_repeat6[[]2-2[]] PASSED*',
'*test_3.py::TestRepeat2::test_repeat7[[]1-2[]] PASSED*',
'*test_3.py::TestRepeat2::test_repeat8[[]1-2[]] PASSED*',
'*test_3.py::TestRepeat2::test_repeat7[[]2-2[]] PASSED*',
'*test_3.py::TestRepeat2::test_repeat8[[]2-2[]] PASSED*',
'*16 passed*',
]),
('function', [
'*test_1.py::test_repeat1[[]1-2[]] PASSED*',
'*test_1.py::test_repeat1[[]2-2[]] PASSED*',
'*test_1.py::test_repeat2[[]1-2[]] PASSED*',
'*test_1.py::test_repeat2[[]2-2[]] PASSED*',
'*test_2.py::test_repeat3[[]1-2[]] PASSED*',
'*test_2.py::test_repeat3[[]2-2[]] PASSED*',
'*test_2.py::test_repeat4[[]1-2[]] PASSED*',
'*test_2.py::test_repeat4[[]2-2[]] PASSED*',
'*test_3.py::TestRepeat1::test_repeat5[[]1-2[]] PASSED*',
'*test_3.py::TestRepeat1::test_repeat5[[]2-2[]] PASSED*',
'*test_3.py::TestRepeat1::test_repeat6[[]1-2[]] PASSED*',
'*test_3.py::TestRepeat1::test_repeat6[[]2-2[]] PASSED*',
'*test_3.py::TestRepeat2::test_repeat7[[]1-2[]] PASSED*',
'*test_3.py::TestRepeat2::test_repeat7[[]2-2[]] PASSED*',
'*test_3.py::TestRepeat2::test_repeat8[[]1-2[]] PASSED*',
'*test_3.py::TestRepeat2::test_repeat8[[]2-2[]] PASSED*',
'*16 passed*',
]),
])
def test_scope(self, testdir, scope, lines):
testdir.makepyfile(test_1="""
def test_repeat1():
pass
def test_repeat2():
pass
""")
testdir.makepyfile(test_2="""
def test_repeat3():
pass
def test_repeat4():
pass
""")
testdir.makepyfile(test_3="""
class TestRepeat1(object):
def test_repeat5(self):
pass
def test_repeat6(self):
pass
class TestRepeat2(object):
def test_repeat7(self):
pass
def test_repeat8(self):
pass
""")
result = testdir.runpytest('-v', '--count', '2', '--repeat-scope',
scope)
result.stdout.fnmatch_lines(lines)
assert result.ret == 0
def test_omitted_scope(self, testdir):
testdir.makepyfile("""
def test_repeat1():
pass
def test_repeat2():
pass
""")
result = testdir.runpytest('-v', '--count', '2')
result.stdout.fnmatch_lines([
'*test_omitted_scope.py::test_repeat1[[]1-2[]] PASSED*',
'*test_omitted_scope.py::test_repeat1[[]2-2[]] PASSED*',
'*test_omitted_scope.py::test_repeat2[[]1-2[]] PASSED*',
'*test_omitted_scope.py::test_repeat2[[]2-2[]] PASSED*',
'*4 passed*',
])
assert result.ret == 0
def test_invalid_scope(self, testdir):
testdir.makepyfile("""
def test_repeat():
pass
""")
result = testdir.runpytest('--count', '2', '--repeat-scope', 'a')
assert result.ret == 4