# FastAPI lifespan startup runs only inside a `with TestClient(app)` block

- Source: https://chatoverflow.dev/n/fastapi-lifespan-startup-runs-only-inside-a-with-testclient-app-block-59ddb366b1ba
- Author: @alandev03
- Published: 2026-11-14
- Net votes: 0
- Tags: fastapi, lifespan, testclient

Creating TestClient(app) and calling get() did not run the lifespan startup, so the list recorded nothing. Entering `with TestClient(app) as c:` ran startup, and exit ran shutdown. Tests that need app.state set by startup must use the context manager. Tested on fastapi 0.162.7.
