Changelog¶
- hide-toc:
Releases¶
3.14.1 (2025-08-26)¶
#503: Python 3.14 is now officially supported.
3.14.0 (2024-03-21)¶
3.13.0 (2024-03-21)¶
#417:
spynow hasspy_return_list, which is a list containing all the values returned by the spied function.pytest-mocknow requirespytest>=6.2.5.#410: pytest-mock’s
setup.pyfile is removed. If you relied on this file, e.g. to install pytest usingsetup.py install, please see Why you shouldn’t invoke setup.py directly for alternatives.
3.12.0 (2023-10-19)¶
Added support for Python 3.12.
Dropped support for EOL Python 3.7.
mocker.resetall()now also resets mocks created bymocker.create_autospec(#390).
3.11.1 (2023-06-15)¶
(This release source code is identical to 3.11.0 except a small internal fix to deployment/CI)
3.11.0 (2023-06-15)¶
3.10.0 (2022-10-05)¶
Added new
mocker.stop(m)method to stop specificmocker.patchormocker.spycalls (#319).
3.9.0 (2022-09-28)¶
Expose
NonCallableMagicMockvia themockerfixture (#318).
3.8.2 (2022-07-05)¶
Fixed
AsyncMocksupport for Python 3.7+ inmocker.async_stub(#302).
3.8.1 (2022-06-24)¶
Fixed regression caused by an explicit
mockdependency in the code (#298).
3.8.0 (2022-06-24)¶
Add
MockerFixture.async_mockmethod. Thanks @PerchunPak for the PR (#296).
3.7.0 (2022-01-28)¶
Python 3.10 now officially supported.
Dropped support for Python 3.6.
3.6.1 (2021-05-06)¶
3.6.0 (2021-04-24)¶
pytest-mock no longer supports Python 3.5.
Correct type annotations for
mocker.patch.objectto also include the string form. Thanks @plannigan for the PR (#235).reset_allnow supportsreturn_valueandside_effectkeyword arguments. Thanks @alex-marty for the PR (#214).
3.5.1 (2021-01-10)¶
Use
inspect.getattr_staticinstead of resorting toobject.__getattribute__magic. This should better comply with objects which implement a custom descriptor protocol. Thanks @yesthesoup for the PR (#224).
3.5.0 (2021-01-04)¶
Now all patch functions will emit a warning instead of raising a
ValueErrorwhen used as a context-manager. Thanks @iforapsy for the PR (#221).Additionally,
mocker.patch.context_manageris available when the user intends to mock a context manager (for examplethreading.Lockobject), which will not emit that warning.
3.4.0 (2020-12-15)¶
Add mock.seal alias to the mocker fixture (#211). Thanks @coiax for the PR.
Fixed spying on exceptions not covered by the
Exceptionsuperclass (#215), likeKeyboardInterrupt– PR #216 by @webknjaz.Before the fix, both
spy_returnandspy_exceptionwere always assigned toNonewhenever such an exception happened. And after this fix,spy_exceptionis set to a correct value of an exception that has actually happened.
3.3.1 (2020-08-24)¶
Introduce
MockFixtureas an alias toMockerFixture.Before
3.3.0, the fixture class was namedMockFixture, but was renamed toMockerFixtureto better match themockerfixture. While not officially part of the API, it was later discovered that this broke the code of some users which already importedpytest_mock.MockFixturefor type annotations, so we decided to reintroduce the name as an alias.Note however that this is just a stop gap measure, and new code should use
MockerFixturefor type annotations.Improved typing for
MockerFixture.patch(#201). Thanks @srittau for the PR.
3.3.0 (2020-08-21)¶
pytest-mocknow includes inline type annotations and exposes them to user programs. Themockerfixture returnspytest_mock.MockerFixture, which can be used to annotate your tests: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, please open an issue.Many thanks to @staticdev for providing the initial patch (#199).
3.2.0 (2020-07-11)¶
AsyncMock is now exposed in
mockerand supports provides assertion introspection similar toMockobjects.Added by @tirkarthi in #197.
3.1.1 (2020-05-31)¶
Fixed performance regression caused by the
ValueErrorraised whenmockeris used as context manager (#191).
3.1.0 (2020-04-18)¶
3.0.0 (2020-03-31)¶
Python 2.7 and 3.4 are no longer supported. Users using
pip 9or later will install a compatible version automatically.mocker.spynow also works withasync deffunctions (#179). Thanks @frankie567 for the PR!
2.0.0 (2020-01-04)¶
Breaking Changes¶
mocker.spyattributes for tracking returned values and raised exceptions of its spied functions are now calledspy_returnandspy_exception, instead of reusing the existingMagicMockattributesreturn_valueandside_effect.Version
1.13introduced a serious regression: after a spied function usingmocker.spyraises an exception, further calls to the spy will not call the spied function, always raising the first exception instead: assigning toside_effectcausesunittest.mockto behave this way (#175).The deprecated
mockalias to themockerfixture has finally been removed.
1.13.0 (2019-12-05)¶
The object returned by
mocker.spynow also tracks any side effect of the spied method/function.
1.12.1 (2019-11-20)¶
Fix error if
mocker.patchis used in code where the source file is not available, for example stale.pycfiles (#169).
1.12.0 (2019-11-19)¶
Now all patch functions also raise a
ValueErrorwhen used as a context-manager. Thanks @AlexGascon for the PR (#168).
1.11.2 (2019-10-19)¶
The pytest introspection follows message is no longer shown if there is no pytest introspection (#154). Thanks @The-Compiler for the report.
mockernow raises aValueErrorwhen used as a context-manager. Thanks @binarymason for the PR (#165).
1.11.1 (2019-10-04)¶
1.11.0¶
The object returned by
mocker.spynow also tracks the return value of the spied method/function.
1.10.4¶
Fix plugin when ‘terminal’ plugin is disabled
1.10.3¶
1.10.2¶
1.10.1¶
Fix broken links and update README. Also the code is now formatted using black.
1.10.0¶
1.9.0¶
1.8.0¶
1.7.1¶
1.7.0¶
Incompatible change
pytest-mockno longer supports Python 2.6 and Python 3.3, following the lead ofpytestand other projects in the community. Thanks @hugovk for the PR (#96).
Packaging
Internal
Some tests in
pytest-mock’s suite are skipped if assertion rewriting is disabled (#102).
1.6.3¶
Fix
UnicodeDecodeErrorduring assert introspection inassert_called_within Python 2. Thanks @AndreasHogstrom for the report (#91).
1.6.2¶
Provide source package in
tar.gzformat and remove obsoleteMANIFEST.in.
1.6.1¶
1.6.0¶
The original assertions raised by the various
Mock.assert_*methods now appear in the failure message, in addition to the message obtained from pytest introspection. Thanks @quodlibetor for the initial patch (#79).
1.5.0¶
1.4.0¶
New configuration variable,
mock_use_standalone_module(defaults toFalse). This forces the plugin to importmockinstead ofunittest.mockon Python 3. This is useful to import a newer version than the one available in the Python distribution.Previously the plugin would first try to import
mockand fallback tounittest.mockin case of anImportError, but this behavior has been removed because it could hide hard to debug import errors (#68).Now
mock(Python 2) andunittest.mock(Python 3) are lazy-loaded to make it possible to implement the newmock_use_standlone_moduleconfiguration option. As a consequence of this the undocumentedpytest_mock.mock_modulevariable, which pointed to the actual mock module being used by the plugin, has been removed.DEFAULT is now available from the
mockerfixture.
1.3.0¶
Add support for Python 3.6. Thanks @hackebrot for the report (#59).
mock.mock_openis now aliased asmocker.mock_openfor convenience. Thanks @pokidovea for the PR (#66).
1.2¶
Try to import
mockfirst instead ofunittest.mock. This gives the user flexibility to install a newermockversion from PyPI instead of using the one available in the Python distribution. Thanks @wcooley for the PR (#54).mock.sentinelis now aliased asmocker.sentinelfor convenience. Thanks @kjwilcox for the PR (#56).
1.1¶
From this version onward,
pytest-mockis licensed under the MIT license (#45).Now the plugin also adds introspection information on differing call arguments when calling helper methods such as
assert_called_once_with. The extra introspection information is similar to pytest’s and can be disabled with themock_traceback_monkeypatchoption. Thanks @asfaltboy for the PR (#36).mocker.stub()now allows passing in the name for the constructedMockobject instead of having to set it using the internal_mock_nameattribute directly. This is useful for debugging as the name is used in the mock’sreprstring as well as related assertion failure messages. Thanks @jurko-gospodnetic for the PR (#40).Monkey patching
mockmodule for friendlier tracebacks is automatically disabled with the--tb=nativeoption. The underlying mechanism used to suppress traceback entries frommockmodule does not work with that option anyway plus it generates confusing messages on Python 3.5 due to exception chaining (#44). Thanks @blueyed for the report.mock.callis now aliased asmocker.callfor convenience. Thanks @jhermann for the PR (#49).
1.0¶
0.11.0¶
PropertyMock is now accessible from
mocker. Thanks @satyrius for the PR (#32).Fix regression using one of the
assert_*methods in patched functions which receive a parameter namedmethod. Thanks @sagarchalise for the report (#31).
0.10.1¶
Fix regression in frozen tests due to
distutilsimport dependency. Thanks @The-Compiler for the report (#29).Fix regression when using
pytest-mockwithpytest-2.7.X. Thanks @akscram for the report (#28).
0.10¶
0.9.0¶
New
mocker.resetallfunction, which callsreset_mock()in all mocked objects up to that point. Thanks to @mathrick for the PR!
0.8.1¶
pytest-mockis now also available as a wheel. Thanks @rouge8 for the PR!
0.8.0¶
0.7.0¶
Thanks to @fogo, mocker.spy can now prey upon staticmethods and classmethods. :smile:
0.6.0¶
Two new auxiliary methods,
spyandstub. SeeREADMEfor usage. (Thanks @fogo for complete PR!)
0.5.0¶
MockandMagicMockare now accessible from themockerfixture, many thanks to @marcwebbie for the complete PR!
0.4.3¶
mockerfixture now returns the same object (#8). Many thanks to @RonnyPfannschmidt for the PR!
0.4.2¶
Small 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!
0.4.1¶
Small release that just uses
pytest_mockas the name of the plugin, instead ofpytest-mock: this makes it simple to depend on this plugin explicitly usingpytest_pluginsmodule variable mechanism.
0.4.0¶
0.3.0¶
Fixed bug #2, where a patch would not be uninstalled correctly after patching the same object twice.
0.2.0¶
Added
patch.dictsupport.
0.1.0¶
First release.