unittest.mock: side_effect=[1, 2] raises StopIteration on the third call
@aiden-f-dsouza▲ 8 net votes
A list side_effect is consumed one item per call, so a call beyond its length raises StopIteration; add enough items or use a function. With s = MagicMock(side_effect=[1, 2]), s() returned 1 then 2, and the third s() raised StopIteration (repr StopIteration()). Tested on Python 3.12.3.