From b74cb3011ebb5884646f702f68b62d4590b4ca6f Mon Sep 17 00:00:00 2001 From: Levi Blaney Date: Wed, 5 May 2021 11:16:50 -0400 Subject: [PATCH 1/3] modify reset_all to work with patch object --- src/pytest_mock/plugin.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/pytest_mock/plugin.py b/src/pytest_mock/plugin.py index 041926e..05803c4 100644 --- a/src/pytest_mock/plugin.py +++ b/src/pytest_mock/plugin.py @@ -70,7 +70,12 @@ def resetall( :param bool side_effect: Reset the side_effect of mocks. """ for m in self._mocks: - m.reset_mock(return_value=return_value, side_effect=side_effect) + + if isinstance(m, self.Mock): + m.reset_mock(return_value=return_value, side_effect=side_effect) + continue + + m.reset_mock() def stopall(self) -> None: """ From 2b0ddc7387d9854a28dd2e6f5cf0bcb56a69a04b Mon Sep 17 00:00:00 2001 From: Levi Blaney Date: Thu, 6 May 2021 03:42:31 -0400 Subject: [PATCH 2/3] modify test case to trigger resetall failure --- tests/test_pytest_mock.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/test_pytest_mock.py b/tests/test_pytest_mock.py index b3e374e..8913f90 100644 --- a/tests/test_pytest_mock.py +++ b/tests/test_pytest_mock.py @@ -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. + mocker.resetall() + with pytest.raises(ValueError): Foo().bar(0) assert spy.spy_return is None From 4c3cfa6480f601c86a99f916981e4f6792a1d868 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Thu, 6 May 2021 16:11:39 -0300 Subject: [PATCH 3/3] Add CHANGELOG and support AsyncMock --- CHANGELOG.rst | 9 +++++++++ src/pytest_mock/plugin.py | 16 +++++++++++----- tests/test_pytest_mock.py | 2 +- 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index f263d8d..6cb2e00 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -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) ------------------ diff --git a/src/pytest_mock/plugin.py b/src/pytest_mock/plugin.py index 05803c4..088e5ce 100644 --- a/src/pytest_mock/plugin.py +++ b/src/pytest_mock/plugin.py @@ -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 @@ -69,13 +70,18 @@ def resetall( :param bool return_value: Reset the return_value of mocks. :param bool side_effect: Reset the side_effect of mocks. """ - for m in self._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,) - if isinstance(m, self.Mock): + for m in self._mocks: + # See issue #237. + if isinstance(m, supports_reset_mock_with_args): m.reset_mock(return_value=return_value, side_effect=side_effect) - continue - - m.reset_mock() + else: + m.reset_mock() def stopall(self) -> None: """ diff --git a/tests/test_pytest_mock.py b/tests/test_pytest_mock.py index 8913f90..6d8feda 100644 --- a/tests/test_pytest_mock.py +++ b/tests/test_pytest_mock.py @@ -310,7 +310,7 @@ def bar(self, x): assert spy.spy_return == 30 assert spy.spy_exception is None - # Testing spy can still be reset. + # Testing spy can still be reset (#237). mocker.resetall() with pytest.raises(ValueError):