Here is a simple code snippet to demonstrate the problem, it seems that the fixture client is not properly injected into the testing functions.
from contextlib import asynccontextmanager
from fastapi import FastAPI
import asyncio
from httpx import AsyncClient
import pytest
@asynccontextmanager
async def lifespan(app: FastAPI):
print("starting up")
yield
print("shutting down")
app = FastAPI(lifespan=lifespan)
@app.get("/hello")
async def hello():
await asyncio.sleep(2)
return {"hello": "world"}
@pytest.fixture(scope="session")
async def client():
async with lifespan(app) as test_app:
async with AsyncClient(app=test_app, base_url="http://localhost") as client:
yield client
@pytest.mark.asyncio
async def test_hello(client):
response = await client.get("/hello")
assert response.json() == {"hello": "world"}
Running pytest will produce the following error message
client = <async_generator object client at 0x00000175B3EA1990>
@pytest.mark.asyncio
async def test_hello(client):
> response = await client.get("/hello")
E AttributeError: 'async_generator' object has no attribute 'get'