Framework Mode - Flaky test detection and retry
Folder Structure
tests/ ├── test_login.py ├── test_checkout.py ├── test_profile.py conftest.py pytest.ini utils/ ├── retry_helper.py ├── flaky_detector.py reports/ └── latest_report.html
Jump into concepts and practice - no test required
tests/ ├── test_login.py ├── test_checkout.py ├── test_profile.py conftest.py pytest.ini utils/ ├── retry_helper.py ├── flaky_detector.py reports/ └── latest_report.html
tests/ folder, e.g., test_login.py. These contain test functions using pytest.conftest.py for setup, teardown, and retry hooks.retry_helper.py to implement retry logic and flaky_detector.py to log flaky test info.pytest.ini to configure pytest plugins and retry options.reports/ folder for review.[pytest] addopts = --reruns 2 --reruns-delay 1This retries failed tests up to 2 times with 1 second delay.
.env files to toggle flaky detection on/off without code changes.conftest.py to detect flaky tests by tracking retries and logging flaky occurrences.reports/ folder for easy access and historical comparison.Where in this folder structure would you add a new utility function to log flaky test occurrences during retries?
@pytest.mark.flaky(reruns=N)?@pytest.mark.flaky(reruns=N)@pytest.mark.flaky with parameter reruns to specify retry count.@pytest.mark.flaky(reruns=3), which is the correct syntax for retrying 3 times.import pytest
@pytest.mark.flaky(reruns=2)
def test_random():
import random
assert random.choice([True, False])import pytest
@pytest.mark.flaky(rerun=3)
def test_example():
assert Falsereruns, not rerun.rerun is ignored by pytest, so no retries happen despite failures.