freshcrate
Home > Frameworks > pytest-mock

pytest-mock

Thin-wrapper around the mock package for easier use with 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

Release History

VersionChangesUrgencyDate
3.15.1Imported from PyPI (3.15.1)Low4/21/2026
v3.15.1*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. Low9/16/2025
v3.15.0*2025-09-04* - Python 3.8 (EOL) is no longer supported. - [#524](https://github.com/pytest-dev/pytest-mock/pull/524): Added `spy_return_iter` to `mocker.spy`, which contains a duplicate of the return value of the spied method if it is an `Iterator`. Low9/4/2025
v3.14.1- [#503](https://github.com/pytest-dev/pytest-mock/pull/503): Python 3.14 is now officially supported. Low5/26/2025
v3.14.0- [\#415](https://github.com/pytest-dev/pytest-mock/pull/415): `MockType` and `AsyncMockType` can be imported from `pytest_mock` for type annotation purposes. - [\#420](https://github.com/pytest-dev/pytest-mock/issues/420): Fixed a regression which would cause `mocker.patch.object` to not being properly cleared between tests. Low3/21/2024
v3.13.0- [\#417](https://github.com/pytest-dev/pytest-mock/pull/417): `spy` now has `spy_return_list`, which is a list containing all the values returned by the spied function. - `pytest-mock` now requires `pytest>=6.2.5`. - [\#410](https://github.com/pytest-dev/pytest-mock/pull/410): pytest-mock\'s `setup.py` file is removed. If you relied on this file, e.g. to install pytest using `setup.py install`, please see [Why you shouldn\'t invoke setup.py directly](https://blog.ganssle.io/articlLow3/21/2024
v3.12.0- Added support for Python 3.12. - Dropped support for EOL Python 3.7. - `mocker.resetall()` now also resets mocks created by `mocker.create_autospec` ([\#390](https://github.com/pytest-dev/pytest-mock/pull/390)). Low10/19/2023
v3.11.1* Fixed introspection for failed ``assert_has_calls`` (#365). * Updated type annotations for ``mocker.patch`` and ``mocker.spy`` (#364). Low6/15/2023
v3.10.0 - Added new `mocker.stop(m)` method to stop specific `mocker.patch` or `mocker.spy` calls ([\#319](https://github.com/pytest-dev/pytest-mock/pull/319)). Low10/5/2022
v3.9.0- Expose `NonCallableMagicMock` via the `mocker` fixture ([\#318](https://github.com/pytest-dev/pytest-mock/pull/318)). Low9/28/2022
v3.8.2 - Fixed `AsyncMock` support for Python 3.7+ in `mocker.async_stub` ([\#302](https://github.com/pytest-dev/pytest-mock/pull/302)). Low7/5/2022
v3.8.1- Fix regression caused by an explicit `mock` dependency in the code ([\#298](https://github.com/pytest-dev/pytest-mock/issues/298)). Low6/24/2022
v3.8.0- Add `MockerFixture.async_mock` method. Thanks [\@PerchunPak](https://github.com/PerchunPak) for the PR ([\#296](https://github.com/pytest-dev/pytest-mock/pull/296)). Low6/24/2022
v3.7.0 - Python 3.10 now officially supported. - Dropped support for Python 3.6. Low1/28/2022
v3.6.1- Fix `mocker.resetall()` when using `mocker.spy()` ([\#237](https://github.com/pytest-dev/pytest-mock/issues/237)). Thanks [\@blaxter](https://github.com/blaxter) for the report and [\@shadycuz](https://github.com/shadycuz) for the PR.Low5/18/2021
v3.6.0- pytest-mock no longer supports Python 3.5. - Correct type annotations for `mocker.patch.object` to also include the string form. Thanks [\@plannigan](https://github.com/plannigan) for the PR ([\#235](https://github.com/pytest-dev/pytest-mock/pull/235)). - `reset_all` now supports `return_value` and `side_effect` keyword arguments. Thanks [\@alex-marty](https://github.com/alex-marty) for the PR ([\#214](https://github.com/pytest-dev/pytest-mock/pull/214)).Low5/18/2021
v3.5.1- Use `inspect.getattr_static` instead of resorting to `object.__getattribute__` magic. This should better comply with objects which implement a custom descriptor protocol. Thanks [\@yesthesoup](https://github.com/yesthesoup) for the PR ([\#224](https://github.com/pytest-dev/pytest-mock/pull/224)).Low5/18/2021
v3.5.0- Now all patch functions will emit a warning instead of raising a `ValueError` when used as a context-manager. Thanks [\@iforapsy](https://github.com/iforapsy) for the PR ([\#221](https://github.com/pytest-dev/pytest-mock/pull/221)). - Additionally, `mocker.patch.context_manager` is available when the user intends to mock a context manager (for example `threading.Lock` object), which will not emit that warning.Low5/18/2021
v3.4.0- Add [mock.seal]{.title-ref} alias to the [mocker]{.title-ref} fixture ([\#211](https://github.com/pytest-dev/pytest-mock/pull/211)). Thanks [\@coiax](https://github.com/coiax) for the PR. - Fixed spying on exceptions not covered by the `Exception` superclass ([\#215](https://github.com/pytest-dev/pytest-mock/issues/215)), like `KeyboardInterrupt` \-- PR [\#216](https://github.com/pytest-dev/pytest-mock/pull/216) by [\@webknjaz](https://github.com/sponsors/webknjaz). BefLow5/18/2021
v3.3.1* Introduce ``MockFixture`` as an alias to ``MockerFixture``. Before ``3.3.0``, the fixture class was named ``MockFixture``, but was renamed to ``MockerFixture`` to better match the ``mocker`` fixture. While not officially part of the API, it was later discovered that this broke the code of some users which already imported ``pytest_mock.MockFixture`` for type annotations, so we decided to reintroduce the name as an alias. Note however that this is just a stop gap measure, andLow8/26/2020
v3.3.0 * ``pytest-mock`` now includes inline type annotations and exposes them to user programs. The ``mocker`` fixture returns ``pytest_mock.MockerFixture``, which can be used to annotate your tests: ```python from pytest_mock import MockerFixture def test_foo(mocker: MockerFixture) -> None: ... ``` The type annotations were developed against mypy version ``0.782``, the minimum version supported at the moment. If you run into an error that you believe to be incorrect, pleLow8/22/2020
v3.2.0* [AsyncMock](https://docs.python.org/3/library/unittest.mock.html#unittest.mock.AsyncMock) is now exposed in ``mocker`` and supports provides assertion introspection similar to ``Mock`` objects. Added by [@tirkarthi](https://github.com/tirkarthi) in [#197](https://github.com/pytest-dev/pytest-mock/pull/197). Low7/11/2020
v3.1.1* Fixed performance regression caused by the ``ValueError`` raised when ``mocker`` is used as context manager (#191).Low6/5/2020
v3.1.0 * New mocker fixtures added that allow using mocking functionality in other scopes: * ``class_mocker`` * ``module_mocker`` * ``package_mocker`` * ``session_mocker`` Added by [@scorphus](https://github.com/scorphus) in [#182](https://github.com/pytest-dev/pytest-mock/pull/182). Low5/15/2020
v3.0.0* Python 2.7 and 3.4 are no longer supported. Users using pip 9 or later will install a compatible version automatically. * `mocker.spy` now also works with `async def` functions ([#179](https://github.com/pytest-dev/pytest-mock/issues/118)). Thanks @frankie567 for the PR!Low3/31/2020
v2.0.0## Breaking Changes * ``mocker.spy`` attributes for tracking returned values and raised exceptions of its spied functions are now called ``spy_return`` and ``spy_exception``, instead of reusing the existing ``MagicMock`` attributes ``return_value`` and ``side_effect``. Version ``1.13`` introduced a serious regression: after a spied function using ``mocker.spy`` raises an exception, further calls to the spy will not call the spied function, always raising the first exception iLow1/4/2020
v1.13.0* The object returned by ``mocker.spy`` now also tracks any side effect of the spied method/function. Low12/6/2019
v1.12.1* Fix error if ``mocker.patch`` is used in code where the source file is not available, for example stale ``.pyc`` files ([#169](https://github.com/pytest-dev/pytest-mock/issues/169#issuecomment-555729265)). Low11/22/2019
v1.12.0Now all patch functions also raise a `ValueError` when used as a context-manager. Thanks @AlexGascon for the PR (#168).Low11/19/2019
v1.11.2* The *pytest introspection follows* message is no longer shown if there is no pytest introspection ([#154](https://github.com/pytest-dev/pytest-mock/issues/154)). Thanks @The-Compiler for the report. * `mocker` now raises a `ValueError` when used as a context-manager. Thanks @binarymason for the PR ([#165](https://github.com/pytest-dev/pytest-mock/pull/165)). Low10/24/2019
v1.11.1 * Fix `mocker.spy` on Python 2 when used on non-function objects which implement `__call__` [#157](https://github.com/pytest-dev/pytest-mock/issues/157). Thanks [@pbasista](https://github.com/pbasista) for the report. Low10/11/2019
v1.11.0* `mocker.spy` now also tracks the return value.Low10/4/2019
v0.8.1- `mock.ANY` is now accessible from the `mocker` fixture (#17), thanks @tigarmo for the PR! - `pytest-mock` is now also available as a `wheel`. Thanks @rouge8 for the PR! Low9/29/2015
v0.7.0Thanks to @fogo, `mocker.spy` can now prey upon staticmethods and classmethods. :smile: Low7/7/2015
v0.6.0Two new auxiliary methods, `spy` and `stub`. See `README` for usage. (Thanks @fogo for complete PR!) Low6/2/2015
v0.5.0`Mock` and `MagicMock` are now accessible from the `mocker` fixture, many thanks to @marcwebbie for the complete PR! Low5/3/2015
v0.4.3- `mocker` and backward compatible `mock` fixture now return the same object (#8). Many thanks to @RonnyPfannschmidt for the PR! Low4/9/2015
v0.4.2Small fix, no longer using wheel as an alternate package since it conditionally depends on `mock` module based on Python version, as Python >= 3.3 already includes `unittest.mock` . Many thanks to @The-Compiler for letting me know and providing a PR with the fix! Low3/31/2015
v0.4.1Small release that just uses `pytest_mock` as the name of the plugin, instead of `pytest-mock`: this makes it simple to depend on this plugin explicitly using `pytest_plugins` module variable mechanism Low3/15/2015
v0.4.0Changed fixture name from `mock` into `mocker` because it conflicted with the actual `mock` module, which made using it awkward when access to both the module and the plugin were required within a test. Thanks @kmosher for request and discussion in #4. :smile: Low9/17/2014
v0.3.0- Fixed bug #2, where a patch would not be uninstalled correctly after patching the same object twice Low8/26/2014
v0.2.0Release v0.2.0Low7/19/2014
v0.1.0Release v0.1.0Low7/17/2014

Dependencies & License Audit

Loading dependencies...

Similar Packages

pytest-recordingA pytest plugin powered by VCR.py to record and replay HTTP traffic0.13.4
schemathesisProperty-based testing framework for Open API and GraphQL based apps4.15.2
pytest-subtestsunittest subTest() support and subtests fixture0.15.0
seleniumbaseA complete web automation framework for end-to-end testing.4.48.2
pytest-randomlyPytest plugin to randomly order tests and control random.seed.4.1.0