Reset mock call history without silently changing behavior
Also published in our primary archive.
Reset mock call history without silently changing behavior
reset_mock() clears call evidence by default, but it retains configured return_value and side_effect. Resetting history therefore does not necessarily reset what the next call does.
The fixture configures both fields. Although the return_value is configured, the callable side_effect supplies reply:one and takes precedence for the first call. After reset_mock(), call_count is zero, but the same callable remains installed. Calling with two produces reply:two, proving in this limited experiment that interaction history and configured behavior are separate state.
The last reset passes return_value=True, side_effect=True. The explicit options clear the callable and replace the configured return value with a fresh default mock. The fixture verifies side_effect is None and checks the return-value object's type rather than its unstable representation. A following call has one new recorded interaction.
This is useful when reusing a double across phases, but it does not make shared fixtures automatically isolated: other assigned attributes and external state may still need deliberate setup. reset_mock() is available with unittest.mock from Python 3.3. Its return_value and side_effect keyword-only reset options were added in Python 3.6; see the official reset_mock() reference.
AI assistance disclosure: this article and its synthetic fixture were drafted with AI assistance.
from unittest.mock import Mock
def reply(label):
return f"reply:{label}"
service = Mock(return_value="configured", side_effect=reply)
assert service("one") == "reply:one"
service.assert_called_once_with("one")
service.reset_mock()
assert service.call_count == 0
assert service.return_value == "configured"
assert service.side_effect is reply
assert service("two") == "reply:two"
service.assert_called_once_with("two")
service.reset_mock(return_value=True, side_effect=True)
assert service.call_count == 0
assert service.side_effect is None
assert isinstance(service.return_value, Mock)
service("three")
assert service.call_count == 1
print("default reset: calls=0, side_effect=retained")
print("next call after default reset: reply:two")
print("explicit reset: side_effect=None, return_value=Mock")
default reset: calls=0, side_effect=retained
next call after default reset: reply:two
explicit reset: side_effect=None, return_value=Mock