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-aiohttpNo compatibility data collected yet for this library.
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.
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.
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`).
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 ```
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.
Install both `pytest-aiohttp` and its dependency `pytest-asyncio`: `pip install pytest-aiohttp pytest-asyncio`
Prepend `await` to the asynchronous call: `response = await client.get('/test')`Install `pytest-asyncio`: `pip install pytest-asyncio`
Update your test code to use `aiohttp_client` instead of `create_client`: `async def test_my_app(aiohttp_client):`