liveBook · Manning

Schemathesis

As explained in Automating API Delivery

Schemathesis is an open-source tool that generates and executes property-based tests for web APIs from API schemas such as OpenAPI definitions.

Schemathesis generates and executes property-based tests from an API definition, avoiding hand-written tests for every endpoint and response type. It exercises happy and sad paths against an API gateway or service while checking whether responses conform to the published schema. section 7.4 section 7.4.1

Listing 7.3 Running Schemathesis
st run     #1
 --checks all     #2
 --contrib-openapi-formats-uuid     #3
 --cassette-path cassette.yaml     #4
 --data-generation-method all     #5
 --base-url http://localhost:8000     #6
 --header "X-API-Key: my_secret_api_key"     #7
 product-catalog-v1-0.oas.yaml     #8

Run the Schemathesis CLI with all checks, broad data generation, a base URL, authentication, cassette recording, and the OpenAPI definition. section 7.4.1

A typical workflow includes:

  • Start the API gateway and API service that will receive generated requests. section 7.4.1
  • Run the CLI against the OpenAPI definition, selecting checks, data-generation methods, a base URL, and required headers. section 7.4.1
  • Record requests in a cassette file so generated traffic can be inspected and retained. section 7.4.1
  • Use examples in the OpenAPI document deliberately: Schemathesis can send those values, making realistic examples useful for both documentation consumers and automated testing. section 7.4.1

Finding a product-catalog schema defect

Against a product-catalog API, Schemathesis used the documented productId UUID and produced a failing response-schema check: additional properties numberOfReviews and reviewRating were not allowed. A summary can distinguish successful checks for server errors, status codes, content types, headers, and response schemas, while the cassette preserves the network log. section 7.4.1

Schemathesis is written in Python and builds on Hypothesis, but targets APIs described by OpenAPI documents and can also test GraphQL APIs. It is especially useful for external or public APIs where consumer-driven contract tests cannot be applied; generated schema tests reduce maintenance when the API changes. section 7.4.1 section 7.8

How Automating API Delivery covers it

How deep it goes
Walks through CLI execution, data generation, request recording, documented examples, and interpretation of failures and summaries.
Examples
Uses a Product Catalog API, an OpenAPI YAML definition, a CLI command, a cassette, and response-schema failure output.
What it assumes
Assumes an available API gateway or service and an OpenAPI definition, while introducing the generated schema-testing workflow.
What you can do after
Diagnose API schema defects and automate happy- and sad-path conformance checks with the Schemathesis CLI.
How it connects
Connects property-based testing, fuzz testing, OpenAPI, GraphQL, schema testing, and consumer-driven contract testing.

Also explained in

Compare how 2 books explain Schemathesis →

Related concepts

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