Open ziima opened 2 months ago
As per my understanding this is recorded as part of await_args_list
internally but not available as documented public API.
from unittest.mock import AsyncMock
import asyncio
async def test():
mock = AsyncMock()
await mock(1)
mock(2)
print(f"Awaited {mock.await_args_list}")
print(f"Called {mock.mock_calls}")
asyncio.run(test())
python /tmp/foo.py
/tmp/foo.py:8: RuntimeWarning: coroutine 'AsyncMockMixin._execute_mock_call' was never awaited
mock(2)
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
Awaited [call(1)]
Called [call(1), call(2)]
Not entirely. await_args_list
is populated only on the mock itself, it's not populated by the calls on the children. The change would require recording await
s internally similarly to how mock_calls
are handled. But it shouldn't be big deal.
Feature or enhancement
Proposal:
I’d like to have an alternative to
mock_calls
for awaited coroutines toAsyncMock
.Mock.mock_calls
contain all calls to the mock and its children. When used onAsyncMock
, themock_calls
record is added when coroutine is created, regardless of whether it was awaited. Awaits can be checked usingassert_await*
methods orawait_*
attributes, but those only record the mock itself and there is no option to check awaits on theAsyncMock
and its children at once.The best case scenario, it should be possible to switch from sync version
to async version
Since I commonly use
mock_calls
checks in tests and there is no alternative for awaits, I tend to use it also for async code and omit the checks for awaits. I’m afraid that I may miss some awaits this way.Has this already been discussed elsewhere?
I have already discussed this feature proposal on Discourse
Links to previous discussion of this feature:
https://discuss.python.org/t/alternative-to-mock-calls-for-awaits-in-asyncmock/25425