File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -259,6 +259,34 @@ the *new_callable* argument to :func:`patch`.
259259 used to set attributes on the mock after it is created. See the
260260 :meth: `configure_mock ` method for details.
261261
262+ .. method :: assert_called(*args, **kwargs)
263+
264+ Assert that the mock was called at least once.
265+
266+ >>> mock = Mock()
267+ >>> mock.method()
268+ <Mock name='mock.method()' id='...'>
269+ >>> mock.method.assert_called()
270+
271+ .. versionadded :: 3.6
272+
273+ .. method :: assert_called_once(*args, **kwargs)
274+
275+ Assert that the mock was called exactly once.
276+
277+ >>> mock = Mock()
278+ >>> mock.method()
279+ <Mock name='mock.method()' id='...'>
280+ >>> mock.method.assert_called_once()
281+ >>> mock.method()
282+ <Mock name='mock.method()' id='...'>
283+ >>> mock.method.assert_called_once()
284+ Traceback (most recent call last):
285+ ...
286+ AssertionError: Expected 'method' to have been called once. Called 2 times.
287+
288+ .. versionadded :: 3.6
289+
262290
263291 .. method :: assert_called_with(*args, **kwargs)
264292
Original file line number Diff line number Diff line change @@ -161,6 +161,18 @@ telnetlib
161161Stéphane Wirtel in :issue: `25485 `).
162162
163163
164+ unittest.mock
165+ -------------
166+
167+ The :class: `~unittest.mock.Mock ` class has the following improvements:
168+
169+ * Two new methods, :meth: `Mock.assert_called()
170+ <unittest.mock.Mock.assert_called> ` and :meth: `Mock.assert_called_once()
171+ <unittest.mock.Mock.assert_called_once> ` to check if the mock object
172+ was called.
173+ (Contributed by Amit Saha in :issue: `26323 `.)
174+
175+
164176urllib.robotparser
165177------------------
166178
Original file line number Diff line number Diff line change @@ -772,6 +772,24 @@ def assert_not_called(_mock_self):
772772 (self ._mock_name or 'mock' , self .call_count ))
773773 raise AssertionError (msg )
774774
775+ def assert_called (_mock_self ):
776+ """assert that the mock was called at least once
777+ """
778+ self = _mock_self
779+ if self .call_count == 0 :
780+ msg = ("Expected '%s' to have been called." %
781+ self ._mock_name or 'mock' )
782+ raise AssertionError (msg )
783+
784+ def assert_called_once (_mock_self ):
785+ """assert that the mock was called only once.
786+ """
787+ self = _mock_self
788+ if not self .call_count == 1 :
789+ msg = ("Expected '%s' to have been called once. Called %s times." %
790+ (self ._mock_name or 'mock' , self .call_count ))
791+ raise AssertionError (msg )
792+
775793 def assert_called_with (_mock_self , * args , ** kwargs ):
776794 """assert that the mock was called with the specified arguments.
777795
Original file line number Diff line number Diff line change @@ -1222,6 +1222,27 @@ def test_assert_not_called(self):
12221222 with self .assertRaises (AssertionError ):
12231223 m .hello .assert_not_called ()
12241224
1225+ def test_assert_called (self ):
1226+ m = Mock ()
1227+ with self .assertRaises (AssertionError ):
1228+ m .hello .assert_called ()
1229+ m .hello ()
1230+ m .hello .assert_called ()
1231+
1232+ m .hello ()
1233+ m .hello .assert_called ()
1234+
1235+ def test_assert_called_once (self ):
1236+ m = Mock ()
1237+ with self .assertRaises (AssertionError ):
1238+ m .hello .assert_called_once ()
1239+ m .hello ()
1240+ m .hello .assert_called_once ()
1241+
1242+ m .hello ()
1243+ with self .assertRaises (AssertionError ):
1244+ m .hello .assert_called_once ()
1245+
12251246 #Issue21256 printout of keyword args should be in deterministic order
12261247 def test_sorted_call_signature (self ):
12271248 m = Mock ()
Original file line number Diff line number Diff line change @@ -1267,6 +1267,7 @@ Bernt Røskar Brenna
12671267Constantina S.
12681268Patrick Sabin
12691269Sébastien Sablé
1270+ Amit Saha
12701271Suman Saha
12711272Hajime Saitou
12721273George Sakkis
Original file line number Diff line number Diff line change @@ -201,6 +201,9 @@ Core and Builtins
201201Library
202202-------
203203
204+ - Issue #26323: Add Mock.assert_called() and Mock.assert_called_once()
205+ methods to unittest.mock. Patch written by Amit Saha.
206+
204207- Issue #20589: Invoking Path.owner() and Path.group() on Windows now raise
205208 NotImplementedError instead of ImportError.
206209
You can’t perform that action at this time.
0 commit comments