diff options
author | Kushal Das <kushaldas@gmail.com> | 2016-06-02 17:20:16 (GMT) |
---|---|---|
committer | Kushal Das <kushaldas@gmail.com> | 2016-06-02 17:20:16 (GMT) |
commit | 9cd39a170b4e65bd17ba853e87134000523c055a (patch) | |
tree | 1cee2737612f86431f0ccbe918c5e5468bc1521e /Lib/unittest | |
parent | e2e71685f34b7252f0832d3b1247685e63b5acf8 (diff) | |
download | cpython-9cd39a170b4e65bd17ba853e87134000523c055a.zip cpython-9cd39a170b4e65bd17ba853e87134000523c055a.tar.gz cpython-9cd39a170b4e65bd17ba853e87134000523c055a.tar.bz2 |
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.
Diffstat (limited to 'Lib/unittest')
-rw-r--r-- | Lib/unittest/mock.py | 7 | ||||
-rw-r--r-- | Lib/unittest/test/testmock/testmock.py | 18 |
2 files changed, 24 insertions, 1 deletions
diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py index 123c156..be215f3 100644 --- a/Lib/unittest/mock.py +++ b/Lib/unittest/mock.py @@ -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 diff --git a/Lib/unittest/test/testmock/testmock.py b/Lib/unittest/test/testmock/testmock.py index 9910ab9..b07a7cc 100644 --- a/Lib/unittest/test/testmock/testmock.py +++ b/Lib/unittest/test/testmock/testmock.py @@ -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 |