summaryrefslogtreecommitdiffstats
path: root/Doc/library
diff options
context:
space:
mode:
authorLisa Roach <lisaroach14@gmail.com>2019-09-30 04:56:47 (GMT)
committerGitHub <noreply@github.com>2019-09-30 04:56:47 (GMT)
commit3667e1ee6c90e6d3b6a745cd590ece87118f81ad (patch)
treed9802110a2f6ebbd181b904b05956a56d88c8c44 /Doc/library
parent5bcc6d89bcb622a6786fff632fabdcaf67dbb4e2 (diff)
downloadcpython-3667e1ee6c90e6d3b6a745cd590ece87118f81ad.zip
cpython-3667e1ee6c90e6d3b6a745cd590ece87118f81ad.tar.gz
cpython-3667e1ee6c90e6d3b6a745cd590ece87118f81ad.tar.bz2
bpo-38163: Child mocks detect their type as sync or async (GH-16471)
Diffstat (limited to 'Doc/library')
-rw-r--r--Doc/library/unittest.mock.rst28
1 files changed, 27 insertions, 1 deletions
diff --git a/Doc/library/unittest.mock.rst b/Doc/library/unittest.mock.rst
index 300f28c..3fa3cfd 100644
--- a/Doc/library/unittest.mock.rst
+++ b/Doc/library/unittest.mock.rst
@@ -865,7 +865,7 @@ object::
True
The result of ``mock()`` is an async function which will have the outcome
- of ``side_effect`` or ``return_value``:
+ of ``side_effect`` or ``return_value`` after it has been awaited:
- if ``side_effect`` is a function, the async function will return the
result of that function,
@@ -890,6 +890,32 @@ object::
>>> mock() # doctest: +SKIP
<coroutine object AsyncMockMixin._mock_call at ...>
+
+ Setting the *spec* of a :class:`Mock`, :class:`MagicMock`, or :class:`AsyncMock`
+ to a class with asynchronous and synchronous functions will automatically
+ detect the synchronous functions and set them as :class:`MagicMock` (if the
+ parent mock is :class:`AsyncMock` or :class:`MagicMock`) or :class:`Mock` (if
+ the parent mock is :class:`Mock`). All asynchronous functions will be
+ :class:`AsyncMock`.
+
+ >>> class ExampleClass:
+ ... def sync_foo():
+ ... pass
+ ... async def async_foo():
+ ... pass
+ ...
+ >>> a_mock = AsyncMock(ExampleClass)
+ >>> a_mock.sync_foo
+ <MagicMock name='mock.sync_foo' id='...'>
+ >>> a_mock.async_foo
+ <AsyncMock name='mock.async_foo' id='...'>
+ >>> mock = Mock(ExampleClass)
+ >>> mock.sync_foo
+ <Mock name='mock.sync_foo' id='...'>
+ >>> mock.async_foo
+ <AsyncMock name='mock.async_foo' id='...'>
+
+
.. method:: assert_awaited()
Assert that the mock was awaited at least once. Note that this is separate