22import inspect
33import unittest
44
5- from unittest .mock import call , AsyncMock , patch , MagicMock , create_autospec
5+ from unittest .mock import (call , AsyncMock , patch , MagicMock , create_autospec ,
6+ _AwaitEvent )
67
78
89def tearDownModule ():
@@ -20,6 +21,9 @@ def normal_method(self):
2021async def async_func ():
2122 pass
2223
24+ async def async_func_args (a , b , * , c ):
25+ pass
26+
2327def normal_func ():
2428 pass
2529
@@ -141,8 +145,63 @@ def test_create_autospec_instance(self):
141145 create_autospec (async_func , instance = True )
142146
143147 def test_create_autospec (self ):
144- spec = create_autospec (async_func )
148+ spec = create_autospec (async_func_args )
149+ awaitable = spec (1 , 2 , c = 3 )
150+ async def main ():
151+ await awaitable
152+
153+ self .assertEqual (spec .await_count , 0 )
154+ self .assertIsNone (spec .await_args )
155+ self .assertEqual (spec .await_args_list , [])
156+ self .assertIsInstance (spec .awaited , _AwaitEvent )
157+ spec .assert_not_awaited ()
158+
159+ asyncio .run (main ())
160+
145161 self .assertTrue (asyncio .iscoroutinefunction (spec ))
162+ self .assertTrue (asyncio .iscoroutine (awaitable ))
163+ self .assertEqual (spec .await_count , 1 )
164+ self .assertEqual (spec .await_args , call (1 , 2 , c = 3 ))
165+ self .assertEqual (spec .await_args_list , [call (1 , 2 , c = 3 )])
166+ spec .assert_awaited_once ()
167+ spec .assert_awaited_once_with (1 , 2 , c = 3 )
168+ spec .assert_awaited_with (1 , 2 , c = 3 )
169+ spec .assert_awaited ()
170+
171+ def test_patch_with_autospec (self ):
172+
173+ async def test_async ():
174+ with patch (f"{ __name__ } .async_func_args" , autospec = True ) as mock_method :
175+ awaitable = mock_method (1 , 2 , c = 3 )
176+ self .assertIsInstance (mock_method .mock , AsyncMock )
177+
178+ self .assertTrue (asyncio .iscoroutinefunction (mock_method ))
179+ self .assertTrue (asyncio .iscoroutine (awaitable ))
180+ self .assertTrue (inspect .isawaitable (awaitable ))
181+
182+ # Verify the default values during mock setup
183+ self .assertEqual (mock_method .await_count , 0 )
184+ self .assertEqual (mock_method .await_args_list , [])
185+ self .assertIsNone (mock_method .await_args )
186+ self .assertIsInstance (mock_method .awaited , _AwaitEvent )
187+ mock_method .assert_not_awaited ()
188+
189+ await awaitable
190+
191+ self .assertEqual (mock_method .await_count , 1 )
192+ self .assertEqual (mock_method .await_args , call (1 , 2 , c = 3 ))
193+ self .assertEqual (mock_method .await_args_list , [call (1 , 2 , c = 3 )])
194+ mock_method .assert_awaited_once ()
195+ mock_method .assert_awaited_once_with (1 , 2 , c = 3 )
196+ mock_method .assert_awaited_with (1 , 2 , c = 3 )
197+ mock_method .assert_awaited ()
198+
199+ mock_method .reset_mock ()
200+ self .assertEqual (mock_method .await_count , 0 )
201+ self .assertIsNone (mock_method .await_args )
202+ self .assertEqual (mock_method .await_args_list , [])
203+
204+ asyncio .run (test_async ())
146205
147206
148207class AsyncSpecTest (unittest .TestCase ):
0 commit comments