REST-API test automation using Python with tavern-ci — Part 2

when you are looking for a complete python solution to your REST API testing needs

Imran Ali
Level Up Coding

--

This article is a continuation of the article I submitted about same time last year. Can’t believe its been an year now, so I wanted to go ahead and finish the series on this topic.

https://taverntesting.github.io/

What topic will I cover this time?

I want to take a different approach this time around and try to solve a common problem that is encountered when setting up an API automation solutions on real projects. The solution to the problem as it turns out would be the exciting feature of tavern that you would also get to learn about. So let’s dive into it

So what’s this problem?

When you are running your API test suites you are generally trying to mock a user that interacts with your application. Part of that involves creating some object that is stored in the application database. Databases generally have some unique constraints to avoid duplication of records. Trying to insert another record of same type could result in SQL IntegrityError. This is a problem because your tests use some sort of test data. Since that test data doesn’t change your tests start failing because the database keeps rejecting them

One solution..

Make sure the database tables are cleaned up before running your tests suites to make sure you are starting with a clean slate. How would you do that with tavern? The answer is with the help of pytest fixtures. Lets work through an example

Example

Let’s say we have a tavern test that has three stages:-

  1. login
  2. posts a number (this number is added to the DB)
  3. gets the number (number is read back from the DB) and verifies it matches what was sent in step 2

the test would look something like this

If you try to run the test the second time, it could fail because of a possible constraint on the DB table.

pytest-fixture to the rescue..

create a python function that connects to the database and cleans up the table. An example shown below. Using the decorator pytest.fixture on this function makes it available to be used in your tavern test

Now modify the tavern test and use the fixture as shown below

What happens now is every time you run the tavern tests, the fixture will be run first and ensure that the DB table is clean. The tests can then proceed as normal. This way you don’t have to change your test data and can run the tests over and over again

Conclusion

Tavern being a pytest plugin has access to all the bells and whistles of pytest and its massive plugin ecosystem. The neat integration of pytest features within your tavern test must now be obvious to you. As a side note, the example of cleaning up database could also be done using DELETE endpoints instead of modifying the database directly. It just depends on what solution you prefer

PS: If you are interested in learning API test automation in depth with realtime projects you can check out my udemy course

Resources

https://taverntesting.github.io/

--

--