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/Aki-07/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.1.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.1-py3-none-any.whl (7.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pytest_smart_rerun-0.1.1.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.1.tar.gz
Algorithm Hash digest
SHA256 83c5431f7567631aeefe775b873dea4f02c5e688df39b0a620ffdf9d740b0612
MD5 e8b8d9a5009ecb44006d5e35a4da5d5b
BLAKE2b-256 f1292a47362671aa6311f6cc9607f67ad3ee810cba78c176aaa91c03a50f9ea0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pytest_smart_rerun-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 3e3064b43daf523fa9db140fba80687b1fe64360c07568a532257fa7ae813d55
MD5 2ec698a645894824cc3985c2f1ab1dbe
BLAKE2b-256 9f37923897e9ae4c7d8c9372353e2ceaa06ccafa77aeba7a4250af3ca8289482

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