async_test¶
-
aiounittest.async_test(func=None, loop=None)¶ Runs synchonously given function (coroutine)
Parameters: - func (callable) – function to run (mostly coroutine)
- loop (event loop of None) – event loop to use to run func
By default the brand new event loop will be created (old closed). After completion, the loop will be closed and then recreated, set as default, leaving asyncio clean.
Note:
aiounittest.async_testis an alias ofaiounittest.helpers.run_syncFunction can be used like a pytest.mark.asyncio (implementation differs), but it’s compatible with
unittest.TestCaseclass.import asyncio import unittest from aiounittest import async_test async def add(x, y): await asyncio.sleep(0.1) return x + y class MyAsyncTestDecorator(unittest.TestCase): @async_test async def test_async_add(self): ret = await add(5, 6) self.assertEqual(ret, 11)
Note
If the loop is provided, it won’t be closed. It’s up to you.
This function is also used internally by
aiounittest.AsyncTestCaseto run coroutines.