Skip to content

API Tests Quick Start Guide

nilaya123 edited this page May 17, 2018 · 43 revisions

Table of Contents

Sample API Application

For Player-Interface layer based API Automation Framework, we have written a sample API application called cars_app.py using Python Flask. In cars_app.py application we have features related to getting car details, adding cars, updating cars, removing cars, register cars, filter cars etc. We can make the required REST (GET, POST, PUT, DELETE) API calls to use the features.

Usage Example

API automation offers a sweet spot between unit testing and GUI automation. Most web applications these days have RESTful interfaces - lending themselves nicely to API automation. At Qxf2, we use the API automation to perform a lot of the test setup activities. API automation is fast, robust and much easier to maintain than GUI automation. The API automation framework is based on object-oriented approach and easier to maintain. To get setup with API and run your tests you need to do the following things.

Get set with API test framework

Install Flask using command "pip install flask"

For more information related to API testing, you can refer to the links below: https://qxf2.com/blog/easily-maintainable-api-test-automation-framework/ https://qxf2.com/blog/api-testing-developer-tools/ https://qxf2.com/blog/api-testing-python-mechanize/

Write your first API test

Here is the sample test script for writing a test for the Cars app we discussed earlier.

"""
API Test Cases to Player-Interface pattern based Framework will do the following:
    # Test Case for Adding new car details
    # Test Case to check the registration of the new car
"""

import os,sys,time
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from endpoints.API_Player import API_Player
from conf import api_example_conf as conf


def test_api_example(api_url='http://127.0.0.1:5000'):
    "Run api test"
    
    try:

        # Create test object
        test_obj = API_Player(url=api_url)
        expected_pass = 0
        actual_pass = -1

        # set authentication details
        username = conf.user_name
        password = conf.password
        auth_details = test_obj.set_auth_details(username, password)

        # add cars
        car_details = conf.car_details

        result_flag = test_obj.add_car(car_details=car_details,
                                       auth_details=auth_details)
        test_obj.log_result(result_flag,
                            positive='Successfully added new car with details %s' % car_details,
                            negative='Could not add new car with details %s' % car_details)
        
        
        # Get Cars and verify if new car is added
        result_flag = test_obj.get_cars(auth_details)
        result_flag = test_obj.verify_car_count(expected_count=5,
                                                auth_details=auth_details)
        test_obj.log_result(result_flag,
                            positive='Total car count matches expected count',
                            negative='Total car count doesnt match expected count')

        # Update car
        update_car = conf.update_car
        update_car_name = conf.car_name_2
        result_flag = test_obj.update_car(auth_details=auth_details,
                                          car_name=update_car_name,
                                          car_details=update_car)
        test_obj.log_result(result_flag,
                            positive='Successfully updated car : %s' % update_car_name,
                            negative='Couldnt update car :%s' % update_car_name)

        # Get one car details
        new_car = conf.car_name_1
        brand = conf.brand
        result_flag = test_obj.get_car(auth_details=auth_details,
                                       car_name=new_car,
                                       brand=brand)
        test_obj.log_result(result_flag,
                            positive='Successfully fetched car details of car : %s' % new_car,
                            negative='Couldnt fetch car details of car :%s' % new_car)

        # Register car
        customer_details = conf.customer_details
        result_flag = test_obj.register_car(auth_details=auth_details,
                                            car_name=new_car,
                                            brand=brand)
        test_obj.log_result(result_flag,
                            positive='Successfully registered new car %s with customer details %s' % (new_car, customer_details),
                            negative='Couldnt register new car %s with cutomer details %s' % (new_car, customer_details))

        # write out test summary
        expected_pass = test_obj.total
        actual_pass = test_obj.passed
        test_obj.write_test_summary()

    except Exception, e:
        test_obj.write("Exception when trying to run test:%s" % __file__)
        test_obj.write("Python says:%s" % str(e))

    # Assertion
    assert expected_pass == actual_pass
        
if __name__ == '__main__':
    test_api_example()

How to run tests

Follow below steps to run test_api_example test:

  1. Start the cars application using command "python cars-api/cars_app.py"
  2. Run the test script "pytest -k api -s"