pytest-mock
Thin-wrapper around the mock package for easier use with pytest
Install
pytest-mock on PyPI
pip
pip install pytest-mockuv
uv add pytest-mockpoetry
poetry add pytest-mockPackage facts
| License | MIT (permissive) |
| Python support | supports the current Python release (>=3.9) |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | 1 — pytest |
| Maintenance | actively maintained — 331 days since the last release |
| Last repo commit | |
| First released | |
| Popularity | one of the top 1,000 most-downloaded packages on PyPI (30-day window, as of 2026-08-13) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-13) |
Evidence: pytest_mock-3.15.1-py3-none-any.whl
Keywords: pytest, mock
About pytest-mock
from the package's own PyPI description — quoted content, verbatim
=========== pytest-mock ===========
This plugin provides a mocker fixture which is a thin-wrapper around the patching API
provided by the mock package <http://pypi.python.org/pypi/mock>_:
.. code-block:: python
import os
class UnixFS:
@staticmethod
def rm(filename):
os.remove(filename)
def test_unix_fs(mocker):
mocker.patch('os.remove')
UnixFS.rm('file')
os.remove.assert_called_once_with('file')
Besides undoing the mocking automatically after the end of the test, it also provides other
nice utilities such as spy and stub, and uses pytest introspection when
comparing calls.
|python| |version| |anaconda| |docs| |ci| |coverage| |black| |pre-commit|
.. |version| image:: http://img.shields.io/pypi/v/pytest-mock.svg :target: https://pypi.python.org/pypi/pytest-mock
.. |anaconda| image:: https://img.shields.io/conda/vn/conda-forge/pytest-mock.svg :target: https://anaconda.org/conda-forge/pytest-mock
.. |ci| image:: https://github.com/pytest-dev/pytest-mock/workflows/test/badge.svg :target: https://github.com/pytest-dev/pytest-mock/actions
.. |coverage| image::...
Read as markdown · JSON record · Source repository · Homepage · Docs
AI interpretation — verify before relying
AI-generated interpretation of the package facts above; every digit, version, license, or vulnerability id it cites is grounded in the facts already shown on this page
pytest-mock provides a `mocker` fixture that wraps the mock package's patching API for pytest, automatically cleaning up patches after each test and offering utilities like spy and stub.
Low friction: pure Python wheel with only pytest as a runtime dependency. Actively maintained with recent releases and 2037 GitHub stars; last commit 2026-08-13.
MIT license (permissive) imposes no significant restrictions; you may use, modify, and distribute freely with minimal attribution requirements.
Usage
pip install pytest-mock
import os
from unittest.mock import call
def test_example(mocker):
mocker.patch('os.remove')
os.remove('file')
os.remove.assert_called_once_with('file')
Requires Python >=3.9 and pytest to be installed.
Verdict: pytest-mock is a stable, widely-used testing utility (top 1000 PyPI, 2037 stars, active maintenance) with no known vulnerabilities, low install friction, and permissive licensing. It is a safe, standard choice for mock-based testing in pytest projects.
Needs verification
- Whether the mocker fixture's automatic cleanup behavior has any edge cases or performance implications for large test suites.
- How the spy and stub utilities compare in capability or performance to direct mock.patch usage.
Similar packages
permissive · top 1,000 on PyPI
respxpermissive · top 1,000 on PyPI
requests-mockpermissive · top 1,000 on PyPI
pytestpermissive · top 100 on PyPI
pytest-metadatacopyleft · top 1,000 on PyPI
pytest-timeoutpermissive · top 1,000 on PyPI
pytest-httpxpermissive · top 1,000 on PyPI
vcrpypermissive · top 1,000 on PyPI
responsespermissive · top 1,000 on PyPI
pypermissive · top 1,000 on PyPI