PyTest - Asyncio

pytest-asyncio simplifies handling event loops, managing async fixtures, and bridges the gap between async programming and thorough testing.

On this page

anyio - 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.anyio marker
  • Using the anyio_backend fixture, either directly or via another fixture

The simplest way is thus the following:

[tool.pytest.ini_options]
anyio_mode = "auto

For example, the following code is executed as a test item by pytest:

@pytest.mark.asyncio
async def test_some_asyncio_code():
res = await library.do_something()
assert b"expected result" == res

Pending