Codementor Events

Writing Unit Tests for REST APIs in Python

Published Feb 14, 2018

A little background: over the last few months, I have been contributing to an open source organization named FOSSASIA, where I’m working on a project called BadgeYay. It is a badge generator with a simple web UI to add data and generate printable badges in PDF.

BadgeYay's back-end is now shifted to REST-APIs and to test functions used in REST-APIs, we need some testing technology that will test each and every function used in the API. For our purposes, we chose the popular unit tests Python test suite.

In this blog post, I’ll be discussing how I have written unit tests to test BadgeYay's REST-API.

First, let’s understand what unit tests is and why we have chosen it. Then we will move onto writing API tests for BadgeYay. These tests have a generic structure. Thus, the code I mention would work in other REST API testing scenarios, often with little to no modifications.

Let’s get started and understand API testing step by step.

What is Unit tests?

Unit tests is a Python unit testing framework which supports test automation, the sharing of setup and shutdown code for tests, aggregation of tests into collections, and independence of the tests from the reporting framework. The unit test module provides classes that make it easy to support these qualities for a set of tests.

Why Unit tests?

We get two primary benefits from unit testing, with a majority of the value going to the first:

  • Guides your design to be loosely coupled and well fleshed out. If doing test driven development, it limits the code you write to only what is needed and helps you to evolve that code in small steps.
  • Provides fast automated regression for re-factors and small changes to the code.
  • Unit testing also gives you living documentation about how small pieces of the system work.

We should always strive to write comprehensive tests that cover the working code pretty well.

Now, here is glimpse of how I wrote unit tests for testing code in the REST-API back-end of BadgeYay. Using unit test's Python package and requests modules, we can test REST API in test automation.

Below is the code snippet for which I have written unit tests in one of my pull requests.

def output(response_type, message, download_link): 
      if download_link == ‘’: 
         response = [
         	{ 
                   ‘type’: response_type, 
                   ‘message’: message 
          	}
          ]
       else: 
         response = [
         	{ 
                   ‘type’: response_type, 
                   ‘message’: message, 
                   ‘download_link’: download_link 
          	}
          ]
 
       return jsonify({‘response’: response})

To test this function, I basically created a mock object which could simulate the behavior of real objects in a controlled way, so in this case, a mock object may simulate the behavior of the output function and return something like a JSON response without hitting the real REST API.

Now the next challenge is to parse the JSON response and feed the specific value of the response JSON to the Python automation script. So Python reads the JSON as a dictionary object and it really simplifies the way JSON needs to be parsed and used.

And here’s the content of the backend/tests/test_basic.py file.

#!/usr/bin/env python3
“””Tests for Basic Functions”””
import sys
import json
import unittest

sys.path.append(“../..”)
from app.main import *

class TestFunctions(unittest.TestCase): 
      “””Test case for the client methods.””” 
      def setup(self): 
          app.app.config[‘TESTING’] = True 
          self.app = app.app.test_client() 
      
      # Test of Output function 
      def test_output(self): 
          with app.test_request_context(): 
          # mock object 
          out = output(‘error’, ‘Test Error’, ‘local_host’) 
          # Passing the mock object 
          response = [
          	{ 
                        ‘type’: ‘error’, 
                        ‘message’: ‘Test Error’, 
                        ‘download_link’: ‘local_host’ 
           	}
           ] 
           data = json.loads(out.get_data(as_text=True) 
           # Assert response 
           self.assertEqual(data[‘response’], response)
           
 if __name__ == ‘ __main__ ’: 
      unittest.main()

And finally, we can verify that everything works by running nosetests .


Screen-shot of Test Passing

This is how I wrote unit tests in the BadgeYay repository.

With that, I have reached the end of our discussion on writing unit tests for REST APIs in Python Web Applications. I wrote this post as a solution to this issue in the BadgeYay project. If you liked this post, consider having a look at my other work on GitHub 🙂.

Sources : Unit testing framework

PS: I’m new to blogging, so constructive criticism is not only welcome, but very much wanted!

Discover and read more posts from Parth Shandilya
get started