pytest-aiohttp

1.1.0 · active · verified Thu Apr 09

pytest-aiohttp is a pytest plugin that provides useful fixtures for testing aiohttp applications. It simplifies the creation and interaction with aiohttp servers and clients within your pytest test suite. The current version is 1.1.0 and releases are typically made as needed, often following updates to `aiohttp` or `pytest`.

Warnings

Install

Quickstart

To use `pytest-aiohttp`, you need to configure `pytest-asyncio` in your `pytest.ini` (or `pyproject.toml`). A minimal `pytest.ini` might look like this: ```ini [pytest] asyncio_mode = auto ``` This setup automatically handles the asyncio event loop. The example demonstrates defining an `aiohttp` application within a fixture and then using the `aiohttp_client` fixture (provided by `pytest-aiohttp`) to create a test client for your app, which can then be used in your async test functions.

import pytest
from aiohttp import web

# test_app.py

@pytest.fixture
async def cli(aiohttp_client):
    """Client fixture that returns a test client for your aiohttp application."""
    app = web.Application()
    async def hello_handler(request):
        return web.Response(text="Hello from aiohttp app!")
    app.router.add_get('/', hello_handler)
    return await aiohttp_client(app)

async def test_hello_world(cli):
    """Test that the application responds correctly."""
    resp = await cli.get('/')
    assert resp.status == 200
    text = await resp.text()
    assert 'Hello from aiohttp app!' in text

view raw JSON →