mirror of
https://github.com/python/cpython.git
synced 2025-08-31 14:07:50 +00:00
bpo-38136: Updates await_count and call_count to be different things (GH-16192)
This commit is contained in:
parent
6f53d34fb0
commit
ef04851775
5 changed files with 208 additions and 43 deletions
|
@ -514,21 +514,6 @@ the *new_callable* argument to :func:`patch`.
|
|||
>>> mock.call_count
|
||||
2
|
||||
|
||||
For :class:`AsyncMock` the :attr:`call_count` is only iterated if the function
|
||||
has been awaited:
|
||||
|
||||
>>> mock = AsyncMock()
|
||||
>>> mock() # doctest: +SKIP
|
||||
<coroutine object AsyncMockMixin._mock_call at ...>
|
||||
>>> mock.call_count
|
||||
0
|
||||
>>> async def main():
|
||||
... await mock()
|
||||
...
|
||||
>>> asyncio.run(main())
|
||||
>>> mock.call_count
|
||||
1
|
||||
|
||||
.. attribute:: return_value
|
||||
|
||||
Set this to configure the value returned by calling the mock:
|
||||
|
@ -907,19 +892,22 @@ object::
|
|||
|
||||
.. method:: assert_awaited()
|
||||
|
||||
Assert that the mock was awaited at least once.
|
||||
Assert that the mock was awaited at least once. Note that this is separate
|
||||
from the object having been called, the ``await`` keyword must be used:
|
||||
|
||||
>>> mock = AsyncMock()
|
||||
>>> async def main():
|
||||
... await mock()
|
||||
>>> async def main(coroutine_mock):
|
||||
... await coroutine_mock
|
||||
...
|
||||
>>> asyncio.run(main())
|
||||
>>> coroutine_mock = mock()
|
||||
>>> mock.called
|
||||
True
|
||||
>>> mock.assert_awaited()
|
||||
>>> mock_2 = AsyncMock()
|
||||
>>> mock_2.assert_awaited()
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
AssertionError: Expected mock to have been awaited.
|
||||
>>> asyncio.run(main(coroutine_mock))
|
||||
>>> mock.assert_awaited()
|
||||
|
||||
.. method:: assert_awaited_once()
|
||||
|
||||
|
@ -1004,14 +992,15 @@ object::
|
|||
... await mock(*args, **kwargs)
|
||||
...
|
||||
>>> calls = [call("foo"), call("bar")]
|
||||
>>> mock.assert_has_calls(calls)
|
||||
>>> mock.assert_has_awaits(calls)
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
AssertionError: Calls not found.
|
||||
AssertionError: Awaits not found.
|
||||
Expected: [call('foo'), call('bar')]
|
||||
Actual: []
|
||||
>>> asyncio.run(main('foo'))
|
||||
>>> asyncio.run(main('bar'))
|
||||
>>> mock.assert_has_calls(calls)
|
||||
>>> mock.assert_has_awaits(calls)
|
||||
|
||||
.. method:: assert_not_awaited()
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue