python
runtime_error
ai_generated
true
AssertionError: Expected 'mock' to have been called once. Called 0 times.
ID: python/unittest-mock-call-count
80%Fix Rate
86%Confidence
0Evidence
2024-08-22First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 3.8 | active | — | — | — |
| 3.9 | active | — | — | — |
| 3.10 | active | — | — | — |
Root Cause
使用assert_called_once_with或assert_called_once时,mock没有被调用或调用次数不符。
generic中文
使用assert_called_once_with或assert_called_once时,mock没有被调用或调用次数不符。
Workarounds
-
90% success
print(mock.call_count) print(mock.mock_calls)
-
85% success
mock.assert_any_call(args)
Dead Ends
Common approaches that don't work:
-
60% fail
return_value是返回值,不是调用记录
-
70% fail
如果代码逻辑错误,增加调用次数是错误修复