python / python/cpython

Alternative to mock_calls for awaits in AsyncMock

Open
#121,685 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

stdlib type-feature
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the AsyncMock and Mock.mock_calls entry points, along with the existing assert_await* methods and await_* attributes. Review the linked Discourse discussion for the prior design context; done means an agreed API and corresponding behavior that can verify awaits across an AsyncMock and its children.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
testing-qa
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.