Automating API Test Script Generation: From Manual Coding to Tool‑Assisted Production

This article describes the pain points of manual API test script writing, proposes a workflow that parses interface documentation, automatically generates both interface classes and robust test cases using Python and Swagger, and demonstrates significant efficiency gains and future improvement directions.

Hujiang Technology
Hujiang Technology
Hujiang Technology
Automating API Test Script Generation: From Manual Coding to Tool‑Assisted Production

Pain Points

Interface automation test engineers spend a large portion of their time manually creating API classes and test cases, leading to repetitive work, low robustness, and difficulty in refactoring.

Solution Overview

Parse API documentation (Swagger, RAP, Wiki).

Identify scripts that are suitable for automatic generation.

Use a code‑generation tool to produce the scripts.

Document Parsing

For Wiki pages the team uses BeautifulSoup to extract HTML content, while for Swagger they directly parse the json definition.

from bs4 import BeautifulSoup
soup = BeautifulSoup(html_doc)
title_string = soup.title.string
# ... further parsing logic

Code Generation Engine

A lightweight backend generator is implemented in Python:

class CodeGeneratorBackend():
    def __init__(self):
        self.code = []
        self.tab = "\t"
        self.level = 0
    def begin(self, tab="\t"):
        self.code = []
        self.tab = tab
        self.level = 0
    def write(self, string):
        self.code.append(self.tab * self.level + string)
    def indent(self):
        self.level += 1
    def dedent(self):
        if self.level == 0:
            raise SyntaxError("internal error in code generator")
        self.level -= 1
    def end(self):
        return "".join(self.code)

Typical usage:

c = CodeGeneratorBackend()
c.begin(tab="    ")
c.write("def function(self):
")
c.indent()
# generate body
c.dedent()
print(c.end())

Generation Rules

Interface class scripts are generated by iterating over API objects and substituting placeholders.

Test case scripts consider special values (0, None, empty string), type‑based values, pagination parameters, and time range parameters.

Query‑type APIs generate single, combined, and full‑parameter queries; update‑type APIs generate per‑field and bulk updates.

Efficiency Results

For a coupon‑related feature with ~10 new APIs (≈250 parameters) the manual effort was 2 person‑days, while the tool reduced it to under an hour, achieving a 94 % efficiency gain for interface classes and a 50 % gain for robustness test cases, overall 74 % improvement.

Future Work

Expand multi‑parameter test case generation.

Invoke real APIs to capture expected results automatically.

Refine rule definitions through continuous collaboration with developers.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendCode GenerationPythonAutomationSwaggerAPI testing
Hujiang Technology
Written by

Hujiang Technology

We focus on the real-world challenges developers face, delivering authentic, practical content and a direct platform for technical networking among developers.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.