# pytest-mock

> Thin-wrapper around the mock package for easier use with pytest

- **URL**: https://www.freshcrate.ai/projects/pytest-mock
- **Author**: pypi
- **Category**: Frameworks
- **Latest version**: `3.15.1` (2026-04-21)
- **License**: MIT
- **Source**: https://github.com/pytest-dev/pytest-mock/
- **Homepage**: https://pypi.org/project/pytest-mock/
- **Language**: Python
- **GitHub**: 2,025 stars, 155 forks
- **Registry**: pypi (`pytest-mock`)
- **Tags**: `mock`, `pypi`, `pytest`

## Description

===========
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:: https://coveralls.io/repos/github/pytest-dev/pytest-mock/badge.svg?branch=master
  :target: https://coveralls.io/github/pytest-dev/pytest-mock?branch=master

.. |python| image:: https://img.shields.io/pypi/pyversions/pytest-mock.svg
  :target: https://pypi.python.org/pypi/pytest-mock/

.. |black| image:: https://img.shields.io/badge/code%20style-black-000000.svg
  :target: https://github.com/ambv/black

.. |pre-commit| image:: https://results.pre-commit.ci/badge/github/pytest-dev/pytest-mock/master.svg
   :target: https://results.pre-commit.ci/latest/github/pytest-dev/pytest-mock/master

.. |docs| image:: https://readthedocs.org/projects/pytest-mock/badge/?version=latest
   :target: https://pytest-mock.readthedocs.io/en/latest/?badge=latest


`Professionally supported pytest-mock is available <https://tidelift.com/subscription/pkg/pypi-pytest_mock?utm_source=pypi-pytest-mock&utm_medium=referral&utm_campaign=readme>`_.


Documentation
=============

For full documentation, please see https://pytest-mock.readthedocs.io/en/latest.

License
=======

Distributed under the terms of the `MIT`_ license.


.. _MIT: https://github.com/pytest-dev/pytest-mock/blob/master/LICENSE

## Recent releases

| Version | Date | Urgency | Changes |
| --- | --- | --- | --- |
| `3.15.1` | 2026-04-21 | Low | Imported from PyPI (3.15.1) |
| `v3.15.1` | 2025-09-16 | Low | *2025-09-16*  -   [#529](https://github.com/pytest-dev/pytest-mock/issues/529): Fixed `itertools._tee object has no attribute error` \-- now `duplicate_iterators=True` must be passed to `mocker.spy` to duplicate iterators. |
| `v3.15.1` | 2025-09-16 | Low | *2025-09-16*  -   [#529](https://github.com/pytest-dev/pytest-mock/issues/529): Fixed `itertools._tee object has no attribute error` \-- now `duplicate_iterators=True` must be passed to `mocker.spy` to duplicate iterators. |
| `v3.15.1` | 2025-09-16 | Low | *2025-09-16*  -   [#529](https://github.com/pytest-dev/pytest-mock/issues/529): Fixed `itertools._tee object has no attribute error` \-- now `duplicate_iterators=True` must be passed to `mocker.spy` to duplicate iterators. |
| `v3.15.1` | 2025-09-16 | Low | *2025-09-16*  -   [#529](https://github.com/pytest-dev/pytest-mock/issues/529): Fixed `itertools._tee object has no attribute error` \-- now `duplicate_iterators=True` must be passed to `mocker.spy` to duplicate iterators. |
| `v3.15.1` | 2025-09-16 | Low | *2025-09-16*  -   [#529](https://github.com/pytest-dev/pytest-mock/issues/529): Fixed `itertools._tee object has no attribute error` \-- now `duplicate_iterators=True` must be passed to `mocker.spy` to duplicate iterators. |
| `v3.15.1` | 2025-09-16 | Low | *2025-09-16*  -   [#529](https://github.com/pytest-dev/pytest-mock/issues/529): Fixed `itertools._tee object has no attribute error` \-- now `duplicate_iterators=True` must be passed to `mocker.spy` to duplicate iterators. |
| `v3.15.1` | 2025-09-16 | Low | *2025-09-16*  -   [#529](https://github.com/pytest-dev/pytest-mock/issues/529): Fixed `itertools._tee object has no attribute error` \-- now `duplicate_iterators=True` must be passed to `mocker.spy` to duplicate iterators. |
| `v3.15.1` | 2025-09-16 | Low | *2025-09-16*  -   [#529](https://github.com/pytest-dev/pytest-mock/issues/529): Fixed `itertools._tee object has no attribute error` \-- now `duplicate_iterators=True` must be passed to `mocker.spy` to duplicate iterators. |
| `v3.15.1` | 2025-09-16 | Low | *2025-09-16*  -   [#529](https://github.com/pytest-dev/pytest-mock/issues/529): Fixed `itertools._tee object has no attribute error` \-- now `duplicate_iterators=True` must be passed to `mocker.spy` to duplicate iterators. |

## Citation

- HTML: https://www.freshcrate.ai/projects/pytest-mock
- Markdown: https://www.freshcrate.ai/projects/pytest-mock.md
- Dependencies JSON: https://www.freshcrate.ai/api/projects/pytest-mock/deps

_Generated by freshcrate.ai. Indexes pypi releases for AI-agent ecosystem packages._
