-
-
Save karantan/4225cfad7fa6ffab561d5c9667e9cbd5 to your computer and use it in GitHub Desktop.
Mocking await calls in python 3.5 is a problem
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async def some_coroutine(): | |
await async.sleep(10.0) | |
await another_coroutine() | |
return 'test' | |
async def another_coroutine(): | |
pass |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from unittest import TestCase, mock, main | |
import asyncio | |
from problem import some_coroutine | |
class AsyncMock(mock.MagicMock): | |
async def __call__(self, *args, **kwargs): | |
return super(AsyncMock, self).__call__(*args, **kwargs) | |
class Test(TestCase): | |
def setUp(self): | |
self.loop = asyncio.new_event_loop() | |
asyncio.set_event_loop(self.loop) | |
@mock.patch('problem.asyncio.sleep', new_callable=AsyncMock) | |
@mock.patch('problem.another_coroutine', new_callable=AsyncMock) | |
def test_some_coroutine(self, mock_another_coroutine, sleep): | |
async def run_test(): | |
resp = await some_coroutine() | |
mock_another_coroutine.assert_called_with() | |
sleep.assert_called_with(10.0) | |
self.assertEqual(resp, 'test') | |
self.loop.run_until_complete(run_test()) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment