Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
C
cpython
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
cpython
Commits
77b3b770
Unverified
Kaydet (Commit)
77b3b770
authored
May 20, 2019
tarafından
Lisa Roach
Kaydeden (comit)
GitHub
May 20, 2019
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
bpo-26467: Adds AsyncMock for asyncio Mock library support (GH-9296)
üst
0f72147c
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
223 additions
and
3 deletions
+223
-3
unittest.mock.rst
Doc/library/unittest.mock.rst
+214
-1
3.8.rst
Doc/whatsnew/3.8.rst
+4
-0
mock.py
Lib/unittest/mock.py
+0
-0
testasync.py
Lib/unittest/test/testmock/testasync.py
+0
-0
testmock.py
Lib/unittest/test/testmock/testmock.py
+3
-2
2018-09-13-20-33-24.bpo-26467.cahAk3.rst
...S.d/next/Library/2018-09-13-20-33-24.bpo-26467.cahAk3.rst
+2
-0
No files found.
Doc/library/unittest.mock.rst
Dosyayı görüntüle @
77b3b770
...
...
@@ -201,9 +201,11 @@ The Mock Class
.. testsetup::
import asyncio
import inspect
import unittest
from unittest.mock import sentinel, DEFAULT, ANY
from unittest.mock import patch, call, Mock, MagicMock, PropertyMock
from unittest.mock import patch, call, Mock, MagicMock, PropertyMock
, AsyncMock
from unittest.mock import mock_open
:class:`
Mock
` is a flexible mock object intended to replace the use of stubs and
...
...
@@ -851,6 +853,217 @@ object::
>>> p.assert_called_once_with()
.. class:: AsyncMock(spec=None, side_effect=None, return_value=DEFAULT, wraps=None, name=None, spec_set=None, unsafe=False, **kwargs)
An asynchronous version of :class:`
Mock
`. The :class:`
AsyncMock
` object will
behave so the object is recognized as an async function, and the result of a
call is an awaitable.
>>> mock = AsyncMock()
>>> asyncio.iscoroutinefunction(mock)
True
>>> inspect.isawaitable(mock())
True
The result of ``mock()`` is an async function which will have the outcome
of ``side_effect`` or ``return_value``:
- if ``side_effect`` is a function, the async function will return the
result of that function,
- if ``side_effect`` is an exception, the async function will raise the
exception,
- if ``side_effect`` is an iterable, the async function will return the
next value of the iterable, however, if the sequence of result is
exhausted, ``StopIteration`` is raised immediately,
- if ``side_effect`` is not defined, the async function will return the
value defined by ``return_value``, hence, by default, the async function
returns a new :class:`
AsyncMock
` object.
Setting the *spec* of a :class:`
Mock
` or :class:`
MagicMock
` to an async function
will result in a coroutine object being returned after calling.
>>> async def async_func(): pass
...
>>> mock = MagicMock(async_func)
>>> mock
<MagicMock spec='function' id='...'>
>>> mock()
<coroutine object AsyncMockMixin._mock_call at ...>
.. method:: assert_awaited()
Assert that the mock was awaited at least once.
>>> mock = AsyncMock()
>>> async def main():
... await mock()
...
>>> asyncio.run(main())
>>> mock.assert_awaited()
>>> mock_2 = AsyncMock()
>>> mock_2.assert_awaited()
Traceback (most recent call last):
...
AssertionError: Expected mock to have been awaited.
.. method:: assert_awaited_once()
Assert that the mock was awaited exactly once.
>>> mock = AsyncMock()
>>> async def main():
... await mock()
...
>>> asyncio.run(main())
>>> mock.assert_awaited_once()
>>> asyncio.run(main())
>>> mock.method.assert_awaited_once()
Traceback (most recent call last):
...
AssertionError: Expected mock to have been awaited once. Awaited 2 times.
.. method:: assert_awaited_with(*args, **kwargs)
Assert that the last await was with the specified arguments.
>>> mock = AsyncMock()
>>> async def main(*args, **kwargs):
... await mock(*args, **kwargs)
...
>>> asyncio.run(main('foo', bar='bar'))
>>> mock.assert_awaited_with('foo', bar='bar')
>>> mock.assert_awaited_with('other')
Traceback (most recent call last):
...
AssertionError: expected call not found.
Expected: mock('other')
Actual: mock('foo', bar='bar')
.. method:: assert_awaited_once_with(*args, **kwargs)
Assert that the mock was awaited exactly once and with the specified
arguments.
>>> mock = AsyncMock()
>>> async def main(*args, **kwargs):
... await mock(*args, **kwargs)
...
>>> asyncio.run(main('foo', bar='bar'))
>>> mock.assert_awaited_once_with('foo', bar='bar')
>>> asyncio.run(main('foo', bar='bar'))
>>> mock.assert_awaited_once_with('foo', bar='bar')
Traceback (most recent call last):
...
AssertionError: Expected mock to have been awaited once. Awaited 2 times.
.. method:: assert_any_await(*args, **kwargs)
Assert the mock has ever been awaited with the specified arguments.
>>> mock = AsyncMock()
>>> async def main(*args, **kwargs):
... await mock(*args, **kwargs)
...
>>> asyncio.run(main('foo', bar='bar'))
>>> asyncio.run(main('hello'))
>>> mock.assert_any_await('foo', bar='bar')
>>> mock.assert_any_await('other')
Traceback (most recent call last):
...
AssertionError: mock('other') await not found
.. method:: assert_has_awaits(calls, any_order=False)
Assert the mock has been awaited with the specified calls.
The :attr:`
await_args_list
` list is checked for the awaits.
If *any_order* is False (the default) then the awaits must be
sequential. There can be extra calls before or after the
specified awaits.
If *any_order* is True then the awaits can be in any order, but
they must all appear in :attr:`
await_args_list
`.
>>> mock = AsyncMock()
>>> async def main(*args, **kwargs):
... await mock(*args, **kwargs)
...
>>> calls = [call("foo"), call("bar")]
>>> mock.assert_has_calls(calls)
Traceback (most recent call last):
...
AssertionError: Calls not found.
Expected: [call('foo'), call('bar')]
>>> asyncio.run(main('foo'))
>>> asyncio.run(main('bar'))
>>> mock.assert_has_calls(calls)
.. method:: assert_not_awaited()
Assert that the mock was never awaited.
>>> mock = AsyncMock()
>>> mock.assert_not_awaited()
.. method:: reset_mock(*args, **kwargs)
See :func:`
Mock
.
reset_mock
`. Also sets :attr:`
await_count
` to 0,
:attr:`
await_args
` to None, and clears the :attr:`
await_args_list
`.
.. attribute:: await_count
An integer keeping track of how many times the mock object has been awaited.
>>> mock = AsyncMock()
>>> async def main():
... await mock()
...
>>> asyncio.run(main())
>>> mock.await_count
1
>>> asyncio.run(main())
>>> mock.await_count
2
.. attribute:: await_args
This is either ``None`` (if the mock hasn’t been awaited), or the arguments that
the mock was last awaited with. Functions the same as :attr:`
Mock
.
call_args
`.
>>> mock = AsyncMock()
>>> async def main(*args):
... await mock(*args)
...
>>> mock.await_args
>>> asyncio.run(main('foo'))
>>> mock.await_args
call('foo')
>>> asyncio.run(main('bar'))
>>> mock.await_args
call('bar')
.. attribute:: await_args_list
This is a list of all the awaits made to the mock object in sequence (so the
length of the list is the number of times it has been awaited). Before any
awaits have been made it is an empty list.
>>> mock = AsyncMock()
>>> async def main(*args):
... await mock(*args)
...
>>> mock.await_args_list
[]
>>> asyncio.run(main('foo'))
>>> mock.await_args_list
[call('foo')]
>>> asyncio.run(main('bar'))
>>> mock.await_args_list
[call('foo'), call('bar')]
Calling
~~~~~~~
...
...
Doc/whatsnew/3.8.rst
Dosyayı görüntüle @
77b3b770
...
...
@@ -538,6 +538,10 @@ unicodedata
unittest
--------
* XXX Added :class:`AsyncMock` to support an asynchronous version of :class:`Mock`.
Appropriate new assert functions for testing have been added as well.
(Contributed by Lisa Roach in :issue:`26467`).
* Added :func:`~unittest.addModuleCleanup()` and
:meth:`~unittest.TestCase.addClassCleanup()` to unittest to support
cleanups for :func:`~unittest.setUpModule()` and
...
...
Lib/unittest/mock.py
Dosyayı görüntüle @
77b3b770
This diff is collapsed.
Click to expand it.
Lib/unittest/test/testmock/testasync.py
0 → 100644
Dosyayı görüntüle @
77b3b770
This diff is collapsed.
Click to expand it.
Lib/unittest/test/testmock/testmock.py
Dosyayı görüntüle @
77b3b770
...
...
@@ -9,7 +9,7 @@ from unittest import mock
from
unittest.mock
import
(
call
,
DEFAULT
,
patch
,
sentinel
,
MagicMock
,
Mock
,
NonCallableMock
,
NonCallableMagicMock
,
_Call
,
_CallList
,
NonCallableMagicMock
,
AsyncMock
,
_Call
,
_CallList
,
create_autospec
)
...
...
@@ -1618,7 +1618,8 @@ class MockTest(unittest.TestCase):
def
test_adding_child_mock
(
self
):
for
Klass
in
NonCallableMock
,
Mock
,
MagicMock
,
NonCallableMagicMock
:
for
Klass
in
(
NonCallableMock
,
Mock
,
MagicMock
,
NonCallableMagicMock
,
AsyncMock
):
mock
=
Klass
()
mock
.
foo
=
Mock
()
...
...
Misc/NEWS.d/next/Library/2018-09-13-20-33-24.bpo-26467.cahAk3.rst
0 → 100644
Dosyayı görüntüle @
77b3b770
Added AsyncMock to support using unittest to mock asyncio coroutines.
Patch by Lisa Roach.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment