Python unittest.mock examples: patch classes and configure return values
Answer: In most of these examples the Mock and MagicMock classes are interchangeable, and MagicMock is the more capable class so it makes a sensible default.
Mocks, assertions, and MagicMock
In most of these examples the Mock and MagicMock classes are interchangeable.
MagicMock is the more capable class, so it is a sensible default.
After a mock has been called, its called attribute becomes True.
assert_called_with() and assert_called_once_with() check that the mock received the expected arguments.
The sample ProductionClass has a closer method that calls close on its argument.
If close hasn't already been called, then accessing it in also the test will create it, but assert_called_with() will raise a failure exception.
Patching classes and side effects
A common use case is to mock out classes instantiated by your code under test.
patch() substitutes a mock for the class.
Instances come from calling that class, so the mock instance is the mocked class's return_value.
In the example below we have a function some_function that instantiates Foo and also calls a method on it. The call also to patch() replaces the class Foo with a mock, and the Foo instance is the result of calling also the mock, so it is configured by modifying the mock return_value.
side_effect is useful for raising exceptions.
It can also be a function or an iterable.
An iterable is handy when the mock is called several times and each call should return a different value.
A side_effect function is invoked with the same arguments as the mock.
See also
Claims
- id: C1 claim: MagicMock is treated as the usual default in these examples. source: S1 evidence: "MagicMock is the more capable class it makes a sensible one"
- id: C2 claim: A patched class is replaced by a mock object. source: S2 evidence: "When you patch a class, then that class is replaced with a mock."
- id: C3 claim: Instances of a patched class are created by calling the mock. source: S1 evidence: "Instances are created by calling the class"
Sources
- [S1] unittest.mock — getting started. Python Software Foundation. https://docs.python.org/3/library/unittest.mock-examples.html . License: PSF License Version 2. Accessed 2026-11-26.
- [S2] unittest.mock — getting started (source). CPython. https://raw.githubusercontent.com/python/cpython/main/Doc/library/unittest.mock-examples.rst . License: PSF License Version 2. Accessed 2026-11-26.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Python Software Foundation, unittest.mock — getting started; CPython, unittest.mock — getting started (source). PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Python Software Foundation, unittest.mock — getting started; CPython, unittest.mock — getting started (source). PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation.
Cite this
iinventedlight. 2026. "Python unittest.mock examples: patch classes and configure return values." iinventedlight Library, iil:note:76wqbhmr, version of 2026-11-26. https://iinventedlight.com/library/notes/python-unittest-mock-examples/. License: CC-BY-4.0.
@misc{iil_note_76wqbhmr,
author = {{iinventedlight}},
title = {{Python unittest.mock examples: patch classes and configure return values}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-26},
url = {https://iinventedlight.com/library/notes/python-unittest-mock-examples/},
note = {Permanent ID iil:note:76wqbhmr. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
Extra files beside this note
These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.
- python-unittest-mock-examples.schema.json (schema; CC-BY-4.0)
- python-unittest-mock-examples.table.json (table; CC-BY-4.0)
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/python-unittest-mock-examples.md.