-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Description
What's the problem this feature will solve?
Flaky tests are a major pain point in modern CI/CD pipelines. Currently, pytest (and its ecosystem) supports retries via plugins like pytest-rerunfailures
, but these retries are static, they simply rerun failed tests a fixed number of times without any analysis or adaptation.
This causes unnecessary CI time consumption and makes it difficult for developers to distinguish between:
- genuine regressions,
- transient or environment-related issues (network, timeout, race condition),
- and inherently flaky test logic.
In other words, pytest lacks a built-in way to learn from historical test behavior or adjust retry strategies dynamically based on failure patterns.
Describe the solution you'd like
A Smart Retry System that intelligently manages flaky tests and adapts retry behavior using failure pattern detection.
Example:
@pytest.mark.smart_retry(max_attempts=3, analyze=True)
def test_api_call():
# This test might fail intermittently due to network timeouts
...
Core behaviors:
-
Failure Categorization: Analyze stack traces and exception types to classify failures (e.g., network, timeout, assertion, resource contention).
-
Retry Strategy Tuning: Automatically adjust how retries happen:
- Exponential backoff for timeouts
- No retry for deterministic assertion failures
- Limited retries for transient network issues -
Failure History Tracking: Store test outcomes and failure categories in a lightweight local DB (SQLite or JSON).
-
Flakiness Metrics: Compute per-test flakiness rates across runs (e.g., "test_api_call failed 2/10 times in the last 5 builds").
Alternative Solutions
- pytest-rerunfailures: Provides basic retry capability but does not classify or analyze failures, and treats all failures equally.
- pytest-flakefinder: Helps detect flaky tests but reruns tests multiple times blindly and doesn’t adapt future
- CI-level reruns: Often implemented in GitHub Actions or Jenkins, but lack any integration with test logic or test metadata.
While these workarounds help mitigate flakes temporarily, they do not address the underlying need for failure intelligence and adaptive retries.
Additional context
This proposal can begin as a standalone plugin, e.g. pytest-smart-retry, built using pytest’s hook system (pytest_runtest_logreport, pytest_sessionfinish, etc.).
Once mature, and if it gains community adoption, it could potentially be integrated into core pytest or become an officially recommended plugin.
Would love to contribute if given a green signal