This test sometimes fails because it randomly chooses True or False. Pytest will retry it up to 3 times if it fails.
PyTest
import pytest
import random
@pytest.mark.flaky(reruns=3)
def test_flaky():
# This test randomly failsassert random.choice([True, False])
if __name__ == "__main__":
pytest.main(["-v", __file__])
OutputSuccess
Important Notes
Use retries sparingly to avoid hiding real problems.
Flaky tests should be fixed eventually, not just retried.
Pytest-flaky plugin or built-in rerunfailures plugin can be used for retries.
Summary
Flaky tests fail randomly and need special handling.
Use @pytest.mark.flaky(reruns=N) to retry tests automatically.
Retries help reduce false failures but don't replace fixing flaky tests.
Practice
(1/5)
1. What is the main purpose of marking a test as flaky in pytest using @pytest.mark.flaky(reruns=N)?
easy
A. To skip the test permanently
B. To automatically retry the test N times if it fails
C. To mark the test as slow and run it last
D. To run the test only once without retries
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 @pytest.mark.flaky(reruns=N)
This decorator tells pytest to rerun the test up to N times if it fails, increasing chances of passing despite flakiness.
Final Answer:
To automatically retry the test N times if it fails -> Option B
Quick Check:
Flaky test retry = reruns N times [OK]
Hint: Retries mean automatic rerun on failure [OK]
Common Mistakes:
Confusing flaky with skipped tests
Thinking flaky marks slow tests
Assuming flaky disables retries
2. Which of the following is the correct syntax to retry a flaky test 3 times in pytest?
easy
A. @pytest.flaky(tries=3)
B. @pytest.retry(3)
C. @pytest.mark.retry(3)
D. @pytest.mark.flaky(reruns=3)
Solution
Step 1: Recall the correct decorator name and parameter
The correct decorator is @pytest.mark.flaky with parameter reruns to 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 D
Quick Check:
Decorator name + reruns param = correct syntax [OK]
Hint: Use @pytest.mark.flaky with reruns param [OK]
Common Mistakes:
Using @pytest.retry instead of @pytest.mark.flaky
Using wrong parameter names like tries
Missing the mark prefix
3. Given this pytest test code snippet, what will be the output if the test randomly fails once and passes on retry?
import pytest
@pytest.mark.flaky(reruns=2)
def test_random():
import random
assert random.choice([True, False])
medium
A. Test may pass after 1 or 2 retries if random returns True
B. Test always fails because random.choice is unpredictable
C. Test runs only once and fails if random returns False
D. Test is skipped due to flaky mark
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 A
Quick Check:
Retries allow passing despite random failure [OK]
Hint: Retries give multiple chances to pass random failures [OK]
Common Mistakes:
Assuming flaky skips tests
Thinking test always fails on first try
Ignoring retry count effect
4. You wrote this flaky test with retry but it never retries on failure. What is the error?
B. The decorator should be @pytest.retry, not @pytest.mark.flaky
C. The parameter name should be 'reruns', not 'rerun'
D. The test cannot retry if it always fails
Solution
Step 1: Check the decorator parameter spelling
The correct parameter for retry count is reruns, not rerun.
Step 2: Understand impact of wrong parameter
Using rerun is ignored by pytest, so no retries happen despite failures.
Final Answer:
The parameter name should be 'reruns', not 'rerun' -> Option C
Quick Check:
Correct param spelling = reruns [OK]
Hint: Check exact parameter spelling: reruns, not rerun [OK]
Common Mistakes:
Misspelling reruns parameter
Using wrong decorator name
Expecting retries on always failing test
5. You want to reduce false failures from a flaky test that sometimes fails due to timing issues. Which approach best combines flaky test detection and retry in pytest?
hard
A. Use @pytest.mark.flaky(reruns=3) and add explicit wait in test code
B. Use @pytest.mark.skip to ignore the flaky test
C. Remove retries and fix test to always pass
D. Use @pytest.mark.flaky(reruns=0) to disable retries
Solution
Step 1: Understand flaky test retry purpose
Retries help reduce false failures by rerunning tests that fail randomly.