Registry / testing / pytest-aiohttp

pytest-aiohttp

JSON →
library1.1.0pypypiunverified

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`.

pip install pytest-aiohttp
INSTALL
IMPORT
SIG · PYTEST-AIOHTTP
P
pytest-aiohttp
testingpythonv1.1.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

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
Debug
Known issues
breakingThe `loop` fixture was removed in version 1.0.0. If your tests explicitly depended on `loop` for asyncio event loop access, you should now use `asyncio.get_event_loop()` or `asyncio.current_task()` where appropriate.
fix
Remove the `loop` fixture from your test function signatures. Instead, use standard `asyncio` module functions like `asyncio.get_event_loop()` if you need to interact with the event loop directly, or rely on `pytest-asyncio` to manage it implicitly.
affects: >=1.0.0
breakingPython 3.7 and 3.8 support was dropped in version 1.0.0. Users on these Python versions must upgrade to Python 3.9 or newer to use `pytest-aiohttp` v1.0.0 and later.
fix
Upgrade your Python environment to version 3.9 or higher. Alternatively, if upgrading Python is not an option, you must stick to `pytest-aiohttp` versions older than 1.0.0 (e.g., `0.4.0`).
affects: >=1.0.0
gotchaYou must configure `pytest-asyncio` by adding `asyncio_mode = auto` to your `pytest.ini` or `pyproject.toml` file. Without this, your async tests using `pytest-aiohttp` fixtures will not run correctly.
fix
Create a `pytest.ini` file in your project root (or configure `pyproject.toml`) and add the line `asyncio_mode = auto` under the `[pytest]` section. For example:
```ini
[pytest]
asyncio_mode = auto
```
affects: All versions
gotcha`pytest-aiohttp` has specific `pytest` version requirements. For example, `pytest-aiohttp>=1.0.0` requires `pytest>=7.0.0`. Using an incompatible version of `pytest` can lead to unexpected errors or fixtures not being discovered.
fix
Always ensure your `pytest` installation meets the requirements specified by `pytest-aiohttp`. Check the `install_requires` section on the `pytest-aiohttp` PyPI page or its `setup.py`/`pyproject.toml` for the exact `pytest` version bounds.
affects: All versions
Errors
Common errors & fixes
fixture 'aiohttp_client' not found
The pytest-aiohttp plugin is not installed or not discoverable by pytest in the current environment.
fix
Install both `pytest-aiohttp` and its dependency `pytest-asyncio`: `pip install pytest-aiohttp pytest-asyncio`
AttributeError: 'coroutine' object has no attribute 'status'
An asynchronous method of the `aiohttp_client` (e.g., `client.get()`) was called without `await`, causing it to return a coroutine object instead of the actual `ClientResponse`.
fix
Prepend `await` to the asynchronous call: `response = await client.get('/test')`
fixture 'event_loop' not found
The `pytest-asyncio` plugin, which provides the `event_loop` fixture required by `pytest-aiohttp`, is either not installed or not properly configured.
fix
Install `pytest-asyncio`: `pip install pytest-asyncio`
fixture 'create_client' not found
The `create_client` fixture was renamed to `aiohttp_client` in `pytest-aiohttp` version 1.0.0 (and `create_server` to `aiohttp_server`).
fix
Update your test code to use `aiohttp_client` instead of `create_client`: `async def test_my_app(aiohttp_client):`
Upgrade
Version history
1.1.0latest on PyPI · released Jan 23, 2025
Audit
Dependencies
aiohttprequiredCore functionality for creating and testing aiohttp applications.
pytestrequiredThe base test framework this plugin extends.
pytest-asynciorequiredProvides asyncio support for pytest, including the necessary event loop handling for async tests.
Agent activity
3 hits · last 30 days
node
2
Resources
pytest-aiohttp — pip install pytest-aiohttp · libregistry