pytest-rerunfailures

16.1 · active · verified Sun Mar 29

pytest-rerunfailures is a plugin for pytest that re-runs failed or intermittently failing tests to eliminate flaky failures, improving the reliability of CI/CD pipelines. It is actively maintained with frequent releases, typically every few months, adding new features and ensuring compatibility with the latest Python and pytest versions.

Warnings

Install

Imports

Quickstart

To enable `pytest-rerunfailures`, simply install it. You can configure reruns via command-line options (`pytest --reruns N`), a `pytest.mark.flaky` decorator on individual tests, or globally in `pytest.ini`/`pyproject.toml`. Adding `--reruns-delay N` introduces a pause between retries.

import pytest
import random

def test_flaky_example():
    # This test will randomly fail, demonstrating rerunfailures
    if random.choice([True, False, False]): # Higher chance to fail initially
        pytest.fail("Simulated flaky failure")

# To run with reruns: pytest --reruns 3 your_test_file.py
# To mark a specific test as flaky:
@pytest.mark.flaky(reruns=2, reruns_delay=1)
def test_another_flaky_example():
    if random.choice([True, False]):
        pytest.fail("Another flaky failure")

# Example of pytest.ini configuration (add to a file named pytest.ini in your project root):
# [pytest]
# reruns = 2
# reruns_delay = 1
# You can then run: pytest

view raw JSON →