Skip to content

Instantly share code, notes, and snippets.

@rfong
Last active September 6, 2019 09:00
Show Gist options
  • Save rfong/c9675226e6dd194c77af to your computer and use it in GitHub Desktop.
Save rfong/c9675226e6dd194c77af to your computer and use it in GitHub Desktop.
django retry decorator for tests
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