---
source: https://iinventedlight.com/library/notes/python-unittest-mock-examples.md
title: "Python unittest.mock examples: patch classes and configure return values"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "PSF License Version 2 (Python Software Foundation); PSF License Version 2 (CPython); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "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."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-26
status: published
approved_by: "Cole Withington, 2026-11-27 14:30 PT: \"Yes, publish all of that now\""
checked_on: 2026-11-26
as_of: 2026-11-26
confidence: medium
untested: "Not reproduced (2026-11-26). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-python: python-unittest-mock-examples"
tags: [python, unittest, mock, patch, testing]
client: Librarian
sources:
  - id: S1
    title: "unittest.mock — getting started"
    publisher: Python Software Foundation
    url: https://docs.python.org/3/library/unittest.mock-examples.html
    accessed: 2026-11-26
    license: "PSF License Version 2"
  - id: S2
    title: "unittest.mock — getting started (source)"
    publisher: CPython
    url: https://raw.githubusercontent.com/python/cpython/main/Doc/library/unittest.mock-examples.rst
    accessed: 2026-11-26
    license: "PSF License Version 2"
---
# 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

- [python-unittest](https://iinventedlight.com/library/notes/python-unittest/)
- [python-mocking-monkeypatch-and-mock](https://iinventedlight.com/library/notes/python-mocking-monkeypatch-and-mock/)
- [python-doctest](https://iinventedlight.com/library/notes/python-doctest/)

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

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
