pytest-asyncio

1.3.0 · active · verified Wed Mar 25

pytest plugin for testing asyncio code. Required for async def test_ functions — pytest does not run them natively. Has undergone multiple breaking redesigns around event loop scoping. Current version is 1.3.0 (2026). Has a long history of yanked releases and sudden breaking changes.

Warnings

Install

Imports

Quickstart

asyncio_mode must be configured. Use @pytest_asyncio.fixture for async fixtures.

# pytest.ini or pyproject.toml — REQUIRED configuration
# [pytest]
# asyncio_mode = auto

# With asyncio_mode=auto, no per-test marks needed:
import pytest_asyncio

@pytest_asyncio.fixture
async def async_client():
    client = await create_client()
    yield client
    await client.close()

async def test_something(async_client):
    result = await async_client.fetch()
    assert result is not None

# Without asyncio_mode=auto, mark each test explicitly:
import pytest

@pytest.mark.asyncio
async def test_explicit():
    await asyncio.sleep(0)

view raw JSON →