mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
Add Mock.assert_called()
Issue #26323: Add assert_called() and assert_called_once() methods to unittest.mock.Mock.
This commit is contained in:
parent
82442b7022
commit
2c2a4e63d7
6 changed files with 83 additions and 0 deletions
|
@ -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
|
||||
"""
|
||||
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.
|
||||
|
||||
|
|
|
@ -1222,6 +1222,27 @@ class MockTest(unittest.TestCase):
|
|||
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()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue