python
runtime_error
ai_generated
true
StopIteration: raised by side_effect list exhausted
ID: python/unittest-mock-side-effect
80%Fix Rate
85%Confidence
0Evidence
2024-06-11First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 3.8 | active | — | — | — |
| 3.12 | active | — | — | — |
Root Cause
A mock's side_effect was set to a finite list, and the code under test called the mock more times than the list length, raising StopIteration.
generic中文
mock 的 side_effect 被设置为有限列表,而被测代码调用 mock 的次数超过列表长度,引发 StopIteration。
Workarounds
-
90% success
Use a callable for side_effect that handles any number of calls: m.side_effect = lambda x: x * 2
-
88% success
Extend the list to match the expected call count and assert: m.side_effect = [1, 2, 3] run() assert m.call_count == 3
Dead Ends
Common approaches that don't work:
-
85% fail
This hides the real issue: the test exercises more calls than expected, or the list is too short.
-
80% fail
Adding one None only delays the problem; a subsequent call raises StopIteration again.