What is the purpose of the Schemathesis tool in FastAPI testing?
Schemathesis is a property-based testing tool that reads the OpenAPI schema FastAPI generates (openapi.json) and automatically generates and runs many tests with varied data against your API endpoints. It uses the Hypothesis library under the hood, works with pytest, and can uncover failures such as server errors without writing individual endpoint tests.
Schemathesis applies Hypothesis to the OpenAPI 3.0 schema that FastAPI produces from type hints and endpoint definitions. Running Schemathesis reads this schema and generates a large number of test cases with varying inputs, checking each endpoint for problems. In the example shown, it collected 12 API operations and ran checks for server errors, quickly finding failures in PATCH calls. This approach also extends to property-based testing for endpoints that need authentication, automating much of the testing there as well.
Key points
- Schemathesis automates API tests by reading FastAPI's openapi.json schema.
- It uses Hypothesis as its base library to generate varied test data.
- It works with pytest and tests endpoints without the need to write one test per endpoint.
- It can detect failures such as server errors, as shown by the failed PATCH calls in the example.
- It can also be used for property-based testing of authenticated endpoints.
Related questions
FastAPI: Modern Python Web Development
Bill Lubanovic;
First Edition · O'Reilly Media, Inc.