mirror of
https://github.com/python/cpython.git
synced 2025-07-23 19:25:40 +00:00
Closes Issue 21262: New method assert_not_called for Mock.
It raises AssertionError if the mock has been called.
This commit is contained in:
parent
8c14534df6
commit
8af9db3e4f
4 changed files with 34 additions and 0 deletions
|
@ -321,6 +321,20 @@ the `new_callable` argument to `patch`.
|
||||||
>>> calls = [call(4), call(2), call(3)]
|
>>> calls = [call(4), call(2), call(3)]
|
||||||
>>> mock.assert_has_calls(calls, any_order=True)
|
>>> mock.assert_has_calls(calls, any_order=True)
|
||||||
|
|
||||||
|
.. method:: assert_not_called(*args, **kwargs)
|
||||||
|
|
||||||
|
Assert the mock was never called.
|
||||||
|
|
||||||
|
>>> m = Mock()
|
||||||
|
>>> m.hello.assert_not_called()
|
||||||
|
>>> obj = m.hello()
|
||||||
|
>>> m.hello.assert_not_called()
|
||||||
|
Traceback (most recent call last):
|
||||||
|
...
|
||||||
|
AssertionError: Expected 'hello' to not have been called. Called 1 times.
|
||||||
|
|
||||||
|
.. versionadded:: 3.5
|
||||||
|
|
||||||
|
|
||||||
.. method:: reset_mock()
|
.. method:: reset_mock()
|
||||||
|
|
||||||
|
|
|
@ -758,6 +758,14 @@ class NonCallableMock(Base):
|
||||||
else:
|
else:
|
||||||
return _call
|
return _call
|
||||||
|
|
||||||
|
def assert_not_called(_mock_self, *args, **kwargs):
|
||||||
|
"""assert that the mock was never called.
|
||||||
|
"""
|
||||||
|
self = _mock_self
|
||||||
|
if self.call_count != 0:
|
||||||
|
msg = ("Expected '%s' to not have been called. Called %s times." %
|
||||||
|
(self._mock_name or 'mock', self.call_count))
|
||||||
|
raise AssertionError(msg)
|
||||||
|
|
||||||
def assert_called_with(_mock_self, *args, **kwargs):
|
def assert_called_with(_mock_self, *args, **kwargs):
|
||||||
"""assert that the mock was called with the specified arguments.
|
"""assert that the mock was called with the specified arguments.
|
||||||
|
|
|
@ -1198,6 +1198,15 @@ class MockTest(unittest.TestCase):
|
||||||
m.assert_foo_call()
|
m.assert_foo_call()
|
||||||
m.assret_foo_call()
|
m.assret_foo_call()
|
||||||
|
|
||||||
|
#Issue21262
|
||||||
|
def test_assert_not_called(self):
|
||||||
|
m = Mock()
|
||||||
|
m.hello.assert_not_called()
|
||||||
|
m.hello()
|
||||||
|
with self.assertRaises(AssertionError):
|
||||||
|
m.hello.assert_not_called()
|
||||||
|
|
||||||
|
|
||||||
def test_mock_add_spec(self):
|
def test_mock_add_spec(self):
|
||||||
class _One(object):
|
class _One(object):
|
||||||
one = 1
|
one = 1
|
||||||
|
|
|
@ -50,6 +50,9 @@ Core and Builtins
|
||||||
Library
|
Library
|
||||||
-------
|
-------
|
||||||
|
|
||||||
|
- Issue #21262: New method assert_not_called for Mock.
|
||||||
|
It raises AssertionError if the mock has been called.
|
||||||
|
|
||||||
- Issue #21238: New keyword argument `unsafe` to Mock. It raises
|
- Issue #21238: New keyword argument `unsafe` to Mock. It raises
|
||||||
`AttributeError` incase of an attribute startswith assert or assret.
|
`AttributeError` incase of an attribute startswith assert or assret.
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue