pytest-asyncio simplifies handling event loops, managing async fixtures, and bridges the gap between async programming and thorough testing.
AnyIO provides built-in support for testing your library or application in the form of a pytest plugin.
This plugin is part of the AnyIO distribution, so nothing extra needs to be installed to use it.
Pytest does not natively support running asynchronous test functions, so they have to be marked for the AnyIO pytest plugin to pick them up.
This can be done in one of three ways:
- Setting the
anyio_mode = "auto"option in the pytest configuration - Using the
pytest.mark.anyiomarker - Using the
anyio_backendfixture, either directly or via another fixture
The simplest way is thus the following:
[tool.pytest.ini_options]anyio_mode = "autoFor example, the following code is executed as a test item by pytest:
@pytest.mark.asyncioasync def test_some_asyncio_code(): res = await library.do_something() assert b"expected result" == res