runtime
329 / 500
question 329 Decorators & Context Managers · medium

Tests for a retry decorator

retrying.py has retry_on(*exceptions, attempts=3, sleep=None): it retries the decorated function on the listed exceptions, up to attempts calls in total, calling sleep(1), sleep(2), sleep(4), … between them, and raises the last error when the attempts run out. Other exceptions are raised at once. Write unittest tests that pass on it and catch broken versions. Use a fake function that fails a set number of times, and pass sleep=waits.append so nothing really waits.

Writea unittest.TestCase (or test_ functions) that decorate small fake functions with retry_on and assert what happens
Ruleslisted exceptions are retried; others are raised immediately; at most attempts calls; waits double; the last error is raised
Gradedyour tests run against the real decorator and against broken ones; don't call them yourself

Loading your progress…

Tests for a retry decorator — Python practice · Runtime