mockito
Spying framework
What it is and what it does
Mockito is a mocking and spying framework for Python unit tests, originally inspired by the Java library of the same name. It lets you stub out method calls to return specific values, verify that functions were called with expected arguments, and replace real dependencies with controlled fakes during testing. The core workflow is straightforward: use `when()` to set up a stub that returns a value when called, use the stubbed code in your test, then call `unstub()` to clean up.
Version 2 adds first-class support for async/await, properties, descriptors, and ordered verification across multiple mocks. The framework handles context managers automatically, checking that stubs were actually used unless disabled. It has no runtime dependencies beyond Python itself, making it lightweight to add to any test suite.
Use it for:
- Stub external API calls (e.g., requests library) to return fake responses without hitting the network
- Verify that your code calls a dependency with the correct arguments and number of times
- Mock filesystem operations (os.path.exists, open) to test error handling without touching disk
- Test async code by mocking async functions and verifying their call patterns
- Replace database queries with controlled mock responses to isolate unit tests from data layer
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
Mockito is a spying and stubbing framework for Python that lets you mock out dependencies, verify interactions, and stub side effects in unit tests.
Yes. Mockito is a mature, actively maintained testing framework with zero dependencies, permissive licensing, and broad Python version support. It solves a core testing need—mocking and verification—with a clean API. No known vulnerabilities and low install friction make it a straightforward addition to any test suite.
Install
mockito on PyPI
pip
pip install mockitouv
uv add mockitopoetry
poetry add mockitoInstalling mockito
Before you install
Low friction: pure Python wheel with no runtime dependencies. Actively maintained with recent release (121 days ago). Supports Python >=3.8 and targets current Python versions.
License in practice
MIT license is permissive; you can use this freely in commercial and open-source projects with minimal restrictions.
Quickstart
pip install mockito
from mockito import when, mock, unstub
import os
when(os.path).exists('/foo').thenReturn(True)
result = os.path.exists('/foo') # returns True
unstub()
Requires Python >=3.8
Verify before relying
- Whether async/await support covers all common async testing patterns
- Performance characteristics when mocking large numbers of interactions
- Compatibility with popular test runners beyond pytest
Package facts
| License | MIT (permissive) |
| Python support | supports the current Python release (>=3.8) |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | none |
| Maintenance | actively maintained — 121 days since the last release |
| First released | |
| Downloads | 1,570,213/month — #3,750 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: mockito-2.0.4-py3-none-any.whl
Tags
More Testing packages
Pluggy provides a plugin system that lets you…
permissive · top 100 on PyPI
pytestpytest is a testing framework that lets you…
permissive · top 100 on PyPI
virtualenvvirtualenv creates isolated Python environments…
permissive · top 100 on PyPI
coverageCoverage.py measures which lines of Python code…
permissive · top 1,000 on PyPI
pytest-asynciopytest-asyncio is a pytest plugin that enables…
permissive · top 1,000 on PyPI
pytest-json-ctrfA pytest plugin that generates test reports in…
permissive · top 1,000 on PyPI
flexmockflexmock creates mock, stub, spy, and fake…
permissive · top 15,000 on PyPI
stubsProvides a context-manager syntax for…
permissive · top 15,000 on PyPI
pretendPretend provides a lightweight library for…
permissive · top 15,000 on PyPI
pytest-stubA pytest plugin that replaces package, module,…
permissive · top 15,000 on PyPI
pytest-mockProvides a pytest fixture that wraps the mock…
permissive · top 1,000 on PyPI
requests-mockIntercepts and mocks HTTP requests made through…
permissive · top 1,000 on PyPI
types-mockProvides type stubs for the mock library,…
permissive · top 5,000 on PyPI
asyncmockProvides awaitable mock objects for testing…
permissive · top 15,000 on PyPI
pip-install-testA minimal stub package that verifies pip can…
permissive · top 15,000 on PyPI
pact-pythonPact Python implements consumer-driven contract…
permissive · top 5,000 on PyPI