Skip to main content

A Pytest plugin for intelligent retrying of flaky tests.

Project description

pytest-smart-rerun

PyPI License

pytest-smart-rerun is a lightweight Pytest plugin that retries flaky tests with configurable attempts and delays. It integrates directly with Pytest's hook system, requires no external dependencies, and works out of the box from the command line or via per-test decorators.

Installation

pip install pytest-smart-rerun

For local development:

git clone https://github.com/AkhileshKr/pytest-smart-rerun.git
cd pytest-smart-rerun
pip install -e .

Quick start

Retry all failing tests up to three times with a one second delay:

pytest --smart-rerun --smart-rerun-max=3 --smart-rerun-delay=1

Use the smart_rerun marker to target specific flaky tests:

import pytest


@pytest.mark.smart_rerun(max=4, delay=0.5)
def test_eventually_consistent_service(client):
    response = client.read_state()
    assert response.status_code == 200

Combine both for a default retry policy with test-level overrides.

CLI options

  • --smart-rerun: enable smart reruns for the test session.
  • --smart-rerun-max=N: set the total number of attempts per test (default: 2).
  • --smart-rerun-delay=SECONDS: add a delay between retries (default: 0).

Invalid values raise a Pytest UsageError, keeping failure feedback obvious.

Features

  • Intelligent reruns driven purely by Pytest hooks—no external dependencies.
  • Automatic attempt logging with rerun-aware reporting in the terminal.
  • Per-test overrides through @pytest.mark.smart_rerun(max=..., delay=...).
  • Works alongside existing Pytest plugins and respects fixture/state isolation.

Roadmap

  1. Exponential and linear backoff strategies (--smart-rerun-backoff).
  2. Retry filtering by exception classes (--smart-rerun-errors).
  3. Structured JSON retry reports (--smart-rerun-report).
  4. Global configuration via pytest.ini defaults.
  5. CI-friendly colour output and aggregate analytics summaries.

Phase 3: AI Extension Proposal

The long-term vision is to evolve pytest-smart-rerun into an AI-assisted resilience toolkit by adopting the Model Context Protocol (MCP). This optional extension would unlock:

  • AI-powered failure analysis: Forward stack traces, captured logs, and environment metadata to an MCP endpoint so an LLM can classify failures (e.g. deterministic bug vs flaky network) and suggest next actions.
  • Natural-language retry policies: Allow developers to express strategies like “rerun timeout or network errors up to three times” and have the MCP agent translate that instruction into structured plugin configuration.
  • Smart pattern detection: Correlate persistent failures across CI dimensions and have the MCP model generate summaries plus recommended fixes, written straight into a JSON report for future sessions.
  • Contextual debugging hints: When repeated retries still fail, attach LLM-suggested remediation steps or related repo references to the terminal summary and persisted smart_rerun_ai_report.json.

In practice this phase would add an --smart-rerun-ai flag (and matching pytest.ini toggle), an MCP client wrapper, asynchronous request handling, optional local caching, and redaction safeguards for sensitive data. The design keeps outbound calls disabled by default, supports on-prem MCP deployments, and positions the project as an AI-native testing showcase for future releases.

Development

python -m venv .venv
source .venv/bin/activate
pip install -U pip setuptools wheel
pip install -e .[test]
pytest -v
pytest --smart-rerun --smart-rerun-max=3 --smart-rerun-delay=1 -v

Release workflow

  1. python -m build
  2. python -m twine check dist/*
  3. python -m twine upload dist/*

Refer to Publishing to PyPI for account setup and API tokens.

License

MIT © 2025 Akilesh KR. See LICENSE for full text.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytest_smart_rerun-0.1.0.tar.gz (7.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pytest_smart_rerun-0.1.0-py3-none-any.whl (7.0 kB view details)

Uploaded Python 3

File details

Details for the file pytest_smart_rerun-0.1.0.tar.gz.

File metadata

  • Download URL: pytest_smart_rerun-0.1.0.tar.gz
  • Upload date:
  • Size: 7.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for pytest_smart_rerun-0.1.0.tar.gz
Algorithm Hash digest
SHA256 6b8ebfd6d2cee3cd0f894bf677975ed5726fbb4638649daaaeafc169f0b242a1
MD5 e02922278f9ab8ad99a22bd7cbb23c57
BLAKE2b-256 ff1730eb67cff3853d0ef6d19544f77c8bb386b7f017b8a5bd5757e6ad95aeba

See more details on using hashes here.

File details

Details for the file pytest_smart_rerun-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pytest_smart_rerun-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b91ce2e63694ee43fb11b4f7a94bb38255fbd9139213c8115a4219ceb946ab71
MD5 8bb7019ba8574df2614af5fd4853ba7a
BLAKE2b-256 7f948de9a7631794e3d509608a81fc8f41590fd76b66154408a7c6b4381a06e4

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page