I'm using a retry decorator to run the test case if failed. So, want to know how many times the retry function called
def retry(tries=3, delay=10):
def decorator(func):
@wraps(func)
def func_retry(*args, **kwargs):
n_tries, n_delay = tries, delay
output = error = None
while n_tries >= 1:
try:
output = func(*args, **kwargs)
return output
except AssertionError as e:
n_tries = n_tries - 1
error = e.__str__()
print(f'Retry error: "{func.__name__}" due to "{error}". So, Retrying execution [{(tries - n_tries)}/{tries}] in {delay} second(s)...')
time.sleep(n_delay)
return output
return func_retry
return decorator
Sample function for testing purpose
@retry()
def test():
assert "x" == "y"
I want to know how many times retry function in retry decorator called like retry.count or retry.tries
You can work with attributes of the final function that is returned. (You can also do it on the decorator but then it is not accessible from the outside).
Output: