cpython: bd75f8d34f97 (original) (raw)
Mercurial > cpython
changeset 100493:bd75f8d34f97
Add Mock.assert_called() Issue #26323: Add assert_called() and assert_called_once() methods to unittest.mock.Mock. [#26323]
Victor Stinner victor.stinner@gmail.com | |
---|---|
date | Fri, 11 Mar 2016 22:17:48 +0100 |
parents | 40d92c92eb6e |
children | 291d47954618 |
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 |
diffstat | 6 files changed, 83 insertions(+), 0 deletions(-)[+] [-] Doc/library/unittest.mock.rst 28 Doc/whatsnew/3.6.rst 12 Lib/unittest/mock.py 18 Lib/unittest/test/testmock/testmock.py 21 Misc/ACKS 1 Misc/NEWS 3 |
line wrap: on
line diff
--- a/Doc/library/unittest.mock.rst
+++ b/Doc/library/unittest.mock.rst
@@ -259,6 +259,34 @@ the new_callable argument to :func:pa[](#l1.3) used to set attributes on the mock after it is created. See the[](#l1.4) :meth:
configure_mock` method for details.
Assert that the mock was called at least once.[](#l1.9)
>>> mock = Mock()[](#l1.11)
>>> mock.method()[](#l1.12)
<Mock name='mock.method()' id='...'>[](#l1.13)
>>> mock.method.assert_called()[](#l1.14)
.. versionadded:: 3.6[](#l1.16)
Assert that the mock was called exactly once.[](#l1.20)
>>> mock = Mock()[](#l1.22)
>>> mock.method()[](#l1.23)
<Mock name='mock.method()' id='...'>[](#l1.24)
>>> mock.method.assert_called_once()[](#l1.25)
>>> mock.method()[](#l1.26)
<Mock name='mock.method()' id='...'>[](#l1.27)
>>> mock.method.assert_called_once()[](#l1.28)
Traceback (most recent call last):[](#l1.29)
...[](#l1.30)
AssertionError: Expected 'method' to have been called once. Called 2 times.[](#l1.31)
.. versionadded:: 3.6[](#l1.33)
+ .. method:: assert_called_with(*args, **kwargs)
--- a/Doc/whatsnew/3.6.rst
+++ b/Doc/whatsnew/3.6.rst
@@ -161,6 +161,18 @@ telnetlib
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()
- <unittest.mock.Mock.assert_called>
and :meth:
Mock.assert_called_once() - <unittest.mock.Mock.assert_called_once>` to check if the mock object
- was called.
- (Contributed by Amit Saha in :issue:
26323
.) + + urllib.robotparser ------------------
--- a/Lib/unittest/mock.py +++ b/Lib/unittest/mock.py @@ -772,6 +772,24 @@ class NonCallableMock(Base): (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[](#l3.8)
"""[](#l3.9)
self = _mock_self[](#l3.10)
if self.call_count == 0:[](#l3.11)
msg = ("Expected '%s' to have been called." %[](#l3.12)
self._mock_name or 'mock')[](#l3.13)
raise AssertionError(msg)[](#l3.14)
- def assert_called_once(_mock_self):
"""assert that the mock was called only once.[](#l3.17)
"""[](#l3.18)
self = _mock_self[](#l3.19)
if not self.call_count == 1:[](#l3.20)
msg = ("Expected '%s' to have been called once. Called %s times." %[](#l3.21)
(self._mock_name or 'mock', self.call_count))[](#l3.22)
raise AssertionError(msg)[](#l3.23)
+ def assert_called_with(_mock_self, *args, **kwargs): """assert that the mock was called with the specified arguments.
--- a/Lib/unittest/test/testmock/testmock.py +++ b/Lib/unittest/test/testmock/testmock.py @@ -1222,6 +1222,27 @@ class MockTest(unittest.TestCase): with self.assertRaises(AssertionError): m.hello.assert_not_called()
- def test_assert_called(self):
m = Mock()[](#l4.8)
with self.assertRaises(AssertionError):[](#l4.9)
m.hello.assert_called()[](#l4.10)
m.hello()[](#l4.11)
m.hello.assert_called()[](#l4.12)
m.hello()[](#l4.14)
m.hello.assert_called()[](#l4.15)
- def test_assert_called_once(self):
m = Mock()[](#l4.18)
with self.assertRaises(AssertionError):[](#l4.19)
m.hello.assert_called_once()[](#l4.20)
m.hello()[](#l4.21)
m.hello.assert_called_once()[](#l4.22)
m.hello()[](#l4.24)
with self.assertRaises(AssertionError):[](#l4.25)
m.hello.assert_called_once()[](#l4.26)
+ #Issue21256 printout of keyword args should be in deterministic order def test_sorted_call_signature(self): m = Mock()
--- a/Misc/ACKS +++ b/Misc/ACKS @@ -1267,6 +1267,7 @@ Bernt Røskar Brenna Constantina S. Patrick Sabin Sébastien Sablé +Amit Saha Suman Saha Hajime Saitou George Sakkis
--- a/Misc/NEWS +++ b/Misc/NEWS @@ -201,6 +201,9 @@ Core and Builtins Library ------- +- Issue #26323: Add Mock.assert_called() and Mock.assert_called_once()