Hooks Reference
Schema-Level Hooks
Hook | Signature | When / Why |
---|---|---|
before_load_schema | (context, raw_schema: dict) -> None |
Modify the raw schema before parsing (e.g., inject const values, patch syntax). |
after_load_schema | (context, schema: BaseSchema) -> None |
Inspect or mutate the loaded schema object (e.g., add links, override formats). |
before_process_path | (context, path: str, methods: dict) -> None |
Tweak a specific endpoint’s definition (e.g., set parameter defaults from your DB). |
before_init_operation | (context, operation: APIOperation) -> None |
Adjust an APIOperation before building strategies (e.g., restrict enum choices, set defaults). |
before_add_examples | (context, examples: list[Case]) -> None |
Append explicit Case instances before the examples phase. |
Data-Generation Hooks
Request-Lifecycle Hooks
Mutate or inspect the test case and HTTP request flow.
Hook | Signature | When / Why |
---|---|---|
before_call | (context, case: Case) -> None |
Right before sending the HTTP request; modify case (e.g., add headers, tweak query). |
process_call_kwargs | (context, case: Case, kwargs: dict) -> dict |
Alter keyword args to case.call(**kwargs) (e.g., disable redirects, set timeouts). |
after_call | (context, case: Case, response: Response) -> None |
Immediately after a successful call; inspect or mutate response before built-in checks. |
Conditional Application
Apply hooks only to matching API operations:
@schemathesis.hook.apply_to(path_regex=r"^/users/")
.skip_for(method="POST", tag="admin")
def map_headers(context, headers):
headers = headers or {}
headers["X-User-Test"] = "true"
return headers
- apply_to(...): target operations matching all given conditions (AND logic).
- skip_for(...): exclude operations matching any given conditions (OR logic).
Supported filters: path
, path_regex
, method
, name
, tag
, operation_id
(accepts string, list, or <field>_regex
).