Async ASGI TestClient is a framework-agnostic library for testing web applications that implement the ASGI specification (versions 2 and 3). It allows direct interaction with an ASGI application within the same asyncio loop as the tests, eliminating the need for a separate HTTP server. Inspired by Quart's testing module, it supports features like cookies, multipart/form-data, redirects, and streaming for both requests and responses, as well as websocket testing. The current version is 1.4.11.
pip install async-asgi-testclientVerified import paths — ran on the pinned version, not inferred.
Demonstrates how to test a simple Quart (ASGI) application using `async-asgi-testclient` with `pytest` and `pytest-asyncio`. The `TestClient` is used as an async context manager to send requests and assert responses.
Ensure your tests are fully asynchronous when interacting with async resources within an `async def` test. If using a framework's built-in `TestClient` which has a synchronous interface, be mindful of its underlying implementation and potential sync-async bridges. `async-asgi-testclient` inherently supports an async context.
Prefer `async with client.ws_session(...)` for WebSocket tests to ensure connections are properly managed. If using `websocket = await client.ws_connect(...)`, explicitly call `await websocket.close()` afterwards.
Review FastAPI routing configuration. If encountering this, consider simplifying routing or investigating if a more direct `TestClient` initialization with the final ASGI app resolves the issue. (Refer to GitHub issue #62 for updates).
When writing `async def` tests, ensure all async operations are part of the same event loop. `async-asgi-testclient` itself runs within the test's event loop, so this error is less likely with this specific library unless other async components introduce a separate loop. If using other `TestClient` implementations, switch to an `AsyncClient` from `httpx` with `ASGITransport` if you need to perform multiple async operations within the same test function.
This is an active issue. Possible workarounds might involve refactoring routing to be more direct, or ensuring the ASGI application passed to `TestClient` is the final, fully configured app. Check GitHub issues for `async-asgi-testclient` for status or community-provided solutions.
This is an open issue. Ensure you are on a version of `async-asgi-testclient` that explicitly supports your Starlette version for streaming. If the problem persists, check the GitHub repository for updates or specific version recommendations.
This is an open issue. When testing WebSocket rejections, examine the ASGI application's logic for rejecting connections and the `TestClient`'s response. You might need to inspect lower-level attributes of the response object or application logs for more details.
No dependency data recorded yet.