Issue #21271: Adds new keyword only parameters in reset_mock call

We now have two keyword only parameters in the reset_mock function to
selectively reset the return_value or the side_effects, or both.
This commit is contained in:
Kushal Das 2016-06-02 10:20:16 -07:00
parent e2e71685f3
commit 9cd39a170b
4 changed files with 36 additions and 3 deletions

View File

@ -364,7 +364,7 @@ the *new_callable* argument to :func:`patch`.
.. versionadded:: 3.5
.. method:: reset_mock()
.. method:: reset_mock(*, return_value=False, side_effect=False)
The reset_mock method resets all the call attributes on a mock object:
@ -376,12 +376,20 @@ the *new_callable* argument to :func:`patch`.
>>> mock.called
False
.. versionchanged:: 3.6
Added two keyword only argument to the reset_mock function.
This can be useful where you want to make a series of assertions that
reuse the same object. Note that :meth:`reset_mock` *doesn't* clear the
return value, :attr:`side_effect` or any child attributes you have
set using normal assignment. Child mocks and the return value mock
set using normal assignment by default. In case you want to reset
*return_value* or :attr:`side_effect`, then pass the corresponding
parameter as ``True``. Child mocks and the return value mock
(if any) are reset as well.
.. note:: *return_value*, and :attr:`side_effect` are keyword only
argument.
.. method:: mock_add_spec(spec, spec_set=False)

View File

@ -523,7 +523,7 @@ class NonCallableMock(Base):
side_effect = property(__get_side_effect, __set_side_effect)
def reset_mock(self, visited=None):
def reset_mock(self, visited=None,*, return_value=False, side_effect=False):
"Restore the mock object to its initial state."
if visited is None:
visited = []
@ -538,6 +538,11 @@ class NonCallableMock(Base):
self.call_args_list = _CallList()
self.method_calls = _CallList()
if return_value:
self._mock_return_value = DEFAULT
if side_effect:
self._mock_side_effect = None
for child in self._mock_children.values():
if isinstance(child, _SpecState):
continue

View File

@ -1277,6 +1277,24 @@ class MockTest(unittest.TestCase):
self.assertEqual(m.method_calls[0], c)
self.assertEqual(m.method_calls[1], i)
def test_reset_return_sideeffect(self):
m = Mock(return_value=10, side_effect=[2,3])
m.reset_mock(return_value=True, side_effect=True)
self.assertIsInstance(m.return_value, Mock)
self.assertEqual(m.side_effect, None)
def test_reset_return(self):
m = Mock(return_value=10, side_effect=[2,3])
m.reset_mock(return_value=True)
self.assertIsInstance(m.return_value, Mock)
self.assertNotEqual(m.side_effect, None)
def test_reset_sideeffect(self):
m = Mock(return_value=10, side_effect=[2,3])
m.reset_mock(side_effect=True)
self.assertEqual(m.return_value, 10)
self.assertEqual(m.side_effect, None)
def test_mock_add_spec(self):
class _One(object):
one = 1

View File

@ -36,6 +36,8 @@ Library
- Issue #27056: Optimize pickle.load() and pickle.loads(), up to 10% faster
to deserialize a lot of small objects.
- Issue #21271: New keyword only parameters in reset_mock call.
IDLE
----