Skip to content

Commit d0ff1d4

Browse files
Adjust --fail-on-flaky behavior to improve exit status handling (#288)
1 parent c4992c3 commit d0ff1d4

File tree

3 files changed

+70
-3
lines changed

3 files changed

+70
-3
lines changed

CHANGES.rst

+6-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@ Changelog
44
15.1 (unreleased)
55
-----------------
66

7-
- Nothing changed yet.
7+
Bug fixes
8+
+++++++++
9+
10+
- Fix ``--fail-on-flaky`` option to fail the test run with custom exit code
11+
only when reruns are detected.
12+
(`#287 <https://github.com/pytest-dev/pytest-rerunfailures/issues/287>`_)
813

914

1015
15.0 (2024-11-20)

src/pytest_rerunfailures.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -629,5 +629,10 @@ def pytest_sessionfinish(session, exitstatus):
629629
return
630630

631631
if session.config.option.fail_on_flaky:
632-
if session.config.getvalue("reruns") > 0:
633-
session.exitstatus = 7
632+
for item in session.items:
633+
if not hasattr(item, "execution_count"):
634+
# no rerun requested
635+
continue
636+
if item.execution_count > 1:
637+
session.exitstatus = 7
638+
break

tests/test_pytest_rerunfailures.py

+57
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,63 @@ def test_run_fails_with_code_1_after_consistent_test_failure_even_with_fail_on_f
199199
assert result.ret == 1
200200

201201

202+
def test_run_mark_and_fail_on_flaky_fails_with_custom_error_code_after_pass_on_rerun(
203+
testdir,
204+
):
205+
testdir.makepyfile(f"""
206+
import pytest
207+
208+
@pytest.mark.flaky(reruns=1)
209+
def test_fail():
210+
{temporary_failure()}
211+
""")
212+
result = testdir.runpytest("--fail-on-flaky")
213+
assert_outcomes(result, passed=1, rerun=1)
214+
assert result.ret == 7
215+
216+
217+
def test_run_fails_with_code_1_after_test_failure_with_fail_on_flaky_and_mark(
218+
testdir,
219+
):
220+
testdir.makepyfile("""
221+
import pytest
222+
223+
@pytest.mark.flaky(reruns=2)
224+
def test_fail():
225+
assert False
226+
""")
227+
result = testdir.runpytest("--fail-on-flaky")
228+
assert_outcomes(result, passed=0, failed=1, rerun=2)
229+
assert result.ret == 1
230+
231+
232+
def test_run_with_mark_and_fail_on_flaky_succeeds_if_all_tests_pass_without_reruns(
233+
testdir,
234+
):
235+
testdir.makepyfile("""
236+
import pytest
237+
238+
@pytest.mark.flaky(reruns=2)
239+
def test_marked_pass():
240+
assert True
241+
242+
def test_unmarked_pass():
243+
assert True
244+
""")
245+
result = testdir.runpytest("--fail-on-flaky")
246+
assert_outcomes(result, passed=2, rerun=0)
247+
assert result.ret == pytest.ExitCode.OK
248+
249+
250+
def test_run_with_fail_on_flaky_succeeds_if_all_tests_pass_without_reruns(
251+
testdir,
252+
):
253+
testdir.makepyfile("def test_pass(): assert True")
254+
result = testdir.runpytest("--reruns", "1", "--fail-on-flaky")
255+
assert_outcomes(result, passed=1, rerun=0)
256+
assert result.ret == pytest.ExitCode.OK
257+
258+
202259
@pytest.mark.skipif(not has_xdist, reason="requires xdist with crashitem")
203260
def test_rerun_passes_after_temporary_test_crash(testdir):
204261
# note: we need two tests because there is a bug where xdist

0 commit comments

Comments
 (0)