Async Tests — pytest-asyncio & AsyncClient — Concept Check
Questions on Async Tests — pytest-asyncio & AsyncClient. httpx.AsyncClient over ASGITransport calls the app inside a real event loop — required when tests must await alongside requests (async DB checks, gather, WebSockets). pytest-asyncio's loop-scope config is the gotcha.
Questions in this quiz
8 questions on Async Tests — pytest-asyncio & AsyncClient — Concept Check. Work through them here, then start the quiz to answer them and see your score.
1.You are testing an async function that seeds a database and then makes a request to an endpoint. Which client should you use?
- ASynchronous HTTP client
- BWebSocket client
- CAsyncClient with ASGITransport
- DTestClient
2.While running a test, you encounter 'Future attached to a different loop' error. What is the likely cause?
- AAsync fixtures and tests are not sharing the same event loop
- BWebSocket connections are not closed
- CThe database is not seeded properly
- DThe test is not using AsyncClient
3.You need to test concurrent requests to catch race conditions. Which tool should you use?
- AAsyncClient without gather
- BTestClient
- CSynchronous HTTP client
- DAsyncClient with asyncio.gather
4.A WebSocket test requires driving full conversations. Which client is appropriate?
- ATestClient with websocket_connect
- BSynchronous HTTP client
- CAsyncClient without ASGITransport
- DAsyncClient with ASGITransport
5.What happens if you use TestClient for async database seeding?
- AThe test will succeed but run slower
- BThe test will pass without issues
- CThe test will hang indefinitely
- DThe test will fail because TestClient cannot await
6.A developer insists that TestClient can handle async operations if used correctly. What is the flaw in this belief?
- ATestClient is simpler to configure
- BTestClient supports WebSockets
- CTestClient is faster than AsyncClient
- DTestClient cannot await async operations
7.You are testing a plain endpoint for validation and authentication. Which client should you use?
- ASynchronous HTTP client
- BTestClient
- CWebSocket client
- DAsyncClient
8.A test using AsyncClient is failing due to 'attached to a different loop' error. What should you check first?
- ACheck if WebSocket connections are closed
- BVerify database seeding
- CSwitch to TestClient
- DEnsure loop_scope is configured correctly