Skip to content

Add --rerun-warning flag to emit a warning on each scheduled rerun - #375

Open
LouisDeconinck wants to merge 3 commits into
pytest-dev:masterfrom
LouisDeconinck:issue-318-rerun-warning
Open

LouisDeconinck wants to merge 3 commits into
pytest-dev:masterfrom
LouisDeconinck:issue-318-rerun-warning

Conversation

@LouisDeconinck

@LouisDeconinck LouisDeconinck commented Sep 17, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Adds an opt-in --rerun-warning flag that emits a PytestWarning each time a test is scheduled for rerun
  • Intended for CI visibility, e.g. turning warnings into annotations via pytest-github-actions-annotate-failures
  • Opt-in so filterwarnings = error users are unaffected; the caveat is documented in --help and the README

Closes #318

Test plan

  • Test asserting the warning appears in the warnings summary and the rerun still happens
  • Test asserting no warning is emitted without the flag

LouisDeconinck and others added 2 commits September 17, 2026 21:29
Emits a PytestWarning per rerun so CI tooling such as
pytest-github-actions-annotate-failures can surface flaky tests as
annotations. Opt-in so filterwarnings=error users are unaffected.

Closes pytest-dev#318.

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>

report.outcome = "rerun"
if item.session.config.option.rerun_warning:
item.warn(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

— Comment created by Claude

Bug (high): --rerun-warning combined with filterwarnings = error crashes the whole session with INTERNALERROR.

item.warn() is called from pytest_runtest_protocol, not from inside a setup/call/teardown phase. Under -W error or filterwarnings = error, the warning is raised as an exception, and nothing catches it at this level. I reproduced it with --reruns 1 --rerun-warning -W error on a file where one test fails once: the run ends with INTERNALERROR> pytest.PytestWarning: test_x.py::test_a failed on attempt 1 and will be rerun and no tests ran. The remaining tests never run. The report is also left with outcome = "rerun" but is never logged, and any --max-suite-reruns slot has already been used up.

The help text and README say only that the warnings "turn into errors". Users will read that as a failing test, not an aborted session. Fix options: emit the warning in a way that cannot abort the protocol (for example, catch the exception and fail the test), or document the real behaviour clearly. A test covering -W error would also help.

continue

report.outcome = "rerun"
if item.session.config.option.rerun_warning:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

— Comment created by Claude

Gap (low): worker-crash reruns under xdist emit no warning.

The warning is only emitted on this in-process rerun path. XDistHooks.pytest_handlecrashitem also schedules reruns (sched.mark_test_pending(crashitem) / report.outcome = "rerun") and does not check rerun_warning. So with -n and --rerun-warning, a test that crashes its worker and is rerun gets no annotation. The help text promises a warning "each time a test is scheduled for rerun". Either emit a warning on that path too, or narrow the wording.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add ability to raise runtime warnings on reruns

2 participants