mock

The rolling backport of Python's unittest.mock library

Library
PyPI
v5.2.0
538stars
BSD-2-Clause

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
40/100Fair
Development Activity0
Maintenance0
Community80
Maturity60
Momentum20

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
83/100Excellent
Architecture80
Code Quality88
Innovation72
Learning Curve90

mock is the standalone, rolling backport of Python’s standard-library unittest.mock. It provides the same Mock, MagicMock, patch(), and sentinel tooling for replacing parts of your system under test with mock objects and asserting how they were used, but delivered as an independently versioned PyPI package.

Installing mock from PyPI gives older Python versions access to the latest mocking features and lets projects pin a specific mock version independent of their Python runtime. Its API is identical to the standard library module, so the same patterns work whether you import from unittest.mock or from mock.

What You Get

  • Mock and MagicMock classes for flexible, auto-speccing test doubles
  • patch(), patch.object(), and patch.dict() for temporarily replacing targets
  • Call assertion helpers like assert_called_once_with and call tracking
  • sentinel and ANY helpers for expressive test setup and matching
  • A version-pinnable package independent of the Python runtime’s bundled module

Common Use Cases

  • Replacing network or database calls with mocks during unit tests
  • Patching module-level functions or objects for the duration of a test
  • Asserting that dependencies were called with the expected arguments
  • Using newer mocking features on Python versions with an older bundled module

Under The Hood

Architecture — The mock package is a thin distribution wrapper: mock/mock.py contains the mocking implementation (the Mock/MagicMock hierarchy, the _patch machinery, call tracking, and helpers), mock/backports.py supplies compatibility shims, and mock/init.py re-exports the public names. A backport.py script at the repo root syncs the implementation from CPython’s source, keeping the package a faithful rolling backport.

Tech Stack — Pure Python distributed under BSD-2-Clause, packaged with setuptools via setup.py/setup.cfg. It has no hard runtime dependencies beyond the standard library and mirrors CPython’s unittest.mock exactly.

Code Quality — The repository carries the full mock/tests suite inherited from CPython, a CHANGELOG.rst tracking each backport, and Sphinx docs. Because the code is generated by syncing from CPython, quality and behavior track the upstream standard library.

API Design — The API is the well-established unittest.mock surface: patch() as a decorator or context manager, MagicMock for auto-configuring attributes, and readable assert_called_* helpers. It requires essentially no learning beyond the standard library, since the import path is the only difference.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search