Testing REST API

Learn how to test the REST API in Python.

We'll cover the following

Writing REST APIs is nice, but writing REST APIs that work is better. That is why you should always write and run tests against those APIs. It can be tedious and feel unrewarding; however, it is always the solid call in the long run.

Running a distributed system requires cooperation between developers, quality control, and operation engineers. It also means that all those involved should be able to make sure that a service is going to be OK. It is important for developers to be able to do rapid prototyping and testing of an idea. It is crucial for quality engineers to be able to validate what is expected from the developed service. It is essential that engineers deploying the service can test that it is deployed and works as expected.

Testing using gabbi

With all of that in mind, the traditional way of writing Python unit and functional tests sounds a bit far from this objective. However, there is a Python tool that is great at solving this problem: it is named Gabbi.

Gabbi is an HTTP testing tool. It allows writing testing scenarios in a declarative YAML-based format. This file format is powerful enough to write all the tests you could use and imagine while staying simple enough so that it is easy to write and maintain. Having an uncomplicated way of writing tests is helpful as it lowers the friction and the barrier of entry when it comes to writing tests. Concretely, this means it is less of a burden for the software engineer to write tests and it is simpler for quality engineers to provide new checks.

To run tests, Gabbi needs a YAML file per scenario to run. A scenario is a sequence of HTTP calls, each one described as an entry, such as shown in the snapshot provided below.

Get hands-on with 1200+ tech skills courses.