Skip to content

Commit 908852f

Browse files
committed
Add a --force-reruns to override rerun count globally
Add a new `--force-reruns` command-line option that can be used to override the rerun count globally, irrespectively of individual test markers. Fixes #306. Signed-off-by: Michał Górny <mgorny@gentoo.org>
1 parent df47974 commit 908852f

File tree

5 files changed

+57
-0
lines changed

5 files changed

+57
-0
lines changed

CHANGES.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ Changelog
66

77
- Changed "localhost" to "127.0.0.1" to avoid bad hostname resolution
88

9+
- Added ``--force-reruns`` to override rerun count globally.
10+
Fixes `#306 <https://github.com/pytest-dev/pytest-rerunfailures/issues/306>`_.
11+
912
16.0.1 (2025-09-02)
1013
-------------------
1114

README.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,16 @@ You can use ``@pytest.mark.flaky(condition)`` similarly as ``@pytest.mark.skipif
190190
191191
Note that the test will re-run for any ``condition`` that is truthy.
192192

193+
Force rerun count
194+
-----------------
195+
196+
To force a specific re-run count globally, irrespectively of the number
197+
of re-runs specified in test markers, pass ``--force-reruns``:
198+
199+
.. code-block:: bash
200+
201+
$ pytest --force-reruns 5
202+
193203
Output
194204
------
195205

docs/cli.rst

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,22 @@
11
Command Line Interface Options
22
==============================
33

4+
.. option:: --force-reruns
5+
6+
**Description**:
7+
Force specified number of reruns, irrespectively of the number passed to pytest markers.
8+
9+
**Type**:
10+
Integer
11+
12+
**Default**:
13+
Not set (must be provided)
14+
15+
**Example**:
16+
.. code-block:: bash
17+
18+
pytest --force-reruns 5
19+
420
.. option:: --only-rerun
521

622
**Description**:

src/pytest_rerunfailures.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,14 @@ def pytest_addoption(parser):
5151
group = parser.getgroup(
5252
"rerunfailures", "re-run failing tests to eliminate flaky failures"
5353
)
54+
group._addoption(
55+
"--force-reruns",
56+
action="store",
57+
dest="force_reruns",
58+
type=int,
59+
help="Force rerunning all tests the specified number of times, "
60+
"irrespectively of individual test markers.",
61+
)
5462
group._addoption(
5563
"--only-rerun",
5664
action="append",
@@ -112,6 +120,10 @@ def _get_marker(item):
112120

113121

114122
def get_reruns_count(item):
123+
reruns = item.session.config.getvalue("force_reruns")
124+
if reruns is not None:
125+
return reruns
126+
115127
rerun_marker = _get_marker(item)
116128
# use the marker as a priority over the global setting.
117129
if rerun_marker is not None:

tests/test_pytest_rerunfailures.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1357,3 +1357,19 @@ def test_1(session_fixture, function_fixture):
13571357
result = testdir.runpytest()
13581358
assert_outcomes(result, passed=0, failed=1, rerun=1)
13591359
result.stdout.fnmatch_lines("session teardown")
1360+
1361+
1362+
@pytest.mark.parametrize("mark_params", ["", "reruns=1"])
1363+
def test_force_reruns(testdir, mark_params):
1364+
testdir.makepyfile(
1365+
f"""
1366+
import pytest
1367+
1368+
@pytest.mark.flaky({mark_params})
1369+
def test_fail():
1370+
assert False
1371+
"""
1372+
)
1373+
1374+
result = testdir.runpytest("--force-reruns", "3")
1375+
assert_outcomes(result, passed=0, failed=1, rerun=3)

0 commit comments

Comments
 (0)