I'm using the retry
decorator in some code in python. But I want to speed up my tests by removing its effect.
My code is:
@retry(subprocess.CalledProcessError, tries=5, delay=1, backoff=2, logger=logger)
def _sftp_command_with_retries(command, pem_path, user_at_host):
# connect to sftp, blah blah blah
pass
How can I remove the effect of the decorator while testing? I can't create an undecorated version because I'm testing higher-level functions that use this.
Since retry
uses time.sleep
to back off, ideally I'd be able to patch time.sleep
but since this is in a decorator I don't think that's possible.
Is there any way I can speed up testing code that uses this function?
Update
I'm basically trying to test my higher-level functions that use this to make sure that they catch any exceptions thrown by _sftp_command_with_retries
. Since the retry
decorator will propagate them I need a more complicated mock.
So from here I can see how to mock a decorator. But now I need to know how to write a mock that is itself a decorator. It needs to call _sftp_command_with_retries
and if it raises an exception, propagate it, otherwise return the return value.
Adding this after importing my function didn't work:
_sftp_command_with_retries = _sftp_command_with_retries.__wrapped__