python / cpython

The Python programming language
https://www.python.org
Other
62.36k stars 29.94k forks source link

Alternative to mock_calls for awaits in AsyncMock #121685

Open ziima opened 2 months ago

ziima commented 2 months ago

Feature or enhancement

Proposal:

I’d like to have an alternative to mock_calls for awaited coroutines to AsyncMock.

Mock.mock_calls contain all calls to the mock and its children. When used on AsyncMock, the mock_calls record is added when coroutine is created, regardless of whether it was awaited. Awaits can be checked using assert_await* methods or await_* attributes, but those only record the mock itself and there is no option to check awaits on the AsyncMock and its children at once.

The best case scenario, it should be possible to switch from sync version

mock = Mock()
mock(sentinel.foo)
assert mock.mock_calls == [call(sentinel.foo)]

to async version

mock = AsyncMock()
await mock(sentinel.foo)
assert mock.mock_awaits == [call(sentinel.foo)]

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

tirkarthi commented 2 months ago

As per my understanding this is recorded as part of await_args_list internally but not available as documented public API.

https://github.com/python/cpython/blob/94e6644584d9cb08a4edcd1027e288386184816b/Lib/unittest/mock.py#L2295

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)]
ziima commented 2 months ago

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 awaits internally similarly to how mock_calls are handled. But it shouldn't be big deal.