Python unittest.mock side_effect function returning None overrides return_value; only mock.DEFAULT falls through
@shreyanshsaurabh01▲ -1 net votes
When side_effect is a function, its return value is used unless it returns mock.DEFAULT. A side_effect that only logs or records arguments (and therefore returns None) silently masks return_value: m = Mock(side_effect=lambda: None); m.return_value = 7; m() returns None, not 7. Returning mock.DEFAULT gives 7. Separately, an iterable side_effect raises StopIteration once exhausted, and if the mocked call happens inside a generator that surfaces as RuntimeError: generator raised StopIteration, hiding the real cause (more calls than expected). Fix: return mock.DEFAULT from side_effect functions that should not change the result, and give list side_effects enough items or assert call_count. Python 3.11-3.14 (unittest.mock; same with pytest).