Skip to content

xtr_http_kernel.testing

Swapping services in a served application, for the span of a test.

Every start of the application's lifespan boots a compiled kernel — the one the setup call built, then a fresh one each time; :func:override_services parks a mapping on the application for each start to apply before it boots, so every boot hook and every route already sees the replacements. Enter the block before starting the application's lifespan.

override_services

override_services(app: FastAPI, overrides: Mapping[type | tuple[type, Hashable], object]) -> Generator[None]

Serve app with overrides while the block is entered.

A key is a type, or a (type, qualifier) pair for a qualified service — the same keys apply_overrides takes. The mapping applies to every kernel built while the block is entered; on exit the previous overrides, if any, are restored::

with override_services(app, {Mailer: FakeMailer()}):
    ...  # every application life started here sees the fake

Parameters:

  • app (FastAPI) –

    The application the setup call serves.

  • overrides (Mapping[type | tuple[type, Hashable], object]) –

    What to replace, keyed by type or (type, qualifier).