Home / Function/ test_startup_shutdown_handlers_as_parameters() — fastapi Function Reference

test_startup_shutdown_handlers_as_parameters() — fastapi Function Reference

Architecture documentation for the test_startup_shutdown_handlers_as_parameters() function in test_router_events.py from the fastapi codebase.

Entity Profile

Dependency Diagram

graph TD
  3fb64d48_558f_7b9f_79cb_1445d1b2db13["test_startup_shutdown_handlers_as_parameters()"]
  05561238_1d26_3221_b177_440da9d6e710["test_router_events.py"]
  3fb64d48_558f_7b9f_79cb_1445d1b2db13 -->|defined in| 05561238_1d26_3221_b177_440da9d6e710
  style 3fb64d48_558f_7b9f_79cb_1445d1b2db13 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

tests/test_router_events.py lines 322–379

def test_startup_shutdown_handlers_as_parameters(state: State) -> None:
    """Test that startup/shutdown handlers passed as parameters to FastAPI are called correctly."""

    def app_startup() -> None:
        state.app_startup = True

    def app_shutdown() -> None:
        state.app_shutdown = True

    app = FastAPI(on_startup=[app_startup], on_shutdown=[app_shutdown])

    @app.get("/")
    def main() -> dict[str, str]:
        return {"message": "Hello World"}

    def router_startup() -> None:
        state.router_startup = True

    def router_shutdown() -> None:
        state.router_shutdown = True

    router = APIRouter(on_startup=[router_startup], on_shutdown=[router_shutdown])

    def sub_router_startup() -> None:
        state.sub_router_startup = True

    def sub_router_shutdown() -> None:
        state.sub_router_shutdown = True

    sub_router = APIRouter(
        on_startup=[sub_router_startup], on_shutdown=[sub_router_shutdown]
    )

    router.include_router(sub_router)
    app.include_router(router)

    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

Frequently Asked Questions

What does test_startup_shutdown_handlers_as_parameters() do?
test_startup_shutdown_handlers_as_parameters() is a function in the fastapi codebase, defined in tests/test_router_events.py.
Where is test_startup_shutdown_handlers_as_parameters() defined?
test_startup_shutdown_handlers_as_parameters() is defined in tests/test_router_events.py at line 322.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free