python
type_error
ai_generated
true
断言错误:期望mock被调用一次,实际调用0次。
AssertionError: Expected mock to be called once. Called 0 times.
ID: python/unittest-mock-return-value-not-set
80%修复率
83%置信度
0证据数
2024-01-22首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 3.8 | active | — | — | — |
根因分析
unittest.mock中,mock对象未设置return_value或side_effect,导致调用未被模拟,实际调用次数为0。
English
在使用unittest.mock时,mock对象没有正确配置return_value或side_effect,导致测试中的调用没有被正确模拟,实际调用次数为0。
解决方案
-
95% 成功率
mock_obj.return_value = 'expected_value' result = mock_obj() assert result == 'expected_value'
-
90% 成功率
with patch('module.func') as mock_func: mock_func.side_effect = [1, 2] assert module.func() == 1
无效尝试
常见但无效的做法:
-
80% 失败
导入错误不是主要原因,mock配置才是根本问题。
-
90% 失败
这掩盖了问题,而不是修复mock配置。