How to Install pytest-mock in Python
Thin-wrapper around the mock package for easier use with pytest
pip install pytest-mock
What is pytest-mock?
Thin-wrapper around the mock package for easier use with pytest
This plugin provides a `` fixture which is a thin-wrapper around the patching API provided by the :
@staticmethod def rm(filename): os.remove(filename)
Besides undoing the mocking automatically after the end of the test, it also provides other nice utilities such as ``, and uses pytest introspection when comparing calls.
Quick Start
Minimal example to get started with pytest-mock:
import pytest_mock
print(pytest_mock.__version__)
Installation
pip (standard)
pip install pytest-mock
Virtual environment (recommended)
python -m venv venv
source venv/bin/activate # Windows: venv\Scripts\activate
pip install pytest-mock
pip3
pip3 install pytest-mock
conda
conda install -c conda-forge pytest-mock
Poetry
poetry add pytest-mock
Dependencies
Installing pytest-mock will also install these packages:
Verify the Installation
After installing, confirm the package is available:
python -c "import pytest_mock; print(pytest_mock.__version__)"
If this prints a version number, installation succeeded. If you see a ModuleNotFoundError, see the errors section below.
Installation Errors
Common errors when installing pytest-mock with pip.
ModuleNotFoundError: No module named 'pytest_mock'
Cause: The package is not installed in the current Python environment.
Fix: Run pip install pytest-mock. If using a virtual environment, ensure it is activated first.
ModuleNotFoundError: No module named 'pytest_mock' (installed but still failing)
Cause: pip installed the package into a different Python than the one running your script.
Fix: Use python -m pip install pytest-mock to install into the interpreter you are running.
ImportError: cannot import name 'X' from 'pytest_mock'
Cause: The function or class does not exist in the installed version.
Fix: Check the version with pip show pytest-mock and upgrade with pip install --upgrade pytest-mock.
pip: command not found
Cause: pip is not in PATH or Python was not added to PATH during installation.
Fix: Try python -m pip install pytest-mock. On macOS/Linux try pip3.
PermissionError: [Errno 13] Permission denied
Cause: No write access to the system Python package directory.
Fix: Use a virtual environment, or add --user: pip install --user pytest-mock
SSL: CERTIFICATE_VERIFY_FAILED
Cause: pip cannot verify PyPI's SSL certificate — common behind corporate proxies.
Fix: Try: pip install --trusted-host pypi.org --trusted-host files.pythonhosted.org pytest-mock
Recent Releases
| Version | Released |
|---|---|
3.15.1 latest |
2025-09-16 |
3.15.0 |
2025-09-04 |
3.14.1 |
2025-05-26 |
3.13.0 |
2024-03-21 |
3.14.0 |
2024-03-21 |
Manage pytest-mock
Upgrade to latest version
pip install --upgrade pytest-mock
Install a specific version
pip install pytest-mock==3.15.1
Uninstall
pip uninstall pytest-mock
Check what is installed
pip show pytest-mock