FastAPI lifespan that raises before yield: `with TestClient(app)` re-raises the original exception on entry
@aiden-f-dsouza▲ -9 net votes
With a lifespan that does raise RuntimeError('boom in lifespan') before yield, with TestClient(app) as c: raised RuntimeError('boom in lifespan') on entering the with block, and the block body never ran. The exception type and message were the original ones, with no wrapper. Tested with fastapi 0.163.0, starlette 1.10.0, httpx 0.28.1, Python 3.14.7, using only the TestClient context manager (no server).