forked from pytest-dev/pytest-rerunfailures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_pytest_rerunfailures.py
221 lines (172 loc) · 6.68 KB
/
test_pytest_rerunfailures.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
import random
import pytest_rerunfailures
pytest_plugins = 'pytester'
def temporary_failure(count=1):
return """import py
path = py.path.local(__file__).dirpath().ensure('test.res')
count = path.read() or 1
if int(count) <= {0}:
path.write(int(count) + 1)
raise Exception('Failure: {{0}}'.format(count))""".format(count)
def assert_outcomes(result, passed=1, skipped=0, failed=0, error=0, xfailed=0,
xpassed=0, rerun=0):
outcomes = result.parseoutcomes()
assert outcomes.get('passed', 0) == passed
assert outcomes.get('skipped', 0) == skipped
assert outcomes.get('failed', 0) == failed
assert outcomes.get('xfailed', 0) == xfailed
assert outcomes.get('xpassed', 0) == xpassed
assert outcomes.get('rerun', 0) == rerun
def test_error_when_run_with_pdb(testdir):
testdir.makepyfile('def test_pass(): pass')
result = testdir.runpytest('--reruns', '1', '--pdb')
result.stderr.fnmatch_lines_random(
'ERROR: --reruns incompatible with --pdb')
def test_no_rerun_on_pass(testdir):
testdir.makepyfile('def test_pass(): pass')
result = testdir.runpytest('--reruns', '1')
assert_outcomes(result)
def test_no_rerun_on_skipif_mark(testdir):
reason = str(random.random())
testdir.makepyfile("""
import pytest
@pytest.mark.skipif(reason='{0}')
def test_skip():
pass
""".format(reason))
result = testdir.runpytest('--reruns', '1')
assert_outcomes(result, passed=0, skipped=1)
def test_no_rerun_on_skip_call(testdir):
reason = str(random.random())
testdir.makepyfile("""
import pytest
def test_skip():
pytest.skip('{0}')
""".format(reason))
result = testdir.runpytest('--reruns', '1')
assert_outcomes(result, passed=0, skipped=1)
def test_no_rerun_on_xfail_mark(testdir):
reason = str(random.random())
testdir.makepyfile("""
import pytest
@pytest.mark.xfail()
def test_xfail():
assert False
""".format(reason))
result = testdir.runpytest('--reruns', '1')
assert_outcomes(result, passed=0, xfailed=1)
def test_no_rerun_on_xfail_call(testdir):
reason = str(random.random())
testdir.makepyfile("""
import pytest
def test_xfail():
pytest.xfail('{0}')
""".format(reason))
result = testdir.runpytest('--reruns', '1')
assert_outcomes(result, passed=0, xfailed=1)
def test_no_rerun_on_xpass(testdir):
reason = str(random.random())
testdir.makepyfile("""
import pytest
@pytest.mark.xfail()
def test_xpass():
pass
""".format(reason))
result = testdir.runpytest('--reruns', '1')
assert_outcomes(result, passed=0, xpassed=1)
def test_rerun_fails_after_consistent_setup_failure(testdir):
testdir.makepyfile('def test_pass(): pass')
testdir.makeconftest("""
def pytest_runtest_setup(item):
raise Exception('Setup failure')""")
result = testdir.runpytest('--reruns', '1')
assert_outcomes(result, passed=0, error=1, rerun=1)
def test_rerun_passes_after_temporary_setup_failure(testdir):
testdir.makepyfile('def test_pass(): pass')
testdir.makeconftest("""
def pytest_runtest_setup(item):
{0}""".format(temporary_failure()))
result = testdir.runpytest('--reruns', '1', '-r', 'R')
assert_outcomes(result, passed=1, rerun=1)
def test_rerun_fails_after_consistent_test_failure(testdir):
testdir.makepyfile('def test_fail(): assert False')
result = testdir.runpytest('--reruns', '1')
assert_outcomes(result, passed=0, failed=1, rerun=1)
def test_rerun_passes_after_temporary_test_failure(testdir):
testdir.makepyfile("""
def test_pass():
{0}""".format(temporary_failure()))
result = testdir.runpytest('--reruns', '1', '-r', 'R')
assert_outcomes(result, passed=1, rerun=1)
def test_rerun_passes_after_temporary_test_failure_with_flaky_mark(testdir):
testdir.makepyfile("""
import pytest
@pytest.mark.flaky(reruns=2)
def test_pass():
{0}""".format(temporary_failure(2)))
result = testdir.runpytest('-r', 'R')
assert_outcomes(result, passed=1, rerun=2)
def test_reruns_if_flaky_mark_is_called_without_options(testdir):
testdir.makepyfile("""
import pytest
@pytest.mark.flaky()
def test_pass():
{0}""".format(temporary_failure(1)))
result = testdir.runpytest('-r', 'R')
assert_outcomes(result, passed=1, rerun=1)
def test_reruns_if_flaky_mark_is_called_with_positional_argument(testdir):
testdir.makepyfile("""
import pytest
@pytest.mark.flaky(2)
def test_pass():
{0}""".format(temporary_failure(2)))
result = testdir.runpytest('-r', 'R')
assert_outcomes(result, passed=1, rerun=2)
def test_no_extra_test_summary_for_reruns_by_default(testdir):
testdir.makepyfile("""
def test_pass():
{0}""".format(temporary_failure()))
result = testdir.runpytest('--reruns', '1')
assert 'RERUN' not in result.stdout.str()
assert '1 rerun' in result.stdout.str()
def test_extra_test_summary_for_reruns(testdir):
testdir.makepyfile("""
def test_pass():
{0}""".format(temporary_failure()))
result = testdir.runpytest('--reruns', '1', '-r', 'R')
result.stdout.fnmatch_lines_random(['RERUN test_*:*'])
assert '1 rerun' in result.stdout.str()
def test_verbose(testdir):
testdir.makepyfile("""
def test_pass():
{0}""".format(temporary_failure()))
result = testdir.runpytest('--reruns', '1', '-v')
result.stdout.fnmatch_lines_random(['test_*:* RERUN'])
assert '1 rerun' in result.stdout.str()
def test_no_rerun_on_class_setup_error_without_reruns(testdir):
testdir.makepyfile("""
class TestFoo(object):
@classmethod
def setup_class(cls):
assert False
def test_pass():
pass""")
result = testdir.runpytest('--reruns', '0')
assert_outcomes(result, passed=0, error=1, rerun=0)
def test_rerun_on_class_setup_error_with_reruns(testdir):
testdir.makepyfile("""
class TestFoo(object):
@classmethod
def setup_class(cls):
assert False
def test_pass():
pass""")
result = testdir.runpytest('--reruns', '1')
assert_outcomes(result, passed=0, error=1, rerun=1)
def test_rerun_with_resultslog(testdir):
testdir.makepyfile("""
def test_fail():
assert False""")
result = testdir.runpytest('--reruns', '2',
'--result-log', './pytest.log')
assert_outcomes(result, passed=0, failed=1, rerun=2)