Skip to content

Commit cb8ede7

Browse files
mgornyicemacCopilot
authored
Add a --force-reruns to override rerun count globally (#307)
* 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> --------- Signed-off-by: Michał Górny <mgorny@gentoo.org> Co-authored-by: Michael Howitz <icemac@gmx.net> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 5e01132 commit cb8ede7

File tree

5 files changed

+60
-0
lines changed

5 files changed

+60
-0
lines changed

CHANGES.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ Changelog
99
- Drop support for Python 3.9.
1010

1111

12+
- Added ``--force-reruns`` to override rerun count globally.
13+
Fixes `#306 <https://github.com/pytest-dev/pytest-rerunfailures/issues/306>`_.
14+
1215
16.0.1 (2025-09-02)
1316
-------------------
1417

README.rst

Lines changed: 13 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, irrespective 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

@@ -241,6 +251,9 @@ which one takes priority?
241251
* Second priority is what's specified on the command line, like ``--reruns=2``
242252
* Last priority is the ``pyproject.toml`` (or ``pytest.ini``) file setting, like ``reruns = 3``
243253

254+
Additionally, all three can be overridden by passing ``--force-reruns`` argument
255+
on the command line.
256+
244257
.. END-PRIORITY
245258
246259

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, irrespective 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+
" irrespective 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)