mockito-python
mockito-python copied to clipboard
verify() with timeout
Any plans to support the equivalent of Java's org.mockito.verification.Timeout
?
mockito
for python is basically community driven. But 'timeout' seems really like an edge user case when you're already using mocks, and I don't think you can brute-force-just-add-it to the current lib bc we have to think about how python usually deals with concurrency.
Thanks.
I find Java mockito very useful when testing callbacks from other threads. Let me give an example.
someAsyncFunction(myMock);
verify(myMock, timeout(30_000)).callback();
The Java timeout
implementation essentially polls until the condition is satisfied or the timeout expires. It shouldn't be too hard to translate over. If I find I'm missing this a lot, I'll have a go.
FWIW there is a note in the timeout
Javadoc: "This feature should be used rarely - figure out a better way of testing your multi-threaded system.". I obviously disagree, although I see the sense of the similar advice for after()
since that always waits.
Well, this really depends on how the code actually realizes its asynchronicity. Now, if some_async_fn
would be async
you could await
its result. (In older pythons you converted the thread probably to a Future
.)
Otherwise, in python dependency injection is basically optional. If some_async_fn
actually is defer_work(partial(fn, myMock))
, then you can still mock the otherwise internal, hidden Thread
constructor such a fn would call and just ask if the code constructs the Thread
correctly and then calls start()
on its return value. A second testcase would be if fn
calls myMock.callback()
during execution.