(original) (raw)

changeset: 100493:bd75f8d34f97 user: Victor Stinner victor.stinner@gmail.com date: Fri Mar 11 22:17:48 2016 +0100 files: Doc/library/unittest.mock.rst Doc/whatsnew/3.6.rst Lib/unittest/mock.py Lib/unittest/test/testmock/testmock.py Misc/ACKS Misc/NEWS description: Add Mock.assert_called() Issue #26323: Add assert_called() and assert_called_once() methods to unittest.mock.Mock. diff -r 40d92c92eb6e -r bd75f8d34f97 Doc/library/unittest.mock.rst --- a/Doc/library/unittest.mock.rst Fri Mar 11 23:20:09 2016 +0200 +++ b/Doc/library/unittest.mock.rst Fri Mar 11 22:17:48 2016 +0100 @@ -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 at least once. + + >>> 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 40d92c92eb6e -r bd75f8d34f97 Doc/whatsnew/3.6.rst --- a/Doc/whatsnew/3.6.rst Fri Mar 11 23:20:09 2016 +0200 +++ b/Doc/whatsnew/3.6.rst Fri Mar 11 22:17:48 2016 +0100 @@ -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 40d92c92eb6e -r bd75f8d34f97 Lib/unittest/mock.py --- a/Lib/unittest/mock.py Fri Mar 11 23:20:09 2016 +0200 +++ b/Lib/unittest/mock.py Fri Mar 11 22:17:48 2016 +0100 @@ -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 40d92c92eb6e -r bd75f8d34f97 Lib/unittest/test/testmock/testmock.py --- a/Lib/unittest/test/testmock/testmock.py Fri Mar 11 23:20:09 2016 +0200 +++ b/Lib/unittest/test/testmock/testmock.py Fri Mar 11 22:17:48 2016 +0100 @@ -1222,6 +1222,27 @@ 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.assert_called_once() + + m.hello() + with self.assertRaises(AssertionError): + m.hello.assert_called_once() + #Issue21256 printout of keyword args should be in deterministic order def test_sorted_call_signature(self): m = Mock() diff -r 40d92c92eb6e -r bd75f8d34f97 Misc/ACKS --- a/Misc/ACKS Fri Mar 11 23:20:09 2016 +0200 +++ b/Misc/ACKS Fri Mar 11 22:17:48 2016 +0100 @@ -1267,6 +1267,7 @@ Constantina S. Patrick Sabin Sébastien Sablé +Amit Saha Suman Saha Hajime Saitou George Sakkis diff -r 40d92c92eb6e -r bd75f8d34f97 Misc/NEWS --- a/Misc/NEWS Fri Mar 11 23:20:09 2016 +0200 +++ b/Misc/NEWS Fri Mar 11 22:17:48 2016 +0100 @@ -201,6 +201,9 @@ Library ------- +- Issue #26323: Add Mock.assert_called() and Mock.assert_called_once() + methods to unittest.mock. Patch written by Amit Saha. + - Issue #20589: Invoking Path.owner() and Path.group() on Windows now raise NotImplementedError instead of ImportError./victor.stinner@gmail.com