Skip to content

Commit ae55e1a

Browse files
laky55555Michael Howitz
andauthored
Expose reruns and reruns_delay through pytest.ini file (#194)
* Expose `reruns` and `reruns_delay` through `pytest.ini` file Co-authored-by: Michael Howitz <[email protected]>
1 parent f4c0ef9 commit ae55e1a

File tree

3 files changed

+73
-13
lines changed

3 files changed

+73
-13
lines changed

CHANGES.rst

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ Changelog
44
11.1 (unreleased)
55
-----------------
66

7-
- Nothing changed yet.
7+
Features
8+
++++++++
9+
10+
- Expose `reruns` and `reruns_delay` through `pytest.ini` file.
811

912

1013
11.0 (2023-01-12)

pytest_rerunfailures.py

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040

4141

4242
PYTEST_GTE_54 = parse_version(pytest.__version__) >= parse_version("5.4")
43+
PYTEST_GTE_62 = parse_version(pytest.__version__) >= parse_version("6.2.0")
4344
PYTEST_GTE_63 = parse_version(pytest.__version__) >= parse_version("6.3.0.dev")
4445

4546

@@ -60,6 +61,10 @@ def works_with_current_xdist():
6061
return parse_version(d.version) >= parse_version("1.20")
6162

6263

64+
RERUNS_DESC = "number of times to re-run failed tests. defaults to 0."
65+
RERUNS_DELAY_DESC = "add time (seconds) delay between reruns."
66+
67+
6368
# command line options
6469
def pytest_addoption(parser):
6570
group = parser.getgroup(
@@ -80,15 +85,13 @@ def pytest_addoption(parser):
8085
action="store",
8186
dest="reruns",
8287
type=int,
83-
default=0,
84-
help="number of times to re-run failed tests. defaults to 0.",
88+
help=RERUNS_DESC,
8589
)
8690
group._addoption(
8791
"--reruns-delay",
8892
action="store",
8993
dest="reruns_delay",
9094
type=float,
91-
default=0,
9295
help="add time (seconds) delay between reruns.",
9396
)
9497
group._addoption(
@@ -101,6 +104,9 @@ def pytest_addoption(parser):
101104
"regex provided. Pass this flag multiple times to accumulate a list "
102105
"of regexes to match",
103106
)
107+
arg_type = "string" if PYTEST_GTE_62 else None
108+
parser.addini("reruns", RERUNS_DESC, type=arg_type)
109+
parser.addini("reruns_delay", RERUNS_DELAY_DESC, type=arg_type)
104110

105111

106112
def _get_resultlog(config):
@@ -155,21 +161,23 @@ def _get_marker(item):
155161

156162
def get_reruns_count(item):
157163
rerun_marker = _get_marker(item)
158-
reruns = None
159-
160164
# use the marker as a priority over the global setting.
161165
if rerun_marker is not None:
162166
if "reruns" in rerun_marker.kwargs:
163167
# check for keyword arguments
164-
reruns = rerun_marker.kwargs["reruns"]
168+
return rerun_marker.kwargs["reruns"]
165169
elif len(rerun_marker.args) > 0:
166170
# check for arguments
167-
reruns = rerun_marker.args[0]
171+
return rerun_marker.args[0]
168172
else:
169-
reruns = 1
170-
elif item.session.config.option.reruns:
171-
# default to the global setting
172-
reruns = item.session.config.option.reruns
173+
return 1
174+
175+
reruns = item.session.config.getvalue("reruns")
176+
if reruns is not None:
177+
return reruns
178+
179+
with suppress(TypeError, ValueError):
180+
reruns = int(item.session.config.getini("reruns"))
173181

174182
return reruns
175183

@@ -186,7 +194,12 @@ def get_reruns_delay(item):
186194
else:
187195
delay = 0
188196
else:
189-
delay = item.session.config.option.reruns_delay
197+
delay = item.session.config.getvalue("reruns_delay")
198+
if delay is None:
199+
try:
200+
delay = float(item.session.config.getini("reruns_delay"))
201+
except (TypeError, ValueError):
202+
delay = 0
190203

191204
if delay < 0:
192205
delay = 0

test_pytest_rerunfailures.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -657,3 +657,47 @@ def test_fail_two():
657657
)
658658
result = testdir.runpytest()
659659
assert_outcomes(result, passed=0, failed=1, rerun=2)
660+
661+
662+
def test_ini_file_parameters(testdir):
663+
testdir.makepyfile(
664+
"""
665+
import time
666+
def test_foo():
667+
assert False
668+
"""
669+
)
670+
testdir.makeini(
671+
"""
672+
[pytest]
673+
reruns = 2
674+
reruns_delay = 3
675+
"""
676+
)
677+
time.sleep = mock.MagicMock()
678+
result = testdir.runpytest()
679+
680+
time.sleep.assert_called_with(3)
681+
assert_outcomes(result, passed=0, failed=1, rerun=2)
682+
683+
684+
def test_ini_file_parameters_override(testdir):
685+
testdir.makepyfile(
686+
"""
687+
import time
688+
def test_foo():
689+
assert False
690+
"""
691+
)
692+
testdir.makeini(
693+
"""
694+
[pytest]
695+
reruns = 2
696+
reruns_delay = 3
697+
"""
698+
)
699+
time.sleep = mock.MagicMock()
700+
result = testdir.runpytest("--reruns", "4", "--reruns-delay", "5")
701+
702+
time.sleep.assert_called_with(5)
703+
assert_outcomes(result, passed=0, failed=1, rerun=4)

0 commit comments

Comments
 (0)