DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Cypress.io — The Rising Future of Web Automation Testing
  • How Agentic AI Enhances API Testing
  • API Testing With Playwright: A Guide for QA Engineers and Developers
  • Accelerating HCM Cloud Implementation With RPA

Trending

  • Implementing Observability in Distributed Systems Using OpenTelemetry
  • Compliance Automated Standard Solution (COMPASS), Part 10: How OSCAL Mapping Paves the Way for Continuous Compliance Scalability
  • Detecting Plan Regression in SQL Server Using Query Store
  • Architecting Zero-Trust AI Agents: How to Handle Data Safely
  1. DZone
  2. Software Design and Architecture
  3. Integration
  4. API Test Automation

API Test Automation

API test automation framework creation using PyTest.

By 
Tushar Mathur user avatar
Tushar Mathur
·
Dec. 07, 22 · Opinion
Likes (6)
Comment
Save
Tweet
Share
7.0K Views

Join the DZone community and get the full member experience.

Join For Free

APIs are an integral part of software development in any small/mid/large scale application. This means that testing of these APIs will dramatically improve the efficiency of the entire application.

There are several benefits to API Testing, including:

  • Catch early errors during the development process
  • Helpful in providing complete test coverage rather than just UI testing 
  • Not tightly bound to programming language (API Automation)
  • Takes less time to test the application than UI testing

We'll now learn to create an API Test Automation Framework from scratch.

Tech Stack

  • Python
  • Pytest
  • Requests library
  • Allure (Reporting)
  • Any CI/CD tool

Prerequisites

You'll need a basic understanding of REST API, HTTP methods, and response codes.

Let's get started!

Step 1

Check the version of Python installed on your machine:

Step 2

Install and check the version of pip on your machine: pip.

Step 3

Install requests library

 
pip install requests


Step 4

Creation of Project Architecture

Creation of Project Architecture

You can follow the above project architecture.

  • Constants

    This package will contain the __init__.py file and Constants.py module, where you can define all your constant values. For example, URL, username, password, tokens, etc.
  • Expected-Responses

    This package shall contain the expected responses for each Service request.
    You can add multiple packages under Expected-Responses.
  • Payload

    This package shall contain the payload in JSON/dict format, which you'll be passing to the request. The payload might contain values that you can import from the Constant.py module created above.
  • Services

    This package shall contain the list of service modules. For example:
    • test_service1.py
    • test_service2.py

The test_service1.py module will contain the 'test cases.'

Methods are defined as:

def test_<testcasename>:

They are considered test cases.


For example:

Python
 
    def test_service1(self):

        payload_json = json.dumps(payload_service1.data, default=str)
        request = HttpRequest()
        log.info("the API call is : " + self.URL + "<endpoint>")
        headers = {"Content-Type": "application/json; charset=utf-8", 'token': self.token,
                   }
        response = request.send_request_with_data(self.URL + "<endpoint>",
                                                  headers=headers, data=payload_json)
        response_code = response.status_code
        if response_code != 204:
            data = json.loads(response.text)
            log.info('The Custom status code is ' + str(data['statusCode']))
            assert int(data['statusCode']) == 200
            expected_response = json.dumps(service1_expected_response.expected)
            expected = json.loads(expected_response)
            for key in data.keys():
                if key not in expected:
                    assert False, "Key mismatch"
            log.info('The Custom status code is ' + str(data['statusCode']))
            assert int(data['statusCode']) == 200


  • conftest.py
    This file contains all the configuration stuff.  For example, generation of token for API calls. You'll be able to use the 'token' value throughout your Service calls.


Python
 
def get_token(request,url):
    payload = {
        "Key": Constants.KEY,
    }
    headers = {"Content-Type": "application/json; charset=utf-8"}

    requests = HttpRequest()
    log.info("The API call is : " + url + "<endpoint>")
    response = requests.send_request_with_data(url + "<endpoint>", json=payload,
                                               headers=headers, redirects=False)
    response_code = response.status_code
    log.info("The status code is " + str(response_code))
    response = json.loads(response.text)

    token = response['Token']
    if request.cls is not None:
        request.cls.token = token
        request.cls.URL = url
    yield token


  • requestHelpers.py
    This module shall contain the helper functions required for the API requests. You can import the request module to create the helper functions. For example,

Python
 
class HttpRequest(object):

    session = None

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

    def send_request_with_data(self, url, data=None, json=None, headers=None, redirects=True):
        try:
            conn = self.session.post(url, headers=headers, json=json, data=data, verify=False, allow_redirects=redirects, timeout=600)
        except Exception as exception:
            raise exception
        return conn


  • requirements.txt
    This file shall contain all the modules which are a pre-requisite for the creation of this Test automation framework.

Python
 
pytest
requests
pytest-logger
pytest-html
allure-pytest


Use of Markers:

Python
 
@pytest.mark.service1


Markers can be used to execute specific sets of test cases. For example, if you want to execute the tests specific to service1, then you can apply the custom marker using the above command.

Markers can be executed using the command :

Python
 
pytest -m "marker_name"


You can register your custom markers by creating a pytest.ini file. The contents of the file can be:

Python
 
[pytest]
markers =
    marker1: description


Step 5

Reporting

We'll use the allure reporting tool for our API Automation tests.

Follow the below-mentioned steps to set up the allure reporting:

Python
 
pip install allure-pytest

For generating report in custom folder
pytest test.py --alluredir=<path_to_custom_folder>

Once the test are executed, you can go to the Custom Report folder and view the report using:

allure serve <Report_folder_name>


There's much more to API test automation than just creating the framework architecture. I'll write more about the specific insights of each useful package/module in this framework in the upcoming write-ups.

API API testing Test automation

Opinions expressed by DZone contributors are their own.

Related

  • Cypress.io — The Rising Future of Web Automation Testing
  • How Agentic AI Enhances API Testing
  • API Testing With Playwright: A Guide for QA Engineers and Developers
  • Accelerating HCM Cloud Implementation With RPA

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook