API Check Automation – DZone

APIs are an integral a part of software program improvement in any small/mid/massive scale software. Because of this testing of those APIs will dramatically enhance the effectivity of your entire software.

There are a number of advantages to API Testing, together with:

  • Catch early errors through the improvement course of
  • Useful in offering full take a look at protection slightly than simply UI testing 
  • Not tightly sure to programming language (API Automation)
  • Takes much less time to check the appliance than UI testing

We’ll now be taught to create an API Check Automation Framework from scratch.

Tech Stack

  • Python
  • Pytest
  • Requests library
  • Attract (Reporting)
  • Any CI/CD device


You will want a primary understanding of REST API, HTTP strategies, and response codes.

Let’s get began!

Step 1

Examine the model of Python put in in your machine:

Step 2

Set up and examine the model of pip in your machine: pip.

Step 3

Set up requests library

Step 4

Creation of Undertaking Structure

Creation of Project Architecture

You may observe the above mission structure.

  • Constants

    This bundle will include the __init__.py file and Constants.py module, the place you’ll be able to outline all of your fixed values. For instance, URL, username, password, tokens, and many others.

  • Anticipated-Responses

    This bundle shall include the anticipated responses for every Service request.
    You may add a number of packages underneath Anticipated-Responses.

  • Payload

    This bundle shall include the payload in JSON/dict format, which you will be passing to the request. The payload may include values you can import from the Fixed.py module created above.

  • Companies

    This bundle shall include the record of service modules. For instance:

    • test_service1.py
    • test_service2.py

The test_service1.py module will include the ‘take a look at circumstances.’

Strategies are outlined as:

def test_<testcasename>:

They’re thought of take a look at circumstances.

For instance:

    def test_service1(self):

        payload_json = json.dumps(payload_service1.knowledge, default=str)
        request = HttpRequest()
        log.data("the API name is : " + self.URL + "<endpoint>")
        headers = "Content material-Kind": "software/json; charset=utf-8", 'token': self.token,
        response = request.send_request_with_data(self.URL + "<endpoint>",
                                                  headers=headers, knowledge=payload_json)
        response_code = response.status_code
        if response_code != 204:
            knowledge = json.hundreds(response.textual content)
            log.data('The Customized standing code is ' + str(knowledge['statusCode']))
            assert int(knowledge['statusCode']) == 200
            expected_response = json.dumps(service1_expected_response.anticipated)
            anticipated = json.hundreds(expected_response)
            for key in knowledge.keys():
                if key not in anticipated:
                    assert False, "Key mismatch"
            log.data('The Customized standing code is ' + str(knowledge['statusCode']))
            assert int(knowledge['statusCode']) == 200

  • conftest.py
    This file accommodates all of the configuration stuff.  For instance, technology of token for API calls. You can use the ‘token’ worth all through your Service calls.
def get_token(request,url):
    payload = 
        "Key": Constants.KEY,
    headers = "Content material-Kind": "software/json; charset=utf-8"

    requests = HttpRequest()
    log.data("The API name is : " + url + "<endpoint>")
    response = requests.send_request_with_data(url + "<endpoint>", json=payload,
                                               headers=headers, redirects=False)
    response_code = response.status_code
    log.data("The standing code is " + str(response_code))
    response = json.hundreds(response.textual content)

    token = response['Token']
    if request.cls shouldn't be None:
        request.cls.token = token
        request.cls.URL = url
    yield token

  • requestHelpers.py
    This module shall include the helper features required for the API requests. You may import the request module to create the helper features. For instance,
class HttpRequest(object):

    session = None

    def __init__(self):
        self.session = requests.session()

    def send_request_with_data(self, url, knowledge=None, json=None, headers=None, redirects=True):
            conn = self.session.submit(url, headers=headers, json=json, knowledge=knowledge, confirm=False, allow_redirects=redirects, timeout=600)
        besides Exception as exception:
            increase exception
        return conn

  • necessities.txt
    This file shall include all of the modules that are a pre-requisite for the creation of this Check automation framework.

Use of Markers:

Markers can be utilized to execute particular units of take a look at circumstances. For instance, if you wish to execute the checks particular to service1, then you’ll be able to apply the customized marker utilizing the above command.

Markers might be executed utilizing the command :

You may register your customized markers by making a pytest.ini file. The contents of the file might be:

markers =
    marker1: description

Step 5


We’ll use the attract reporting device for our API Automation checks.

Observe the below-mentioned steps to arrange the attract reporting:

pip set up allure-pytest

For producing report in customized folder
pytest take a look at.py --alluredir=<path_to_custom_folder>

As soon as the take a look at are executed, you'll be able to go to the Customized Report folder and consider the report utilizing:

attract serve <Report_folder_name>

There’s way more to API take a look at automation than simply creating the framework structure. I am going to write extra concerning the particular insights of every helpful bundle/module on this framework within the upcoming write-ups.