12 Testing and validating APIs

 

This chapter covers

  • Generating automatic tests for REST APIs using Dredd and Schemathesis
  • Writing Dredd hooks to customize the behavior of your Dredd test suite
  • Using property-based testing to test APIs
  • Leveraging OpenAPI links to enhance your Schemathesis test suite
  • Testing GraphQL APIs with Schemathesis

This chapter teaches you how to test and validate API implementations. Thus far, we’ve learned to design and build APIs to drive integrations between microservices. Along the way, we did some manual tests to ensure our implementations exhibited the correct behavior. However, those tests were minimal, and most importantly, they were purely manual and therefore not repeatable in an automated fashion.

In this chapter, we learn how to run an exhaustive test suite against our API implementations using tools such as Dredd and Schemathesis, tools for API testing that are part of every API developer’s tool kit. Both Dredd and Schemathesis work by looking at the API specification and automatically generating tests against our API server. For an API developer, this is very handy because it means you can focus your efforts on building your APIs instead of testing them.

12.1 Setting up the environment for API testing

12.2 Testing REST APIs with Dredd

12.2.1 What is Dredd?

12.2.2 Installing and running Dredd’s default test suite

12.2.3 Customizing Dredd’s test suite with hooks

12.2.4 Using Dredd in your API testing strategy

12.3 Introduction to property-based testing

12.3.1 What is property-based testing?

12.3.2 The traditional approach to API testing

12.3.3 Property-based testing with Hypothesis

12.3.4 Using Hypothesis to test a REST API endpoint

12.4 Testing REST APIs with Schemathesis

12.4.1 Running Schemathesis’s default test suite

12.4.2 Using links to enhance Schemathesis’ test suite