Challenge - 5 Problems
Retry Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this retry decorator on flaky test?
Consider this Python code using Selenium and a retry decorator for flaky tests. What will be printed when the test function is run?
Selenium Python
import random def retry(times): def decorator(func): def wrapper(): for attempt in range(1, times + 1): try: func() print(f"Test passed on attempt {attempt}") break except Exception as e: print(f"Attempt {attempt} failed: {e}") if attempt == times: print("Test failed after retries") return wrapper return decorator @retry(3) def flaky_test(): if random.choice([True, False]): raise Exception("Random failure") flaky_test()
Attempts:
2 left
💡 Hint
The test stops retrying once it passes.
✗ Incorrect
The retry decorator runs the test up to 3 times. If the test passes on the first try, it prints 'Test passed on attempt 1' and stops retrying.
❓ assertion
intermediate1:30remaining
Which assertion correctly verifies retry count in flaky test?
You want to assert that a flaky Selenium test retried exactly 3 times before failing. Which assertion code snippet correctly checks this?
Selenium Python
retry_attempts = [] def flaky_test(): retry_attempts.append(1) raise Exception("Fail") # Retry decorator calls flaky_test 3 times # Which assertion below is correct?
Attempts:
2 left
💡 Hint
retry_attempts is a list, check its length.
✗ Incorrect
len(retry_attempts) returns the number of times the test was retried. The other options use invalid syntax or methods.
🔧 Debug
advanced2:30remaining
Why does this retry decorator not retry on failure?
This retry decorator is supposed to retry a flaky Selenium test 3 times, but it only runs once. What is the bug?
Selenium Python
def retry(times): def decorator(func): def wrapper(): for attempt in range(times): try: func() print(f"Passed on attempt {attempt}") return except Exception: print(f"Failed on attempt {attempt}") return wrapper return decorator @retry(3) def flaky_test(): raise Exception("Fail") flaky_test()
Attempts:
2 left
💡 Hint
Check what happens when the test passes or fails inside the loop.
✗ Incorrect
The 'return' inside the try block exits the wrapper function immediately after first attempt, so no retries happen.
❓ framework
advanced1:30remaining
Which pytest feature best supports retrying flaky Selenium tests?
You want to automatically retry flaky Selenium tests in pytest without writing custom decorators. Which pytest feature or plugin should you use?
Attempts:
2 left
💡 Hint
Look for a plugin that reruns failed tests.
✗ Incorrect
pytest-rerunfailures reruns failed tests automatically. The others serve different purposes.
🧠 Conceptual
expert2:00remaining
What is the main risk of using retry mechanisms for flaky Selenium tests?
Retrying flaky Selenium tests can hide underlying problems. What is the biggest risk of relying heavily on retries?
Attempts:
2 left
💡 Hint
Think about what happens if flaky tests are ignored by retries.
✗ Incorrect
Retries can hide real bugs or environment issues, making the test suite less trustworthy.