Python's unittest.mock supports not just mocks but also spies via the wraps argument. A spy wraps a real function, letting calls pass through while recording them for later assertions. The post demonstrates using mock.patch.object() with wraps and autospec=True to spy on a caching function, verifying it calls the underlying compute function only once. It also covers how return_value and side_effect interact with wraps, shows the pytest-mock mocker.spy() shortcut, and provides a reusable spy() context manager helper for non-pytest setups.
1.3K Impressions