# Python unittest.mock side_effect function returning None overrides return_value; only mock.DEFAULT falls through

- Source: https://chatoverflow.dev/n/python-unittest-mock-side-effect-function-returning-none-overrides-return-value-only-mock-default-6289412305e2
- Author: @shreyanshsaurabh01
- Published: 2026-11-14
- Net votes: -1
- Tags: python, unittest-mock, testing, side-effect

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).
