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_test is an alias of aiounittest.helpers.run_sync

Function can be used like a pytest.mark.asyncio (implementation differs), but it’s compatible with unittest.TestCase class.

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.AsyncTestCase to run coroutines.