liveBook · Manning

Schemathesis

As explained in Secure APIs

Schemathesis is an open-source API testing tool that generates property-based tests from API schemas.

Schemathesis is a Python API testing tool that reads an API specification and dynamically generates property-based test cases. It checks whether a live server conforms to its contract while probing malformed, random, and potentially malicious inputs. section 12.3

Use Schemathesis to:

  • Detect API drift, such as documented endpoints or parameters that the implementation does not support. section 12.3
  • Expose crashes, unexpected responses, schema violations, and ignored authentication through automated checks. section 12.3
  • Run contract tests continuously after design validation, while adding separate unit tests for business logic and access control. section 12.6

Run tests against a live API

Install Schemathesis with `uv add --dev schemathesis` or `pip install schemathesis`, start the server, then run `schemathesis run openapi.json --base-url http://localhost:8000 --experimental=openapi-3.1 -H 'Authorization: Bearer <token>' --checks=all`. A generated case containing a NUL byte in `phone_number` produced a database error, revealing an unhandled input path. The failure can then be fixed and retested. section 12.3

Figure 12.4 API drift is the difference between the API specification and the implementation. In this example, the specification documents an orphan endpoint, GET /courses/{course_id}/details, that is not implemented, so it responds with 404 when users send a request to that endpoint.
figure

API drift appears when the specification documents `GET /courses/{course_id}/details` but the server returns 404 because the endpoint is not implemented. section 12.3

How Secure APIs covers it

How deep it goes
Generates malformed cases and checks responses, but stops short of testing business-layer behavior.
Examples
Exercises course endpoints and student registration, including malformed NUL-byte input that triggers a database error.
What it assumes
Assumes an API specification, a running server, and access tokens for authenticated endpoints.
What you can do after
Apply it via Schemathesis to discover API drift, malformed-input failures, schema violations, and authentication weaknesses.
How it connects
Connects API schemas, contract testing, fuzzing, API linting, and unit tests for access control and business flows.

Also explained in

Compare how 2 books explain Schemathesis →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Secure APIs. The section links point to the parts of the book it draws on.