{"library":"asynctest","title":"asynctest","description":"asynctest is a Python library that enhances the standard `unittest` package with features tailored for testing `asyncio` applications. It reduces boilerplate by providing `TestCase` subclasses and mock objects designed to work seamlessly with coroutines and event loops. While built on `unittest`, it overrides and adds features for asynchronous testing, currently targeting the 'selector' model. The current version is 0.13.0, and its release cadence has been infrequent, indicating a maintenance status.","status":"maintenance","version":"0.13.0","language":"en","source_language":"en","source_url":"https://github.com/Martiusweb/asynctest/","tags":["asyncio","testing","unittest","mocking","python3"],"install":[{"cmd":"pip install asynctest","lang":"bash","label":"Install stable version"}],"dependencies":[],"imports":[{"note":"asynctest.TestCase is the core class, inheriting from and extending unittest.TestCase for asyncio.","wrong":"from unittest import TestCase","symbol":"TestCase","correct":"from asynctest import TestCase"},{"note":"CoroutineMock specifically handles mocking of async def functions, returning awaitable objects.","wrong":"from unittest.mock import Mock","symbol":"CoroutineMock","correct":"from asynctest.mock import CoroutineMock"},{"note":"asynctest's patch is enhanced to correctly mock coroutines and asynchronous context managers/iterators.","wrong":"from unittest.mock import patch","symbol":"patch","correct":"from asynctest.mock import patch"}],"quickstart":{"code":"import asynctest\nimport asyncio\n\nclass MyAsyncTest(asynctest.TestCase):\n    async def my_async_function(self):\n        # Simulate an asynchronous operation\n        await asyncio.sleep(0.001)\n        return \"async_result\"\n\n    async def test_my_async_method(self):\n        result = await self.my_async_function()\n        self.assertEqual(result, \"async_result\")\n\n    async def test_with_coroutine_mock(self):\n        mock_return = \"mocked_async_data\"\n        my_mock = asynctest.CoroutineMock(return_value=mock_return)\n        \n        # Call the mocked coroutine\n        data = await my_mock()\n        \n        self.assertEqual(data, mock_return)\n        my_mock.assert_awaited_once()\n\n# To run these tests, save the file (e.g., test_my_module.py) and run:\n# python -m unittest test_my_module.py\n# asynctest.main() can also be used if explicitly desired, but unittest discovery works.","lang":"python","description":"This quickstart demonstrates how to create an asynchronous test case using `asynctest.TestCase`, defining an `async def` test method, and how to use `asynctest.CoroutineMock` to mock asynchronous functions. Tests are automatically run within their own event loop, which is managed by `asynctest`."},"warnings":[{"fix":"Upgrade to Python 3.5 or newer.","message":"Python 3.4 is no longer supported since asynctest 0.13.0. Importing the library on Python 3.4 will raise a SyntaxError.","severity":"breaking","affected_versions":">=0.13.0"},{"fix":"Review existing tests using `@patch` on coroutines and adapt them to the new behavior where mocks are no longer implicitly re-used in the same way, aligning with standard `unittest.mock` behavior. Consult issue #121 on GitHub for details.","message":"The behavior of `@patch` decorators for coroutines changed in asynctest 0.13.0. Previously, the mock object was re-used for each call of the coroutine, which was inconsistent with `unittest.patch`.","severity":"breaking","affected_versions":">=0.13.0"},{"fix":"Consider testing on a 'selector' compatible environment or be aware of potential limitations on Windows using the 'proactor' model.","message":"asynctest currently targets the 'selector' model for event loops. Some features may not work as expected or at all with Windows' 'proactor' event loop policy.","severity":"gotcha","affected_versions":"All versions"},{"fix":"This is primarily an internal warning. Users should ensure their own asynchronous code uses `async def` and `await` keywords for modern asyncio patterns.","message":"Internal usage of the `@asyncio.coroutine` decorator within asynctest's mock module may trigger `DeprecationWarning` in Python 3.8+ (where `async def` is preferred). While tests may still run, it indicates reliance on deprecated asyncio features.","severity":"deprecated","affected_versions":"All versions on Python >=3.8"},{"fix":"Always use `await` for any coroutine calls (including mocked ones) within your `asynctest.TestCase` methods to ensure they execute and their results are processed correctly.","message":"Forgetting to `await` asynchronous calls within `async def` test methods, `setUp`, or `tearDown` can lead to coroutines not running or tests passing unexpectedly without verifying the asynchronous operation's completion.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Avoid sharing event loops between tests if possible. asynctest's default behavior of creating a new isolated loop for each test is generally recommended for reliable unit testing. If a shared loop is necessary, carefully manage its state with `setUp` and `tearDown` to ensure isolation.","message":"Setting `TestCase.use_default_loop = True` to share a single event loop across multiple tests can lead to unpredictable test states and side effects, as tasks or callbacks from previous tests might interfere with subsequent ones.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-06T00:00:00.000Z","next_check":"2026-07-05T00:00:00.000Z"}