Skip to content

Commit

Permalink
modify resetall to work with patch object (#241)
Browse files Browse the repository at this point in the history
Co-authored-by: Bruno Oliveira <nicoddemus@gmail.com>
  • Loading branch information
shadycuz and nicoddemus committed May 6, 2021
1 parent c387818 commit b4d91e0
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
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

0 comments on commit b4d91e0

Please sign in to comment.