You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Carter Tinney edited this page Sep 9, 2019
·
1 revision
Testing with Async/Await
Our tooling is currently flawed for testing coroutine functions, as coroutine functions return asyncio.Future objects rather than the result of the call. This makes mocking tricky, even when using both the pytest-mock and pytest-asyncio extensions.
In order to mock a coroutine function/method to A) return a value, and B) not hang forever, do something like the following:
As you can see, by returning a completed future the mocked coroutine method was able to be compatible with the await syntax. Additionally, the result of the future was set to the mocked return value, which was then transferred to the variable res after the await was resolved.