Auto‑Generate API Test Cases with Qwen AI and Python

This guide shows how to use the Qwen large language model to automatically generate pytest‑style API test scripts from an OpenAPI specification, covering setup, prompt design, model invocation, code extraction, and execution steps.

Test Development Learning Exchange
Test Development Learning Exchange
Test Development Learning Exchange
Auto‑Generate API Test Cases with Qwen AI and Python

In software testing, writing interface automation test cases is repetitive and time‑consuming, requiring reading API docs, building request parameters, writing assertions, and handling edge cases. By leveraging a large language model such as Alibaba Cloud's Qwen, developers can automate the generation of these test scripts.

Core Workflow

The process consists of four steps:

Prepare the API definition, typically an OpenAPI (Swagger) YAML or JSON file describing paths, parameters, request bodies, and responses.

Craft a prompt that tells the AI what kind of test code is needed.

Call the large‑model API, sending the OpenAPI spec and prompt, and receive generated code.

Save the returned code to a file, perform a quick review, and run it.

Prerequisites

Install the required Python packages: pip install dashscope pyyaml Obtain an API key from the Alibaba Cloud Baichuan platform and set it as an environment variable (recommended) or embed it directly for testing:

export DASHSCOPE_API_KEY="YOUR_API_KEY"   # Linux/macOS
$env:DASHSCOPE_API_KEY = "YOUR_API_KEY"   # Windows PowerShell

Sample OpenAPI Document

Consider a simple user‑creation endpoint defined in openapi.yaml:

openapi: 3.0.0
info:
  title: User Service
  version: 1.0.0
paths:
  /api/v1/users:
    post:
      summary: Create user
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                name:
                  type: string
                  example: "张三"
                age:
                  type: integer
                  minimum: 0
                  maximum: 150
                email:
                  type: string
                  format: email
              required: [name, email]
      responses:
        '201':
          description: Created successfully
          content:
            application/json:
              schema:
                type: object
                properties:
                  code:
                    type: integer
                  message:
                    type: string
        '400':
          description: Bad request

This spec indicates the endpoint /api/v1/users uses POST, requires name and email, and optionally accepts age (0‑150).

Generating the Test Script

Create generate_tests.py with the following logic:

import os
import yaml
from dashscope import Generation

# 1. Read OpenAPI spec
with open("openapi.yaml", "r", encoding="utf-8") as f:
    openapi_spec = f.read()

# 2. Build prompt
prompt = f"""
You are a professional test engineer. Based on the OpenAPI definition below, generate a pytest‑style test file.
Requirements:
- Use the requests library
- Base URL: "http://localhost:8000"
- Cover these scenarios:
  a) Successful user creation (all fields valid)
  b) Missing required field name
  c) Age less than 0
  d) Invalid email format
- Each test function should start with test_
- Output only Python code, no explanations or comments
OpenAPI definition:
{openapi_spec}
"""

# 3. Call Qwen model
api_key = os.getenv("DASHSCOPE_API_KEY")
if not api_key:
    raise ValueError("Please set DASHSCOPE_API_KEY environment variable")
response = Generation.call(
    model="qwen-max",
    prompt=prompt,
    api_key=api_key,
    temperature=0.2
)

# 4. Save generated code
if response.status_code == 200:
    generated_code = response.output.text.strip()
    # Remove possible markdown fences
    if generated_code.startswith("```"):
        generated_code = generated_code.split("
", 1)[1]
    if generated_code.endswith("```"):
        generated_code = generated_code.rsplit("
", 1)[0]
    with open("test_generated.py", "w", encoding="utf-8") as f:
        f.write(generated_code)
    print("Test cases generated: test_generated.py")
else:
    print(f"Call failed: {response.code} - {response.message}")

Running and Verifying

Execute the generator: python generate_tests.py If successful, test_generated.py will contain pytest functions such as:

import requests
BASE_URL = "http://localhost:8000"

def test_create_user_success():
    data = {"name": "张三", "age": 25, "email": "[email protected]"}
    resp = requests.post(f"{BASE_URL}/api/v1/users", json=data)
    assert resp.status_code == 201
    assert resp.json()["code"] == 0

def test_create_user_missing_name():
    data = {"age": 25, "email": "[email protected]"}
    resp = requests.post(f"{BASE_URL}/api/v1/users", json=data)
    assert resp.status_code == 400

def test_create_user_age_negative():
    data = {"name": "李四", "age": -1, "email": "[email protected]"}
    resp = requests.post(f"{BASE_URL}/api/v1/users", json=data)
    assert resp.status_code == 400

def test_create_user_invalid_email():
    data = {"name": "王五", "email": "invalid-email"}
    resp = requests.post(f"{BASE_URL}/api/v1/users", json=data)
    assert resp.status_code == 400

Install test dependencies and run the tests:

pip install requests pytest
pytest test_generated.py -v

Important Considerations

Quality Assurance: AI‑generated code may contain syntax errors or logical gaps; always review manually before adding to a production test suite.

Security & Privacy: Do not send sensitive API specifications to public AI services; consider self‑hosted models for confidential data.

Cost Management: Large‑model APIs are billed per call; use them for bulk generation after interface changes rather than for every test run.

Extensibility: Adjust the prompt to request data‑driven tests, unique IDs, automatic cleanup, or other advanced features.

Conclusion

By following this method, developers can quickly produce baseline API test cases with AI, freeing time to focus on complex scenarios and business‑logic testing, thereby improving coverage and development efficiency. The key lies in crafting clear prompts; as prompts improve, so does the quality of generated code.

PythonAI code generationQwenOpenAPIAPI testingpytest
Test Development Learning Exchange
Written by

Test Development Learning Exchange

Test Development Learning Exchange

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.