← All notes

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

Sources

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

Permanent ID: iil:note:76wqbhmr

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.},
}

Markdown

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.

All artifact files (JSON index)

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.