0
0
Selenium Pythontesting~20 mins

Retry mechanism for flaky tests in Selenium Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Retry Mastery Badge
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 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()
ATest passed on attempt 3
BAttempt 1 failed: Random failure\nTest passed on attempt 2
CTest passed on attempt 1
DAttempt 1 failed: Random failure\nAttempt 2 failed: Random failure\nAttempt 3 failed: Random failure\nTest failed after retries
Attempts:
2 left
💡 Hint
The test stops retrying once it passes.
assertion
intermediate
1: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?
Aassert retry_attempts == 3
Bassert len(retry_attempts) == 3
Cassert retry_attempts.count == 3
Dassert retry_attempts.length == 3
Attempts:
2 left
💡 Hint
retry_attempts is a list, check its length.
🔧 Debug
advanced
2: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()
AThe decorator is missing @functools.wraps
BThe range(times) should start at 1, not 0
CThe exception is not caught properly
DThe 'return' inside try block stops retries after first attempt
Attempts:
2 left
💡 Hint
Check what happens when the test passes or fails inside the loop.
framework
advanced
1: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?
Apytest-rerunfailures plugin
Bpytest-mock plugin
Cpytest-cov plugin
Dpytest-xdist plugin
Attempts:
2 left
💡 Hint
Look for a plugin that reruns failed tests.
🧠 Conceptual
expert
2: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?
AMasking real issues and reducing test suite reliability
BIncreasing test execution speed significantly
CMaking tests easier to maintain and debug
DGuaranteeing 100% test pass rate
Attempts:
2 left
💡 Hint
Think about what happens if flaky tests are ignored by retries.