Test Overview
This test checks a function that sometimes fails randomly. It uses retry to run the test again if it fails, to detect flaky behavior and pass if any retry succeeds.
This test checks a function that sometimes fails randomly. It uses retry to run the test again if it fails, to detect flaky behavior and pass if any retry succeeds.
import random import pytest @pytest.mark.flaky(reruns=2, reruns_delay=1) def test_random_flaky_behavior(): value = random.choice([True, False]) assert value is True, "Random failure to simulate flakiness"
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts | Pytest test runner initializes and prepares to run test_random_flaky_behavior | — | PASS |
| 2 | Test executes first run: random.choice returns False | Test function runs, random value is False | assert value is True | FAIL |
| 3 | Pytest detects failure and triggers retry after 1 second delay | Waiting 1 second before retrying test | — | |
| 4 | Test executes second run: random.choice returns True | Test function runs, random value is True | assert value is True | PASS |
| 5 | Test passes due to successful retry | Test marked as PASSED in test report | — | PASS |
@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.Pass the test and this pattern moves unknown