0
0
PyTesttesting~20 mins

Flaky test detection and retry in PyTest - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Flaky Test Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this flaky test retry code?
Consider this pytest test with a retry decorator that reruns the test up to 3 times if it fails. What will be the final test result after running this test?
PyTest
import pytest
import random

@pytest.mark.flaky(reruns=3)
def test_random_fail():
    assert random.choice([True, False])
ATest result is always skipped due to flaky marker
BTest always fails because random.choice can be False every time
CTest passes only if first run succeeds, retries ignored
DTest passes if any retry succeeds within 3 reruns
Attempts:
2 left
💡 Hint
The reruns parameter allows retrying the test if it fails up to the given number of times.
assertion
intermediate
2:00remaining
Which assertion correctly detects flaky test retry success?
You want to assert that a flaky test retried up to 2 times eventually passes. Which assertion correctly checks this in pytest?
Aassert test_result.wasSuccessful()
Bassert test_result.rerun_count <= 2 and test_result.passed
Cassert test_result.skipped
Dassert test_result.failed and test_result.rerun_count == 2
Attempts:
2 left
💡 Hint
Check both the number of reruns and the final pass status.
🔧 Debug
advanced
2:00remaining
Why does this flaky test retry decorator not work as expected?
This pytest test uses a retry decorator but never retries on failure. Identify the bug.
PyTest
import pytest

def retry_test(func):
    def wrapper():
        for _ in range(3):
            try:
                func()
                break
            except AssertionError:
                pass
        else:
            raise AssertionError("Test failed after retries")
    return wrapper

@retry_test
def test_always_fail():
    assert False
AThe wrapper does not raise AssertionError after retries, so pytest sees no failure
BThe retry loop is missing a return statement after success
CThe decorator is missing @pytest.mark.flaky
DThe test function is missing a parameter for retries
Attempts:
2 left
💡 Hint
Pytest needs the test to raise AssertionError to mark failure.
🧠 Conceptual
advanced
2:00remaining
What is the main risk of using flaky test retries without fixing root causes?
Why is relying on flaky test retries considered a bad practice in software testing?
AIt increases test execution speed significantly
BIt automatically fixes bugs in the code under test
CIt hides real issues and reduces test reliability over time
DIt guarantees all tests will eventually pass
Attempts:
2 left
💡 Hint
Think about test trustworthiness and maintenance.
framework
expert
2:00remaining
Which pytest plugin and option enable automatic flaky test retries with reporting?
You want to automatically retry flaky tests up to 5 times and see retry counts in the test report. Which plugin and command line option achieve this?
Apytest-rerunfailures plugin with --reruns=5 option
Bpytest-flaky plugin with --flaky-runs=5 option
Cpytest-repeat plugin with --count=5 option
Dpytest-xdist plugin with --max-retries=5 option
Attempts:
2 left
💡 Hint
Look for the plugin designed specifically for rerunning failed tests.