Skip to content

Commit b4d91e0

Browse files
shadycuznicoddemus
andauthored
modify resetall to work with patch object (#241)
Co-authored-by: Bruno Oliveira <[email protected]>
1 parent c387818 commit b4d91e0

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

CHANGELOG.rst

+9
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
3.6.1 (UNRELEASED)
2+
------------------
3+
4+
* Fix ``mocker.resetall()`` when using ``mocker.spy()`` (`#237`_). Thanks `@blaxter`_ for the report and `@shadycuz`_ for the PR.
5+
6+
.. _@blaxter: https://github.com/blaxter
7+
.. _@shadycuz: https://github.com/shadycuz
8+
.. _#237: https://github.com/pytest-dev/pytest-mock/issues/237
9+
110
3.6.0 (2021-04-24)
211
------------------
312

src/pytest_mock/plugin.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from typing import Optional
1717
from typing import overload
1818
from typing import Tuple
19+
from typing import Type
1920
from typing import TypeVar
2021
from typing import Union
2122

@@ -69,8 +70,18 @@ def resetall(
6970
:param bool return_value: Reset the return_value of mocks.
7071
:param bool side_effect: Reset the side_effect of mocks.
7172
"""
73+
supports_reset_mock_with_args: Tuple[Type[Any], ...]
74+
if hasattr(self, "AsyncMock"):
75+
supports_reset_mock_with_args = (self.Mock, self.AsyncMock)
76+
else:
77+
supports_reset_mock_with_args = (self.Mock,)
78+
7279
for m in self._mocks:
73-
m.reset_mock(return_value=return_value, side_effect=side_effect)
80+
# See issue #237.
81+
if isinstance(m, supports_reset_mock_with_args):
82+
m.reset_mock(return_value=return_value, side_effect=side_effect)
83+
else:
84+
m.reset_mock()
7485

7586
def stopall(self) -> None:
7687
"""

tests/test_pytest_mock.py

+3
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,9 @@ def bar(self, x):
310310
assert spy.spy_return == 30
311311
assert spy.spy_exception is None
312312

313+
# Testing spy can still be reset (#237).
314+
mocker.resetall()
315+
313316
with pytest.raises(ValueError):
314317
Foo().bar(0)
315318
assert spy.spy_return is None

0 commit comments

Comments
 (0)