Last active
September 6, 2019 09:00
-
-
Save rfong/c9675226e6dd194c77af to your computer and use it in GitHub Desktop.
django retry decorator for tests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import unittest | |
from django.utils.functional import wraps | |
def retry(tries=1, ignore_exception=None): | |
""" | |
Decorator to retry a test. | |
:param ignore_exception: Exception class to catch. | |
:param tries: number of times to try running the wrapped function. | |
Don't use if you need to do something with the return value. | |
""" | |
def decorator(func): | |
@wraps(func) | |
def wrapped_func(*args, **kwargs): | |
num_tries = max(1, tries) | |
while num_tries > 0: | |
try: | |
return func(*args, **kwargs) | |
except ignore_exception as e: | |
num_tries -= 1 | |
if num_tries == 0: | |
raise unittest.SkipTest('Skipping test; raised exception %s', e) | |
return wrapped_func | |
return decorator |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment