Automate API testing using Schemathesis

How Schemathesis can be a powerful tool to automate and improve your API testing processes.

API testing is a critical aspect of software development. Ensuring that your application’s endpoints and schemas are working correctly is essential for delivering a reliable and robust product. To simplify and enhance the API testing process, tools like Schemathesis have emerged. In this article, we will explore Schemathesis, its features, and how it can help you streamline the testing and documentation of your APIs.

What is Schemathesis?

Schemathesis is an open-source Python tool that tests APIs using OpenAPI, Swagger or GraphQL schemas. It is designed to automate the testing of API endpoints by generating a wide range of test cases and analyzing the API’s behavior based on the provided schema. Schemathesis can be used for both RESTful and GraphQL APIs, making it a versatile solution for API testing.

Explore #LifeAtCapitalOne

Startup-like innovation with Fortune 100 capabilities.

Key features of Schemathesis

Automated test case generation

Schemathesis reads the application’s API schema and automatically generates test cases to cover a wide range of inputs, including edge cases that might not be immediately obvious. This approach significantly reduces the time required for writing tests manually.

Property-based testing

It employs property-based testing strategies to ensure that your application behaves correctly under various conditions. By generating random test data, Schemathesis can uncover unexpected failure modes that fixed datasets might miss.

Fuzz testing

Schemathesis employs a fuzz testing approach, intentionally introducing malformed or invalid data into API requests. This helps identify potential vulnerabilities, security issues and unexpected behaviors.

CLI and Python library

It can be used as a command-line tool, or it can also be integrated into Python test suites as a library. This flexibility allows developers to incorporate Schemathesis into their existing development workflows easily.

Stateful testing

Beyond stateless tests, Schemathesis can perform stateful testing, simulating sequences of requests to test more complex interactions within the API. This is crucial for identifying issues that only arise from specific sequences of operations.

Custom strategies and hooks

While Schemathesis provides robust automatic testing capabilities, it also allows for customization. Developers can define custom strategies for test case generation or hook into the testing process to apply specific checks.

Security testing

It includes features for security testing, such as checking for common vulnerabilities and misconfigurations that attackers could exploit.

Extensive report generation

Schemathesis provides detailed reports highlighting test results, including information about successful test cases and potential issues. This reporting feature makes it easy to understand and address problems within the API.

Compatibility

Schemathesis supports OpenAPI, Swagger and GraphQL schemas. You can easily integrate it with your existing API documentation to start testing without the need to create additional configuration files.

How Schemathesis streamlines API testing

Schemathesis offers the following advantages:

1. Test coverage: Schemathesis generates a wide range of test cases that cover various aspects of the API, including positive and negative scenarios. This ensures that your API is thoroughly tested, and edge cases are considered.

2. Identifying issues early: By running Schemathesis tests during development processes, you can catch issues and discrepancies in your API’s behavior early on. This helps prevent bugs from reaching production.

3. Documentation validation: Schemathesis can be used to validate your API documentation against the actual behavior of the API. This ensures that your documentation accurately reflects how the API works, improving the overall quality of your API documentation.

4. CI/CD integration: Schemathesis can be easily integrated into your CI/CD pipeline, allowing you to automate API testing as part of your development workflow. This helps maintain the reliability of your APIs across different stages of development.


By leveraging these features, Schemathesis helps ensure that web applications are more robust, reliable and secure, with less manual effort required from developers. Its automated, schema-based testing approach is a significant advantage in modern web development, particularly for teams adopting microservices architectures and continuous delivery practices.

Schemathesis demo

Installation

      python -m p install schemathesis

Running

st run https://example.schemathesis.io/openapi.json --report

Report
    
Screenshot of Schemathesis running

Once an error has been identified, the report shows us how to reproduce it. Let’s zoom in one of the outputs:
 

1. Received a response with 5xx status code: 500

      curl -X POST -H 'X-Schemathesis-TestCaseId: 37a7d6352ddd4ad68531429758788022' -d '{"text":\u0080"}' https://example.schemathesis.io/internal-server-errors/improper-unicode-encoding
    

Or add this option to your command line parameters:

      --hypothesis-seed=132877843747217859975749707535751587837
    

This fuzzy testing solution enables Capital One to shift-left and proactively address errors before they impact production. It raises the standard of code quality for our production readiness and enhances our testing framework by leveraging the power of OpenAPI and/or GraphQL specifications.

A powerful tool to improve API testing

Schemathesis is a valuable tool for any development team working with APIs, whether they are RESTful or GraphQL. Its automated testing approach, comprehensive test case generation and integration with popular API schemas make it a powerful choice for improving API quality and reliability. By incorporating Schemathesis into your development and testing processes, you can catch issues early, ensure your API documentation is accurate, and deliver a more robust and dependable API to your users.


Happy testing!

Learn more about tech at Capital One

New to tech at Capital One?


Sergio Bilello, Distinguished Software Engineer

A visionary engineering leader with more than 15 years of experience setting the strategy for highly available, resilient and scalable infrastructure for organizations serving millions of users. Proven track record in building and leading world-class teams to deliver complex, multiyear infrastructure projects from concept to execution. Expert in traffic management, capacity planning and performance engineering, with hands-on experience designing systems for traffic shaping, load testing and cluster management at scale. An exceptional mentor dedicated to hiring, developing and partnering with high-potential and principal-level engineers.

Related Content

Article | December 17, 2024 |3 min read
Capital One associate sitting in Capital One Lounge showing digital wallet on smart phone.
Article | November 6, 2023 |8 min read