{"id":7912,"library":"aiounittest","title":"aiounittest","description":"aiounittest is a helper library designed to simplify testing asynchronous Python code built with `asyncio`. It extends `unittest.TestCase` to support `async`/`await` syntax (Python 3.5+) and `asyncio.coroutine`/`yield from` (Python 3.4). The library is actively maintained, with its latest version being 1.5.0, released in March 2025.","status":"active","version":"1.5.0","language":"en","source_language":"en","source_url":"https://github.com/kwarunek/aiounittest","tags":["asyncio","unittest","testing","async"],"install":[{"cmd":"pip install aiounittest","lang":"bash","label":"Install latest version"}],"dependencies":[],"imports":[{"note":"While technically functional, the top-level import `from aiounittest import AsyncTestCase` is the recommended and cleaner approach.","wrong":"from aiounittest.case import AsyncTestCase","symbol":"AsyncTestCase","correct":"from aiounittest import AsyncTestCase"},{"note":"Used as a decorator for `unittest.TestCase` methods that are coroutines, similar to `pytest.mark.asyncio`.","symbol":"async_test","correct":"from aiounittest import async_test"},{"note":"A helper to make objects awaitable, primarily used with `unittest.mock.Mock` to mock coroutines.","symbol":"futurized","correct":"from aiounittest import futurized"}],"quickstart":{"code":"import asyncio\nimport aiounittest\nimport unittest\n\nasync def async_add(x, y, delay=0.01):\n    await asyncio.sleep(delay)\n    return x + y\n\nclass MyAsyncTest(aiounittest.AsyncTestCase):\n    async def test_async_addition(self):\n        result = await async_add(10, 20)\n        self.assertEqual(result, 30)\n\n    def test_sync_method(self):\n        self.assertTrue(True)\n\n# To run the tests (usually handled by test runners like `pytest` or `unittest` CLI)\nif __name__ == '__main__':\n    unittest.main()","lang":"python","description":"This quickstart demonstrates how to use `aiounittest.AsyncTestCase` to write asynchronous tests. Your async test methods should be `async def` and use `await` where necessary. Synchronous test methods can coexist within the same class."},"warnings":[{"fix":"Migrate tests to inherit from `unittest.IsolatedAsyncioTestCase` and adjust any `aiounittest`-specific helpers as needed.","message":"For Python 3.8 and newer, consider using the built-in `unittest.IsolatedAsyncioTestCase`. The standard library's `unittest` now includes native support for asynchronous tests, reducing the need for `aiounittest` in many cases.","severity":"deprecated","affected_versions":"Python 3.8+"},{"fix":"Initialize `asyncio` objects within the `async def` test methods themselves, or override `get_event_loop` if you need to manage the loop lifecycle more granularly, ensuring objects are created with the test-specific loop.","message":"Creating `asyncio` loop-dependent objects (like `asyncio.Event`) in `setUp()` methods of `aiounittest.AsyncTestCase` can lead to `RuntimeError: got Future attached to a different loop`. `AsyncTestCase` creates a fresh event loop for each test, *after* `setUp()` and *before* the test method runs.","severity":"gotcha","affected_versions":"All"},{"fix":"Rewrite coroutines using `async def` and `await` syntax.","message":"The `@asyncio.coroutine` decorator (for `yield from` syntax) was deprecated in Python 3.8. While `aiounittest` supports it for backward compatibility, it's recommended to use `async def` and `await` for new code.","severity":"deprecated","affected_versions":"Python 3.8+"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Ensure that any `asyncio` objects required by your test are created within the `async def` test method itself, or use `self.loop` (if overridden `get_event_loop`) to explicitly get the test's event loop.","cause":"An `asyncio` object (like a `Future` or `Event`) was created using an event loop that is different from the one `aiounittest.AsyncTestCase` uses to run the actual test. This often happens when such objects are created in `setUp()`.","error":"RuntimeError: got Future attached to a different loop"},{"fix":"Prepend `await` to the call of any coroutine that you intend to execute, e.g., `await my_async_function()`.","cause":"An `async def` function (coroutine) was called but its execution was not scheduled or awaited. `aiounittest.AsyncTestCase` automatically awaits `async def` test methods, but you must `await` any other coroutines called within your test.","error":"RuntimeWarning: coroutine was never awaited"},{"fix":"When mocking asynchronous functions, use `unittest.mock.AsyncMock` instead of `MagicMock`. For example: `with patch('module.async_func', new_callable=AsyncMock) as mock_func:` or use `aiounittest.futurized`.","cause":"You are trying to `await` a standard `unittest.mock.MagicMock` object that is configured to mock an `async def` function. `MagicMock` by default is not awaitable.","error":"TypeError: object MagicMock is not awaitable"}]}