From b4d91e04f2b02a30dfa540dc319596eed4f457ea Mon Sep 17 00:00:00 2001 From: Levi Date: Fri, 7 May 2021 00:46:36 +0530 Subject: [PATCH] modify resetall to work with patch object (#241) Co-authored-by: Bruno Oliveira --- CHANGELOG.rst | 9 +++++++++ src/pytest_mock/plugin.py | 13 ++++++++++++- tests/test_pytest_mock.py | 3 +++ 3 files changed, 24 insertions(+), 1 deletion(-) 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 041926e..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,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: """ diff --git a/tests/test_pytest_mock.py b/tests/test_pytest_mock.py index b3e374e..6d8feda 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 (#237). + mocker.resetall() + with pytest.raises(ValueError): Foo().bar(0) assert spy.spy_return is None