What if your tests could tell the difference between real bugs and random glitches all by themselves?
Why Flaky test detection and retry in PyTest? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine running your tests manually every time you make a small change. Sometimes tests pass, sometimes they fail for no clear reason. You spend hours trying to figure out if the problem is real or just a random glitch.
Manual testing is slow and tiring. You can easily miss flaky tests that fail randomly. This causes confusion and wastes time fixing problems that do not really exist. It is hard to trust your test results.
Flaky test detection and retry automatically rerun tests that fail randomly. This helps identify if a failure is consistent or just a one-time glitch. It saves time and gives you confidence in your test results.
def test_feature(): result = feature() assert result == expected # If it fails, you rerun manually many times
@pytest.mark.flaky(reruns=3) def test_feature(): result = feature() assert result == expected
You can trust your tests to catch real problems and ignore random failures, making your development faster and less stressful.
In a team project, flaky tests caused confusion and delays. Using flaky test detection and retry helped the team focus on real bugs and deliver features on time.
Manual test reruns waste time and cause confusion.
Flaky test detection automatically retries tests to find real failures.
This improves trust in tests and speeds up development.
Practice
@pytest.mark.flaky(reruns=N)?Solution
Step 1: Understand the flaky test concept
Flaky tests fail randomly due to timing or environment issues, so retries help reduce false failures.Step 2: Analyze the effect of
This decorator tells pytest to rerun the test up to N times if it fails, increasing chances of passing despite flakiness.@pytest.mark.flaky(reruns=N)Final Answer:
To automatically retry the test N times if it fails -> Option BQuick Check:
Flaky test retry = reruns N times [OK]
- Confusing flaky with skipped tests
- Thinking flaky marks slow tests
- Assuming flaky disables retries
Solution
Step 1: Recall the correct decorator name and parameter
The correct decorator is@pytest.mark.flakywith parameterrerunsto specify retry count.Step 2: Match syntax with options
@pytest.mark.flaky(reruns=3) uses@pytest.mark.flaky(reruns=3), which is the correct syntax for retrying 3 times.Final Answer:
@pytest.mark.flaky(reruns=3) -> Option DQuick Check:
Decorator name + reruns param = correct syntax [OK]
- Using @pytest.retry instead of @pytest.mark.flaky
- Using wrong parameter names like tries
- Missing the mark prefix
import pytest
@pytest.mark.flaky(reruns=2)
def test_random():
import random
assert random.choice([True, False])Solution
Step 1: Understand the flaky decorator effect
The test will rerun up to 2 times if it fails, allowing multiple chances to pass.Step 2: Analyze the random assertion
The assertion randomly passes or fails. If it fails once, retry can pass if random returns True on retry.Final Answer:
Test may pass after 1 or 2 retries if random returns True -> Option AQuick Check:
Retries allow passing despite random failure [OK]
- Assuming flaky skips tests
- Thinking test always fails on first try
- Ignoring retry count effect
import pytest
@pytest.mark.flaky(rerun=3)
def test_example():
assert FalseSolution
Step 1: Check the decorator parameter spelling
The correct parameter for retry count isreruns, notrerun.Step 2: Understand impact of wrong parameter
Usingrerunis ignored by pytest, so no retries happen despite failures.Final Answer:
The parameter name should be 'reruns', not 'rerun' -> Option CQuick Check:
Correct param spelling = reruns [OK]
- Misspelling reruns parameter
- Using wrong decorator name
- Expecting retries on always failing test
Solution
Step 1: Understand flaky test retry purpose
Retries help reduce false failures by rerunning tests that fail randomly.Step 2: Combine retry with test stabilization
Adding explicit waits addresses timing issues, improving test stability alongside retries.Step 3: Evaluate other options
Skipping ignores tests, removing retries loses retry benefit, and reruns=0 disables retries.Final Answer:
Use @pytest.mark.flaky(reruns=3) and add explicit wait in test code -> Option AQuick Check:
Retries + waits = best flaky test handling [OK]
- Skipping flaky tests instead of fixing
- Disabling retries by setting reruns=0
- Ignoring timing issues causing flakiness
