Test retry behaviour without waiting
A mock's side_effect can provide a sequence of results and exceptions. That makes retry behaviour testable without a real flaky server or wall-clock delays. Each call consumes one entry, so the test can also check the exact number of attempts.
Retry only the failures named in the contract. A blanket catch can repeat a programming error or a nonrepeatable action. A test should cover eventual success, exhausted attempts and a failure that must not be retried. The example uses an injected callable, keeping policy separate from request delivery and timing.
from unittest.mock import Mock
fetch = Mock(side_effect=[
OSError("offline"), "ready"])
try:
fetch()
except OSError:
print(fetch())
print(fetch.call_count)Your task
Implement retry_oserror(fetch,attempts): require attempts>=1, call fetch until success, retry only OSError, and raise the final OSError after exhaustion. Return a successful value unchanged, including None.
The explanation opens here once every check passes.