---
source: https://iinventedlight.com/library/notes/python-mocking-monkeypatch-and-mock.md
title: "Mocking in Python tests: pytest monkeypatch vs unittest.mock patch"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "MIT (pytest-dev/pytest (GitHub)); PSF-2.0 (Python Software Foundation (docs.python.org)); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "Summary by iinventedlight (AI-assisted). Sources: pytest-dev/pytest (GitHub), How to monkeypatch/mock modules and environments (pytest docs), MIT (https://raw.githubusercontent.com/pytest-dev/pytest/main/LICENSE). Copyright (c) 2004 Holger Krekel and others; Python Software Foundation (docs.python.org), unittest.mock — mock object library, PSF-2.0 (https://docs.python.org/3/library/unittest.mock.html (page footer: licensed under the Python Software Foundation License Version 2; code examples Zero Clause BSD)). Copyright 2001 Python Software Foundation."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-16
status: published
approved_by: "Cole Withington, 2026-11-20 13:14 PT: \"Publish the full batch and rewrite the 6 live notes\""
checked_on: 2026-11-16
as_of: 2026-11-16
confidence: medium
untested: "Not reproduced (2026-11-16). Docs read; no API call was made for this note."
category: coding-references
topic_area: "code-python: testing and HTTP"
tags: [python, pytest, mock, monkeypatch, testing]
client: Librarian
sources:
  - id: S1
    title: "How to monkeypatch/mock modules and environments (pytest docs)"
    publisher: pytest-dev/pytest (GitHub)
    url: https://github.com/pytest-dev/pytest/blob/main/doc/en/how-to/monkeypatch.rst
    accessed: 2026-11-16
    license: "MIT"
  - id: S2
    title: "unittest.mock — mock object library"
    publisher: Python Software Foundation (docs.python.org)
    url: https://docs.python.org/3/library/unittest.mock.html
    accessed: 2026-11-16
    license: "PSF-2.0"
---
# Mocking in Python tests: pytest monkeypatch vs unittest.mock patch
**Answer:** The `monkeypatch` fixture helps with patching and mocking in tests; modifications are undone when the requesting test function or fixture finishes. The standard library's `unittest.mock` gives `Mock`, `MagicMock` and a `patch()` decorator.

## monkeypatch

- Methods: `setattr`, `delattr`, `setitem`, `delitem`, `setenv`, `delenv`, `syspath_prepend`, `chdir` and `context()`.
- `raising` decides whether a `KeyError` or `AttributeError` is raised when the target does not exist.

## unittest.mock

- The key rule: patch an object where it is used (or where it is looked up), for example `@patch('b.SomeClass')` when module `b` imported it.
- Auto-speccing (`autospec` or `create_autospec()`) gives mocks the same attributes, methods and call signatures as the real objects.
- Check calls with `assert_called_once_with(...)`; `patch.dict()` sets dictionary values just during a scope.

## See also

- [pytest-fixtures](https://iinventedlight.com/library/notes/pytest-fixtures/)

## Claims

- id: C1
  claim: monkeypatch changes are undone.
  source: S1
  evidence: "All modifications will be undone after the requesting test function or fixture has finished."

- id: C2
  claim: Patch where the object is looked up.
  source: S2
  evidence: "patch out SomeClass where it is used (or where it is looked up)"

- id: C3
  claim: Autospec matches call signatures.
  source: S2
  evidence: "have the same call signature"

## Sources

- [S1] How to monkeypatch/mock modules and environments (pytest docs). pytest-dev/pytest (GitHub). https://github.com/pytest-dev/pytest/blob/main/doc/en/how-to/monkeypatch.rst . License: MIT. Accessed 2026-11-16.

- [S2] unittest.mock — mock object library. Python Software Foundation (docs.python.org). https://docs.python.org/3/library/unittest.mock.html . License: PSF-2.0. Accessed 2026-11-16.

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: pytest-dev/pytest (GitHub), How to monkeypatch/mock modules and environments (pytest docs), MIT (https://raw.githubusercontent.com/pytest-dev/pytest/main/LICENSE). Copyright (c) 2004 Holger Krekel and others; Python Software Foundation (docs.python.org), unittest.mock — mock object library, PSF-2.0 (https://docs.python.org/3/library/unittest.mock.html (page footer: licensed under the Python Software Foundation License Version 2; code examples Zero Clause BSD)). Copyright 2001 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).
