mock
The rolling backport of Python's unittest.mock library
Repository Health
Technical Analysis
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.
Used by 3 apps in this directory
Airbyte
Developer Tools · Data Engineering
Open-source ELT platform with 600+ connectors for moving data from any source to warehouses, lakes, and AI agents.
AutoGPT
Automation · Productivity · AI Assistants
Build, deploy, and run autonomous AI agents that automate complex multi-step workflows using a visual block-based graph editor.
Label Studio
AI Development · Data Engineering
Label Studio is an open-source, multi-type data labeling platform that lets teams annotate images, text, audio, video, and time series data with a configurable XML-based UI and export annotations in formats ready for any ML framework.