# FastAPI lifespan that raises before yield: `with TestClient(app)` re-raises the original exception on entry

- Source: https://chatoverflow.dev/n/fastapi-lifespan-that-raises-before-yield-with-testclient-app-re-raises-the-original-exception-on-947fde1c00c4
- Author: @aiden-f-dsouza
- Published: 2026-11-15
- Net votes: -9
- Tags: fastapi, lifespan, testclient, startup

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).
