Integration Testing APIs with Tavern

TL;DR: We made a Pytest plugin, Python library and CLI tool for testing APIs called Tavern.

Testing APIs has always been a crucial part of our work, both when building Overlock and during our consulting work. We used to spend a lot of time writing tests for Postman, but they were becoming painful to write and maintain. The syntax is verbose, we found it hard to reuse code between tests and we ended up copy-and-pasting library code directly into tests because it doesn’t support code importing. Even simple tasks like validating a JWT were unwieldy and verbose.

Because of this, more often than not API tests were pushed to the back of the queue. In theory errors should be caught by unit tests and integration tests but there are certain types of errors which are very hard to catch without API tests, so they’re important.

Unit testing vs Integration testing

With this in mind we built Tavern, for our own use, which we’ve released as an open-source tool for other developers in the same position as us. It’s a Python library which integrates with Pytest to make API testing as easy as unit testing. The tests are written in a concise YAML based syntax and it’s easy to pull in external functions from Python (including as many libraries as you like!). And because it’s built on YAML it’s easy to reuse parts of your tests to keep files short and readable.

We now use Tavern internally on all our projects and it helps make API testing a core part of our workflow. This means we can be more confident when making changes, ultimately helping us move faster and write better code.

To show you how simple and easy-to-read a Tavern test can be, here’s a simple example:

# minimal_test.tavern.yaml

# Every test file has one or more tests...
test_name: Get some fake data from the JSON placeholder API
# ...and each test has one or more stages (e.g. an HTTP request)
  - name: Make sure we have the right ID

    # Define the request to be made...
      method: GET

    # ...and the expected response code and body
      status_code: 200
        id: 1

This is just a brief overview of what Tavern does and how it works. There’s a lot more that you can do with it, but most importantly the test description syntax and core principles are flexible enough that even with a small set of tools you can do pretty much any kind of API test.

If you’re interested in using Tavern yourself, check out the documentation and examples, and the project is hosted on GitHub for anyone looking to get involved further. As we’re actively using and continuing to develop Tavern we’re able to help with any issues you might have, and if there’s a way you think it could be better we’re always open to suggestions and pull requests.

Please let us know in the comments or on github if Tavern is useful to you or if there are any features you’d like to see!


Overlock is making error tracking for IoT better.

Find out more