pytest-retry

1.7.0 · active · verified Fri Apr 10

pytest-retry is a plugin for Pytest that adds the ability to retry flaky tests, improving the consistency of test suite results. It currently supports Python 3.9+ and pytest 7.0.0+. The library maintains an active release cadence with regular updates.

Warnings

Install

Imports

Quickstart

Mark a test function with `@pytest.mark.flaky(retries=N, delay=S)` to make it retry N additional times upon failure, with S seconds delay between attempts. Global retry settings can be configured via command-line arguments (`--retries N --retry-delay S`) or in `pytest.ini`/`pyproject.toml`.

import pytest
import random

@pytest.mark.flaky(retries=2, delay=0.5)
def test_sometimes_fails():
    # This test has a 50% chance to fail, simulating flakiness.
    # It will retry up to 2 times (3 attempts total) with a 0.5-second delay.
    if random.random() < 0.5:
        pytest.fail("Simulated intermittent failure")
    assert True

# To run this test from your terminal:
# pytest your_test_file.py
# You can also set global retries:
# pytest --retries 2 --retry-delay 0.5 your_test_file.py

view raw JSON →