test_router_nested_lifespan_state() — fastapi Function Reference
Architecture documentation for the test_router_nested_lifespan_state() function in test_router_events.py from the fastapi codebase.
Entity Profile
Dependency Diagram
graph TD 6bb61c99_83cc_3075_d0ff_846b336dd9a0["test_router_nested_lifespan_state()"] 05561238_1d26_3221_b177_440da9d6e710["test_router_events.py"] 6bb61c99_83cc_3075_d0ff_846b336dd9a0 -->|defined in| 05561238_1d26_3221_b177_440da9d6e710 style 6bb61c99_83cc_3075_d0ff_846b336dd9a0 fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
tests/test_router_events.py lines 115–172
def test_router_nested_lifespan_state(state: State) -> None:
@asynccontextmanager
async def lifespan(app: FastAPI) -> AsyncGenerator[dict[str, bool], None]:
state.app_startup = True
yield {"app": True}
state.app_shutdown = True
@asynccontextmanager
async def router_lifespan(app: FastAPI) -> AsyncGenerator[dict[str, bool], None]:
state.router_startup = True
yield {"router": True}
state.router_shutdown = True
@asynccontextmanager
async def subrouter_lifespan(app: FastAPI) -> AsyncGenerator[dict[str, bool], None]:
state.sub_router_startup = True
yield {"sub_router": True}
state.sub_router_shutdown = True
sub_router = APIRouter(lifespan=subrouter_lifespan)
router = APIRouter(lifespan=router_lifespan)
router.include_router(sub_router)
app = FastAPI(lifespan=lifespan)
app.include_router(router)
@app.get("/")
def main(request: Request) -> dict[str, str]:
assert request.state.app
assert request.state.router
assert request.state.sub_router
return {"message": "Hello World"}
assert state.app_startup is False
assert state.router_startup is False
assert state.sub_router_startup is False
assert state.app_shutdown is False
assert state.router_shutdown is False
assert state.sub_router_shutdown is False
with TestClient(app) as client:
assert state.app_startup is True
assert state.router_startup is True
assert state.sub_router_startup is True
assert state.app_shutdown is False
assert state.router_shutdown is False
assert state.sub_router_shutdown is False
response = client.get("/")
assert response.status_code == 200, response.text
assert response.json() == {"message": "Hello World"}
assert state.app_startup is True
assert state.router_startup is True
assert state.sub_router_startup is True
assert state.app_shutdown is True
assert state.router_shutdown is True
assert state.sub_router_shutdown is True
Domain
Subdomains
Defined In
Source
Frequently Asked Questions
What does test_router_nested_lifespan_state() do?
test_router_nested_lifespan_state() is a function in the fastapi codebase, defined in tests/test_router_events.py.
Where is test_router_nested_lifespan_state() defined?
test_router_nested_lifespan_state() is defined in tests/test_router_events.py at line 115.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free