pytest-rerunfailures

repository·master·Indexed 19 days ago

https://github.com/pytest-dev/pytest-rerunfailures

A pytest plugin designed to eliminate intermittent test failures by automatically re-running tests that fail. It provides CLI options like --reruns, --reruns-delay, and --only-rerun, as well as the @pytest.mark.flaky decorator for fine-grained control over retry counts, delays, and conditions. The plugin supports exponential backoff, filtering by exception regex, and recovery from hard crashes (segfaults) when used with pytest-xdist.

Tokens
4.8K
Snippets
25
Records
29
Agent score
65%

What's inside pytest-rerunfailures

  1. Understand re-run priority

    master

    When re-run counts are specified in multiple places, the following priority order applies (from highest to lowest):

    1. Test Markers: @pytest.mark.flaky(reruns=X)
    2. Command Line: --reruns X
    3. Configuration Files: reruns = X in pyproject.toml or pytest.ini

    Note: The --force-reruns CLI flag overrides all of the above. If --reruns-mode=append is used, the marker count and the global setting become additive instead of following this priority.

  2. Mark specific tests as flaky with @pytest.mark.flaky

    master

    Use the @pytest.mark.flaky decorator to mark individual tests for automatic re-runs upon failure. This is useful for handling non-deterministic tests (e.g., race conditions or network latency). Settings applied via this decorator override global command-line options.

    @pytest.mark.flaky(reruns=3)
    def test_example():
        import random
        assert random.choice([True, False])
  3. Recover from hard crashes (segfaults)

    master

    If tests trigger a hard crash (e.g., a segfault), pytest-rerunfailures can recover and rerun them if you meet these conditions:

    1. pytest-xdist is installed.
    2. Tests are run using the -n flag (pytest-xdist).
    3. The workers and controller are on the same LAN (typically the same computer).
  4. Run tests and linting using tox

    master

    The project uses tox to automate testing and environment setup. tox will automatically create virtual environments (using virtualenv) to run the specified test suites. Use tox -e linting to run style checks or specify a Python version (e.g., py312) to run tests.

    $ pip install tox
    $ tox -e linting,py312
  5. How the flaky marker and reruns mode work together

    master

    The plugin determines how many times a test should be rerun by combining the @flaky marker settings with global settings (CLI or .ini). This behavior is controlled by the --reruns-mode option:

    1. strict (default): The marker takes priority. If a test is marked with @flaky(reruns=5), it will rerun 5 times, even if the global --reruns is set to 10.
    2. append: The counts are additive. If the global --reruns is 2 and the test is marked with @flaky(reruns=3), the test will rerun a total of 5 times.