DEV Community

Ben Dowen
Ben Dowen

Posted on

Automated REST API Testing with Python

Originally posted: https://www.dowen.me.uk/Automated-rest-api-testing-with-python/

Last time out I introduced you to the start of my journey to automate all the "test" things in Python. I continue that journey now by switching focus away from frontend, to Restful Web APIs. You can look back at my last post here.

Explore, Request, Assert

For this leg of the Journey, I will be using the python modules requests and pyassert.

Exploring APIs

I am a teapot

Before you can make requests and you need to understand the API your testing. For this I recommend using a Rest Client. While you may already have mad skills in cURL, for the rest of us I suggest using Postman, Insomnia or SoapUI.

If, like me, you want to develop your API testing skills in a safe place, outside of any work projects, there are some great options!

Ministry of Testing has a resource page listing some of those options Websites To Practice Testing. The one I am using is Restful Booker by Mark Winteringham.

Start your exploration using API documentation. For Restful booker, you can find that here.

To learn more about testing APIs and using Postman, I suggest the free course Exploring Service APIs through Test Automation by Amber Race.

Making Requests

Inspired by the article API Integration in Python ā€“ Part 1, I started by making a Python client to abstract interactions with the Restful Booker API. The article is not focused on testing, but instead shows us how to Constructing an API Library, using Requests.

This pattern of abstraction is great and we can use it along side an assertion framework to do some robust testing.

You can take a closer look at my API Library for Restful Booker, and my rest code on the Pybooker GitHub Repository.

basic example

This basic example makes a GET request to the URL https://restful-booker.herokuapp.com/booking/1/, and prints the resulting JSON response body into the Python console. Running it gives us:

example output

Because the method returns the response object, we can not only get the JSON body, but also useful information like the HTTP Status code.

Requests can make use of a wide range of HTTP methods, explore it and see what you can do!

Asserting That

OK so now we can make Requests, and access the returned response in an object. With this, we can start implementing some automated checks, to see if we are getting back what we expect.

To do this, I have made use of the module pyassert, you could also use other assertion libraries such as fluentcheck. I am using Pytest to run my tests.

Test Restful Booker

While you can assert on almost every aspect of the response, the most basic check is the response returned 'OK'. This means we made a valid request, and the service didn't throw an internal server error.

One step further would be to assert on the HTTP Status code. For example for a GET you expect to succeed you might expect "200 Success", and for creating a new item with POST you might expect "201 Created".

You can read more about http status codes HTTP STATUS DOGS or HTTP Cats.

Top comments (6)

Collapse
 
hemanthyamjala profile image
Hemanth Yamjala

Hey, Thanks Benjamin for explaining these technical key points so well. I'm sure many of us struggle at some level with this, as the complexities of applications keep growing. By making such a library, automating API testing becomes a cakewalk. In addition to the technical know-hows, it is also imperative to understand how to implement those skills efficiently.
Please read this blog on "Effective practices for API testing" and do share your take on the same. cigniti.com/blog/effective-practic...

Collapse
 
dowenb profile image
Ben Dowen

Thanks for the recommendation. I will take a look.

Collapse
 
testrigtech profile image
TestrigTech • Edited

Hello,

First Big Thanks for explaining these points in brief. It is really helpful for experienced and new testers also. As a software development and testing industry-changing day by day with many new challenges so we need these types of guidance.

Again Thanks

You can also check here some more informative source on software testing: testrigtechnologies.com/blog/

It might be helpful to you...

Collapse
 
dowenb profile image
Ben Dowen

Thanks to a lucky Google ranking, this is my post popular article in terms of views.

I'm considering an update/follow up.

What extra detail would you like to see here? What's missing?

Also feedback, does the article contain anything you found useful? Or did you glance and move on?

If you don't have a Dev account, feel free to Tweet or email me:
@dowenb ben@dowen.me.uk, please use a descriptive subject line!

Collapse
 
anilkulkarni87 profile image
anilkulkarni87

What I would expect is a github repo with framework code. Which may or may not include Api testing, Logging, Results reporting and DB connection. I am confortable with Java Rest Assured but having a working example of Python framework will help me venture into Pyhton Api tetsing.

Collapse
 
rupeshmohanraj profile image
rupeshmohanraj

*Hey Ben Dowen,
*

Thanks for the information I have been reading the forum after couple of years still is been engaging and newbie information's.

As a freelancer I struggled a lot to understand the test automation, I found some sort of blogs that helped me about the Automation best practices it also might be helpful to you (Automation best practices)

Thank you