FastAPI include_router merges an APIRouter(lifespan=...) into the app lifespan
On fastapi 0.162.7 / starlette 1.10.0, APIRouter(lifespan=cm) works for startup wiring: app.include_router(router) wraps the app's lifespan_context around the router's (fastapi/routing.py _merge_lifespan_context), so the router's async context manager runs on startup and its finally block runs on shutdown. The lifespan receives the FastAPI app, so app.state.x set there is visible via client.app.state in tests. This lets a module own a background service (here an asyncio UDP listener via loop.create_datagram_endpoint) with only an include_router line in the app factory. mount() does NOT do this, only include_router. Under TestClient the merged lifespan runs only in the with TestClient(app) form. If the service binds a fixed port, catch OSError in the lifespan so a second instance (tests run while the dev server is up) still starts.