pytest-smart-rerun
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).--smart-rerun-backoff=linear|exp|none: apply linear or exponential backoff to delay intervals.--smart-rerun-errors=name1,name2: only retry failures whose exception names match the allow-list.--smart-rerun-report=PATH: write structured JSON analytics for downstream dashboards or triage tooling.--smart-rerun-ai: enable MCP-powered AI analysis to classify failures and adapt retry behaviour.--smart-rerun-ai-report=PATH: append AI classifications and suggested actions to a JSON artifact.
Invalid values raise a Pytest UsageError, keeping failure feedback obvious.
Configuration via pytest.ini
All CLI options have config equivalents so you can bake defaults into your repository:
[pytest]
smart_rerun = true
smart_rerun_max = 3
smart_rerun_delay = 0.5
smart_rerun_backoff = exp
smart_rerun_errors = TimeoutError,ConnectionError
smart_rerun_report = reports/smart_rerun.json
smart_rerun_ai = true
smart_rerun_ai_endpoint = https://mcp.example.com/analyze
smart_rerun_ai_token = {{ env:SMART_RERUN_AI_TOKEN }}
smart_rerun_ai_report = reports/smart_rerun_ai.json
smart_rerun_ai_cache = .cache/smart_rerun_ai.json
smart_rerun_ai_redact = true
Features
- Intelligent reruns driven purely by Pytest hooks—no external dependencies.
- Automatic attempt logging with rerun-aware, colourised terminal summaries ready for CI.
- Per-test overrides through
@pytest.mark.smart_rerun(max=..., delay=..., backoff=..., errors=...). - Linear or exponential backoff strategies that cooperate with scripted delays.
- Exception-aware filtering so only flaky categories (e.g. timeouts) trigger reruns.
- Structured JSON analytics for dashboards or historical insights via
--smart-rerun-report. - AI-assisted classification that adjusts retry strategy on the fly (delay overrides, early exits, rich reporting).
- Works alongside existing Pytest plugins and respects fixture/state isolation.
AI-powered smart reruns
Turn on the MCP-backed engine with either --smart-rerun-ai or smart_rerun_ai = true. When a test fails, the plugin gathers the stack trace, exception metadata, and retry configuration, then:
- Sends it to the configured MCP endpoint (or falls back to built-in heuristics when offline).
- Classifies the failure (
flaky-network,deterministic,environmental, …) with a confidence score. - Adapts the retry policy dynamically—e.g.
retry_with_delay(2s)for network blips orstop_retryfor logic bugs. - Writes rich analytics into the main JSON report and, if configured, an AI-specific trail (
--smart-rerun-ai-report).
Caching (--smart-rerun-ai-cache) avoids duplicate calls, --smart-rerun-ai-redact trims overly verbose traces before transmission, and environment variables such as SMART_RERUN_AI_TOKEN can feed credentials securely.
CLI analytics
Run smart-rerun-report path/to/report.json (or multiple files) to see an aggregated dashboard in your terminal. Pass --format=json for machine-readable output or wire it into CI artefacts:
pytest --smart-rerun --smart-rerun-report reports/smart.json
smart-rerun-report reports/smart.json
smart-rerun-report reports/smart.json reports/previous.json --format=json
Future roadmap
- Natural-language retry policies that the MCP agent translates into structured plugin config.
- Correlating repeated failures across repositories to suggest code-level fixes automatically.
- Optional dashboards that visualise the JSON analytics over time for QA and release teams.
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
python -m buildpython -m twine check dist/*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.
Metadata
Release files for pytest-smart-rerun 0.1.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| pytest_smart_rerun-0.1.3.tar.gz | 17.0 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| pytest_smart_rerun-0.1.3-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 32.9 kB
Release files / pytest_smart_rerun-0.1.3.tar.gz
| Download URL | pytest_smart_rerun-0.1.3.tar.gz |
|---|---|
| Size | 17.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
05f25c6a75f7843baff3d5ef5ef4d3bf6dbda57bd0247fcc0a40b5acc2da2db9
|
|
BLAKE2b-256 checksum How to use checksums |
d72139cdbdfaf28b41058ce9d9c1498a5a98d66bb6aa0891863d3a15ca0a9f9d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.13.5
|
Release files / pytest_smart_rerun-0.1.3-py3-none-any.whl
| Download URL | pytest_smart_rerun-0.1.3-py3-none-any.whl |
|---|---|
| Size | 15.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
d9b4fa0fe97e1b1fcb83c9bad748975d34d8a6054bb04acb9768a3f0865c467b
|
|
BLAKE2b-256 checksum How to use checksums |
c0a482faf7dfabda9820ae17649d066789c6d0b50e67744357d1f3ae75ab2f61
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.13.5
|