Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

modify resetall to work with patch object #241

Merged
merged 3 commits into from
May 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
3.6.1 (UNRELEASED)
------------------

* Fix ``mocker.resetall()`` when using ``mocker.spy()`` (`#237`_). Thanks `@blaxter`_ for the report and `@shadycuz`_ for the PR.

.. _@blaxter: https://github.com/blaxter
.. _@shadycuz: https://github.com/shadycuz
.. _#237: https://github.com/pytest-dev/pytest-mock/issues/237

3.6.0 (2021-04-24)
------------------

Expand Down
13 changes: 12 additions & 1 deletion src/pytest_mock/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from typing import Optional
from typing import overload
from typing import Tuple
from typing import Type
from typing import TypeVar
from typing import Union

Expand Down Expand Up @@ -69,8 +70,18 @@ def resetall(
:param bool return_value: Reset the return_value of mocks.
:param bool side_effect: Reset the side_effect of mocks.
"""
supports_reset_mock_with_args: Tuple[Type[Any], ...]
if hasattr(self, "AsyncMock"):
supports_reset_mock_with_args = (self.Mock, self.AsyncMock)
else:
supports_reset_mock_with_args = (self.Mock,)

for m in self._mocks:
m.reset_mock(return_value=return_value, side_effect=side_effect)
# See issue #237.
if isinstance(m, supports_reset_mock_with_args):
m.reset_mock(return_value=return_value, side_effect=side_effect)
else:
m.reset_mock()

def stopall(self) -> None:
"""
Expand Down
3 changes: 3 additions & 0 deletions tests/test_pytest_mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,9 @@ def bar(self, x):
assert spy.spy_return == 30
assert spy.spy_exception is None

# Testing spy can still be reset (#237).
mocker.resetall()

with pytest.raises(ValueError):
Foo().bar(0)
assert spy.spy_return is None
Expand Down