python / python/cpython

Alternative to mock_calls for awaits in AsyncMock

未关闭
#121,685 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

stdlib type-feature
主要语言
Python
星标
77.2k
派生
35.9k
PR 合并指标
PR 指标待抓取

描述

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

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

先阅读 AsyncMock 和 Mock.mock_calls 入口,以及现有的 assert_await* 方法和 await_* 属性。查看链接的 Discourse 讨论,了解之前的设计背景;完成的标准是:具备一套已达成共识的 API 及相应行为,能够验证 AsyncMock 及其子对象上的 await。

由索引模型根据 Issue 内容生成。

评估

技术栈
python
领域
testing-qa
Issue 类型
功能
难度
5/5
预计耗时
一周以上
活跃度
停滞
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。