Module

testing.assertions

Fragment and htmx assertion helpers for chirp tests.

Convenience functions to verify response content in fragment-based applications and inspect htmx response headers. Each assertion produces a clear error message on failure.

Functions

assert_is_fragment 2 None
Assert the response is a fragment (has content, no full page wrapper). Checks …
def assert_is_fragment(response: Response, *, status: int = 200) -> None

Assert the response is a fragment (has content, no full page wrapper).

Checks that the response has the expected status and does not contain<html> / </html>tags that indicate a full page.

Parameters
Name Type Description
response Response
status int Default:200
assert_no_full_document 1 None
Assert an htmx response did not accidentally return a full HTML document.
def assert_no_full_document(response: Response) -> None
Parameters
Name Type Description
response Response
assert_is_full_page 2 None
Assert the response is a full page document.
def assert_is_full_page(response: Response, *, status: int = 200) -> None
Parameters
Name Type Description
response Response
status int Default:200
assert_has_id 2 None
Assert the response body contains an element with the given id.
def assert_has_id(response: Response, element_id: str) -> None
Parameters
Name Type Description
response Response
element_id str
assert_fragment_contains 2 None
Assert the fragment response body contains the given text.
def assert_fragment_contains(response: Response, text: str) -> None
Parameters
Name Type Description
response Response
text str
assert_fragment_not_contains 2 None
Assert the fragment response body does **not** contain the given text.
def assert_fragment_not_contains(response: Response, text: str) -> None
Parameters
Name Type Description
response Response
text str
assert_is_error_fragment 2 None
Assert the response is a chirp error fragment snippet. Error fragments contain…
def assert_is_error_fragment(response: Response, *, status: int | None = None) -> None

Assert the response is a chirp error fragment snippet.

Error fragments contain thechirp-error CSS class and a data-status attribute matching the HTTP status code.

Parameters
Name Type Description
response Response
status int | None Default:None
hx_headers 1 dict[str, str]
Extract all HX-* response headers into a dict. Keys are normalized to canonica…
def hx_headers(response: Response) -> dict[str, str]

Extract all HX-* response headers into a dict.

Keys are normalized to canonical htmx casing (e.g.HX-Push-Url) regardless of whether the response went through the ASGI sender (which lowercases header names per the HTTP spec).

Useful for quick inspection in tests::

headers = hx_headers(response)
assert headers["HX-Redirect"] == "/dashboard"
Parameters
Name Type Description
response Response
Returns
dict[str, str]
assert_hx_redirect 2 None
Assert the response contains an ``HX-Redirect`` header with the given URL.
def assert_hx_redirect(response: Response, url: str) -> None
Parameters
Name Type Description
response Response
url str
assert_hx_trigger 3 None
Assert the response triggers an htmx client-side event.
def assert_hx_trigger(response: Response, event: str | dict[str, Any], *, after: str | None = None) -> None
Parameters
Name Type Description
response Response

The HTTP response to check.

event str | dict[str, Any]

The event name (string) or event dict to match.

after str | None

If"settle" or "swap", checks the corresponding HX-Trigger-After-Settle or HX-Trigger-After-Swap header instead of HX-Trigger.

Default:None
assert_hx_retarget 2 None
Assert the response contains an ``HX-Retarget`` header.
def assert_hx_retarget(response: Response, selector: str) -> None
Parameters
Name Type Description
response Response
selector str
assert_hx_reswap 2 None
Assert the response contains an ``HX-Reswap`` header.
def assert_hx_reswap(response: Response, strategy: str) -> None
Parameters
Name Type Description
response Response
strategy str
assert_hx_push_url 2 None
Assert the response contains an ``HX-Push-Url`` header.
def assert_hx_push_url(response: Response, url: str) -> None
Parameters
Name Type Description
response Response
url str
assert_status 2 None
Assert the response has the expected HTTP status code.
def assert_status(response: Response, status: int) -> None
Parameters
Name Type Description
response Response
status int
assert_oob_targets 2 None
Assert the response contains OOB swap elements for each target ID. Checks that…
def assert_oob_targets(response: Response, *target_ids: str) -> None

Assert the response contains OOB swap elements for each target ID.

Checks that the response body includeshx-swap-oobattributes targeting the given element IDs — the pattern produced byOOB() return values.

Usage::

response = await client.post("/save")
assert_oob_targets(response, "item-row", "count")
Parameters
Name Type Description
response Response
*target_ids str
assert_mutation_redirect 3 None
Assert the response is a mutation redirect (non-htmx POST result). Checks for …
def assert_mutation_redirect(response: Response, url: str, *, status: int = 303) -> None

Assert the response is a mutation redirect (non-htmx POST result).

Checks for a 303 (or custom status) redirect to the given URL — the pattern produced byMutationResultfor non-htmx requests.

Usage::

response = await client.post("/save")
assert_mutation_redirect(response, "/items")
Parameters
Name Type Description
response Response
url str
status int Default:303
assert_mutation_fragments 2 None
Assert the response is an htmx mutation with OOB fragments. Checks for a 200 s…
def assert_mutation_fragments(response: Response, *target_ids: str) -> None

Assert the response is an htmx mutation with OOB fragments.

Checks for a 200 status (htmx inline swap) and verifies the expected OOB swap targets are present — the pattern produced by MutationResultwith fragments for htmx requests.

Usage::

response = await client.post("/save", headers=hx_request_headers)
assert_mutation_fragments(response, "item-row", "count")
Parameters
Name Type Description
response Response
*target_ids str