python
runtime_error
ai_generated
true
AssertionError: 期望 'send_email' 被调用一次。实际调用 0 次。
AssertionError: Expected 'send_email' to be called once. Called 0 times.
ID: python/pytest-mock-reset-between-calls
80%修复率
85%置信度
0证据数
2025-12-12首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 3.10 | active | — | — | — |
| 3.12 | active | — | — | — |
根因分析
测试断言 `mock.assert_called_once()`,但 mock 在断言前被重置(例如被 `mock.reset_mock()` 或某个 fixture 重置),或者被测代码 patch 了错误的目标。
English
The test asserts `mock.assert_called_once()` but the mock was reset (e.g. by `mock.reset_mock()` or a fixture that resets it) before the assertion, or the code under test was patched at the wrong target.
解决方案
-
95% 成功率
Patch where the name is looked up and verify the patch target: @patch('app.services.user.send_email') def test_signup(mock_send): signup() mock_send.assert_called_once() -
90% 成功率
Remove any `reset_mock()` calls between the action and the assertion, or use `mock.call_count` to inspect at the exact point.
无效尝试
常见但无效的做法:
-
70% 失败
Still fails if the mock was never called; masks the real issue.
-
95% 失败
Repeating the assertion does not change the call count.