-
-
Notifications
You must be signed in to change notification settings - Fork 291
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Disable wait
for unittest
#106
Comments
You can change the wait function temporarily in your test: func.retry.wait = wait_none Using mock for example, that should be easy to make sure it's then restored to the original. |
@DanEEStar the way I handle time at least in synchronous tests is that I use a datetime-mock library, my preferred one is freezegun but feel free to use any other. The trick is that there's usually a function to advance the frozen clock manually, for
With that in mind I mock |
We would also appreciate some specific docs on how to mock out delays in running unit tests. Our current approach just got way more complicated https://review.openstack.org/#/c/596471 |
After working on this for a while I went with |
If anyone finds this now, I dug into @steveb's code and found that in a later pull request it looks like you found an even simpler way of doing this: openstack-archive/tripleo-common@eba4912#diff-f02c888d3d91763350bb6b11c12bf0ffR167 In short, if you decorate a function
And that makes the retry calls happen immediately. I whipped up a gist that demonstrates Steve's method: https://gist.github.com/davidscolgan/39433d2de29ea1282bbecaf5afd73900 |
@davidscolgan Thank you |
Unfortunately, you don't always have an access to the function in a unit test. I'd rather go with the proposal in #228. |
I prefer this apporach too |
I was hitting this problem too but thanks to (https://stackoverflow.com/a/73890688) I managed to avoid this problem by patching tenacity nap directly. |
In my case since my function was async I had to |
My application talks to a variety of other services through (async) client libraries, and they each use a dedicated What I ended up doing was to:
See https://gist.github.com/jmehnle/2cf4deba48c32c01fdf6c0ee5248a1d1. Now I can use that I think adding a native facility to |
I used this in my conftest to disable waiting for all my retry cases: tenacity.wait.wait_exponential_jitter.__call__ = lambda __, ___: 0 I want the retry to happen because I want to make sure that my function does retry on certain cases. |
I am using the
@retry
decorator on a function which makes an HTTP-request.I am using this exact decorator call as an example:
I have a unit tests which tests, that
func
does indeed get called multiple times when the post-request fails.But it is a bit annoying that the test takes a long time.
Is it possible to disable the
wait
time somehow only in the unit test to make the test faster?Do I have to mock a specific function?
I also posted this on stackoverflow, in case you want some points :)
https://stackoverflow.com/questions/47906671/python-retry-with-tenacity-disable-wait-for-unittest
The text was updated successfully, but these errors were encountered: