testing.assertions

Page actions AI-ready formats and sharing
Open LLM text
Share with AI
Ask Claude Ask ChatGPT Ask Gemini Ask Copilot

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.

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.

testing.assertions

Name Type Default Description
type
qualified_name
element_type
description
source_file
line_number
is_autodoc
autodoc_element
_autodoc_template
_autodoc_url_path
_autodoc_page_type
title
doc_content_hash

Symbols on this page

function assert_is_fragment

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

Checks that the response has the expected status and does not contain …

Jump to symbol
function assert_no_full_document

Assert an htmx response did not accidentally return a full HTML document.

Jump to symbol
function assert_is_full_page

Assert the response is a full page document.

Jump to symbol
function assert_has_id

Assert the response body contains an element with the given id.

Jump to symbol
function assert_fragment_contains

Assert the fragment response body contains the given text.

Jump to symbol
function assert_fragment_not_contains

Assert the fragment response body does not contain the given text.

Jump to symbol
function assert_is_error_fragment

Assert the response is a chirp error fragment snippet.

Error fragments contain thechirp-error CSS class and a data-statusattribute matching the HTTP status…

Jump to symbol
function hx_headers

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…

Jump to symbol
function assert_hx_redirect

Assert the response contains anHX-Redirectheader with the given URL.

Jump to symbol
function assert_hx_trigger

Assert the response triggers an htmx client-side event.

Timing variants are htmx 2/generic wire assertions only. Htmx 4 removed bothAfterresponse headers, so…

Jump to symbol
function assert_hx_retarget

Assert the response contains anHX-Retargetheader.

Jump to symbol
function assert_hx_reswap

Assert the response contains anHX-Reswapheader.

Jump to symbol
function assert_hx_push_url

Assert the response contains anHX-Push-Urlheader.

Jump to symbol
function assert_status

Assert the response has the expected HTTP status code.

Jump to symbol
function assert_oob_targets

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

Checks that the response body includeshx-swap-oobattributes targeting the given element IDs…

Jump to symbol
function assert_mutation_redirect

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

Checks for a 303 (or custom status) redirect to the given URL — the…

Jump to symbol
function assert_mutation_fragments

Assert the response is an htmx mutation with OOB fragments.

Checks for a 200 status (htmx inline swap) and verifies the expected OOB swap…

Jump to symbol
assert_is_fragment
function
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 Default Description
response Response
status int 200
assert_no_full_document
function
def assert_no_full_document(response: Response) -> None

Assert an htmx response did not accidentally return a full HTML document.

Parameters

Name Type Default Description
response Response
assert_is_full_page
function
def assert_is_full_page(response: Response, *, status: int = 200) -> None

Assert the response is a full page document.

Parameters

Name Type Default Description
response Response
status int 200
assert_has_id
function
def assert_has_id(response: Response, element_id: str) -> None

Assert the response body contains an element with the given id.

Parameters

Name Type Default Description
response Response
element_id str
assert_fragment_contains
function
def assert_fragment_contains(response: Response, text: str) -> None

Assert the fragment response body contains the given text.

Parameters

Name Type Default Description
response Response
text str
assert_fragment_not_contains
function
def assert_fragment_not_contains(response: Response, text: str) -> None

Assert the fragment response body does not contain the given text.

Parameters

Name Type Default Description
response Response
text str
assert_is_error_fragment
function
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 Default Description
response Response
status int | None None
hx_headers
function
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 Default Description
response Response
assert_hx_redirect
function
def assert_hx_redirect(response: Response, url: str) -> None

Assert the response contains anHX-Redirectheader with the given URL.

Parameters

Name Type Default Description
response Response
url str
assert_hx_trigger
function
def assert_hx_trigger(response: Response, event: str | dict[str, Any], *, after: str | None = None) -> None

Assert the response triggers an htmx client-side event.

Timing variants are htmx 2/generic wire assertions only. Htmx 4 removed bothAfterresponse headers, so a passing assertion does not prove browser delivery under the htmx 4 preview; use a browser lifecycle test.

Parameters

Name Type Default 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 None If ``"settle"`` or ``"swap"``, checks the corresponding ``HX-Trigger-After-Settle`` or ``HX-Trigger-After-Swap`` header instead of ``HX-Trigger``.
assert_hx_retarget
function
def assert_hx_retarget(response: Response, selector: str) -> None

Assert the response contains anHX-Retargetheader.

Parameters

Name Type Default Description
response Response
selector str
assert_hx_reswap
function
def assert_hx_reswap(response: Response, strategy: str) -> None

Assert the response contains anHX-Reswapheader.

Parameters

Name Type Default Description
response Response
strategy str
assert_hx_push_url
function
def assert_hx_push_url(response: Response, url: str) -> None

Assert the response contains anHX-Push-Urlheader.

Parameters

Name Type Default Description
response Response
url str
assert_status
function
def assert_status(response: Response, status: int) -> None

Assert the response has the expected HTTP status code.

Parameters

Name Type Default Description
response Response
status int
assert_oob_targets
function
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 Default Description
response Response
*target_ids str
assert_mutation_redirect
function
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 Default Description
response Response
url str
status int 303
assert_mutation_fragments
function
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 Default Description
response Response
*target_ids str

View source · /home/runner/work/chirp/chirp/site/../src/chirp/testing/assertions.py:1