diff -r bbfbde6ee9d0 -r b609f807021b Doc/library/unittest.mock.rst --- a/Doc/library/unittest.mock.rst Thu Feb 18 17:34:32 2016 +0200 +++ b/Doc/library/unittest.mock.rst Fri Feb 19 09:29:51 2016 +1100 @@ -259,6 +259,34 @@ used to set attributes on the mock after it is created. See the :meth:`configure_mock` method for details. + .. method:: assert_called(*args, **kwargs) + + Assert that the mock was called. + + >>> mock = Mock() + >>> mock.method() + + >>> mock.method.assert_called() + + .. versionadded:: 3.6 + + .. method:: assert_called_once(*args, **kwargs) + + Assert that the mock was called exactly once. + + >>> mock = Mock() + >>> mock.method() + + >>> mock.method.assert_called_once() + >>> mock.method() + + >>> mock.method.assert_called_once() + Traceback (most recent call last): + ... + AssertionError: Expected 'method' to have been called once. Called 2 times. + + .. versionadded:: 3.6 + .. method:: assert_called_with(*args, **kwargs) diff -r bbfbde6ee9d0 -r b609f807021b Doc/whatsnew/3.6.rst --- a/Doc/whatsnew/3.6.rst Thu Feb 18 17:34:32 2016 +0200 +++ b/Doc/whatsnew/3.6.rst Fri Feb 19 09:29:51 2016 +1100 @@ -161,6 +161,18 @@ Stéphane Wirtel in :issue:`25485`). +unittest.mock +------------- + +The :class:`~unittest.mock.Mock` class has the following improvements: + +* Two new methods, :meth:`Mock.assert_called() + ` and :meth:`Mock.assert_called_once() + ` to check if the mock object + was called. + (Contributed by Amit Saha in :issue:`26323`.) + + urllib.robotparser ------------------ diff -r bbfbde6ee9d0 -r b609f807021b Lib/unittest/mock.py --- a/Lib/unittest/mock.py Thu Feb 18 17:34:32 2016 +0200 +++ b/Lib/unittest/mock.py Fri Feb 19 09:29:51 2016 +1100 @@ -772,6 +772,24 @@ (self._mock_name or 'mock', self.call_count)) raise AssertionError(msg) + def assert_called(_mock_self): + """assert that the mock was called at least once + """ + self = _mock_self + if self.call_count == 0: + msg = ("Expected '%s' to have been called." % + self._mock_name or 'mock') + raise AssertionError(msg) + + def assert_called_once(_mock_self): + """assert that the mock was called only once. + """ + self = _mock_self + if not self.call_count == 1: + msg = ("Expected '%s' to have been called once. Called %s times." % + (self._mock_name or 'mock', self.call_count)) + raise AssertionError(msg) + def assert_called_with(_mock_self, *args, **kwargs): """assert that the mock was called with the specified arguments. diff -r bbfbde6ee9d0 -r b609f807021b Lib/unittest/test/testmock/testmock.py --- a/Lib/unittest/test/testmock/testmock.py Thu Feb 18 17:34:32 2016 +0200 +++ b/Lib/unittest/test/testmock/testmock.py Fri Feb 19 09:29:51 2016 +1100 @@ -1222,6 +1222,28 @@ with self.assertRaises(AssertionError): m.hello.assert_not_called() + def test_assert_called(self): + m = Mock() + with self.assertRaises(AssertionError): + m.hello.assert_called() + m.hello() + m.hello.assert_called() + + m.hello() + m.hello.assert_called() + + def test_assert_called_once(self): + m = Mock() + with self.assertRaises(AssertionError): + m.hello.assert_called_once() + m.hello() + m.hello() + with self.assertRaises(AssertionError): + m.hello.assert_called_once() + m.reset_mock() + m.hello() + m.hello.assert_called_once() + #Issue21256 printout of keyword args should be in deterministic order def test_sorted_call_signature(self): m = Mock()