Skip to content

Commit e9996df

Browse files
filiplajszczakMichael Howitz
and
Michael Howitz
authored
Adds --fail-on-flaky option. (#276)
Fails the test run with custom exit code when test passed on rerun. Co-authored-by: Michael Howitz <[email protected]>
1 parent 17e7b23 commit e9996df

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

CHANGES.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,16 @@ Changelog
44
14.1 (unreleased)
55
-----------------
66

7+
Features
8+
++++++++
9+
710
- Fix compatibility with pytest 8.2.
811
(`#267 <https://github.com/pytest-dev/pytest-rerunfailures/issues/267>`_)
912

1013

14+
- Add ``--fail-on-flaky`` option to fail the test run with custom exit code
15+
when test passed on rerun.
16+
1117
14.0 (2024-03-13)
1218
-----------------
1319

src/pytest_rerunfailures.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,13 @@ def pytest_addoption(parser):
8585
"regex provided. Pass this flag multiple times to accumulate a list "
8686
"of regexes to match",
8787
)
88+
group._addoption(
89+
"--fail-on-flaky",
90+
action="store_true",
91+
dest="fail_on_flaky",
92+
help="Fail the test run with exit code 7 if a flaky test passes on a rerun.",
93+
)
94+
8895
arg_type = "string"
8996
parser.addini("reruns", RERUNS_DESC, type=arg_type)
9097
parser.addini("reruns_delay", RERUNS_DELAY_DESC, type=arg_type)
@@ -614,3 +621,13 @@ def show_rerun(terminalreporter, lines):
614621
for rep in rerun:
615622
pos = rep.nodeid
616623
lines.append(f"RERUN {pos}")
624+
625+
626+
@pytest.hookimpl(trylast=True)
627+
def pytest_sessionfinish(session, exitstatus):
628+
if exitstatus != 0:
629+
return
630+
631+
if session.config.option.fail_on_flaky:
632+
if session.config.getvalue("reruns") > 0:
633+
session.exitstatus = 7

tests/test_pytest_rerunfailures.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,28 @@ def test_pass():
177177
assert_outcomes(result, passed=1, rerun=1)
178178

179179

180+
def test_run_with_fail_on_flaky_fails_with_custom_error_code_after_pass_on_rerun(
181+
testdir,
182+
):
183+
testdir.makepyfile(
184+
f"""
185+
def test_pass():
186+
{temporary_failure()}"""
187+
)
188+
result = testdir.runpytest("--reruns", "1", "--fail-on-flaky")
189+
assert_outcomes(result, passed=1, rerun=1)
190+
assert result.ret == 7
191+
192+
193+
def test_run_fails_with_code_1_after_consistent_test_failure_even_with_fail_on_flaky(
194+
testdir,
195+
):
196+
testdir.makepyfile("def test_fail(): assert False")
197+
result = testdir.runpytest("--reruns", "1", "--fail-on-flaky")
198+
assert_outcomes(result, passed=0, failed=1, rerun=1)
199+
assert result.ret == 1
200+
201+
180202
@pytest.mark.skipif(not has_xdist, reason="requires xdist with crashitem")
181203
def test_rerun_passes_after_temporary_test_crash(testdir):
182204
# note: we need two tests because there is a bug where xdist

0 commit comments

Comments
 (0)