HOME | Blog | YouTube | LinkedIn | About Me         || Calculators    | SoftEng/Tech Posts    | Code/Scripts

Monday, September 5, 2016

API Testing with Python - my simple template - A quick update

There are different automation testing frameworks that you can pick to do your API testing. I wanted a side home project, and I decided to keep things as simple as possible and put together a very simple template using Python alone and its requests library.

It is not using the Robot framework. It is just plain Python code with the following capabilities:


  • Main program that invokes a list of test suites that individual Python modules
  • Each test suite Python file calls a list of test cases that are individual Python modules
  • Each test suite has ability to pre-load a bunch of test data before calling test cases, and then it has ability to delete the test data that was created.
  • Each test case is a Python module that does the following:
    • gets the test/input data from a JSON file
    • makes an http request (get/post/put) call to a given microservice
    • gets the expected data from a JSON file and compares to the results from http request call
    • has ability to compare the HTTP Status results, the count and the actual data.

The test cases are NOT written in Gherkin format. The test cases are just simple Python code that any IT person would be able to write. You just need a few minutes to define your input JSON, expected JSON and a few lines of Python code if you want to compare specific fields. Once you write one test case, it is very mechanical after that. I am purposely keeping it at the level where you need to write a bit of Python code for your test cases, but it is a template code. If I make more configureable then it would slowly be turning into a framework and that's exactly what I do NOT want.

I have a working POC, but I am not ready to share it yet as I want to make it more robust and more elegant. In the meantime, I can just show you an example of a simple test case that talks to a customer service to create a customer and to verify that the data is correctly created:

https://docs.google.com/document/d/1plYZc8N7ux2KA88fJi7p5NdBSv7fKi7YOFyxzL4nHuE/edit?usp=sharing

Stay tuned. 


Almir M.