Skip to content
api-tester logo

API 测试生成器

api-tester

API 自动化测试 - OpenAPI 解析、测试用例生成、集成测试

SKILL.md

Full skill instructions

API 测试生成器

触发条件

当用户要求测试 API、生成测试用例、接口测试、集成测试时激活此技能。

工作流程

第 1 步:发现 API

按优先级查找 API 定义:

  1. OpenAPI/​Swagger 文件(openapi.yaml、swagger.json)
  2. 路由定义文件(Express routes、Flask blueprints、Spring controllers)
  3. API 目录结构(/​api/、/​routes/、/​controllers/)

OpenAPI 解析方法:

# 使用 swagger-cli 验证
npx @apidevtools/​swagger-cli validate openapi.yaml

# 使用 redocly 提取端点信息
npx @redocly/​cli lint openapi.yaml

# 从 OpenAPI 提取所有路径
grep -E '^\s+/' openapi.yaml | awk '{print $2}'

第 2 步:生成测试用例

为每个 endpoint 生成三类测试:

  • 正常场景(happy path)— 合法输入,期望成功响应
  • 边界值测试 — 空值、超长字符串、特殊字符、极值
  • 错误场景 — 400/​401/​403/​404/​422/​500

第 3 步:生成测试代码

根据项目技术栈选择测试框架并生成代码。

第 4 步:测试数据生成

策略:

策略适用场景工具/方法
固定数据简单接口、确定性测试JSON fixture 文件
Faker 随机生成大量测试数据、压力测试@faker-js/​faker、Faker(Python)
工厂模式复杂对象关系factory_boy、fishery
从 Schema 生成基于 OpenAPI 自动生成json-schema-faker

测试模板

pytest (Python)

import pytest

# 正常场景
def test_get_users_success(client):
    response = client.get("/​api/​users")
    assert response.status_code == 200
    assert isinstance(response.json(), list)

# 边界值测试
def test_create_user_empty_name(client):
    response = client.post("/​api/​users", json={"name": ""})
    assert response.status_code == 422

# 认证测试
def test_get_users_unauthorized(client):
    response = client.get("/​api/​users", headers={})
    assert response.status_code == 401

# 参数化批量测试
@pytest.mark.parametrize("name,expected_status", [
    ("Alice", 200),
    ("", 422),
    ("a" * 500, 422),
    (None, 422),
])
def test_create_user_validation(client, name, expected_status):
    response = client.post("/​api/​users", json={"name": name})
    assert response.status_code == expected_status

Jest (JavaScript/​TypeScript)

import request from 'supertest';
import app from '../​src/​app';

describe('GET /​api/​users', () => {
  it('应返回用户列表', async () => {
    const res = await request(app).get('/​api/​users');
    expect(res.status).toBe(200);
    expect(Array.isArray(res.body)).toBe(true);
  });

  it('未认证应返回 401', async () => {
    const res = await request(app).get('/​api/​users');
    expect(res.status).toBe(401);
  });
});

describe('POST /​api/​users', () => {
  it('空名称应返回 422', async () => {
    const res = await request(app)
      .post('/​api/​users')
      .send({ name: '' });
    expect(res.status).toBe(422);
  });

  it('有效数据应返回 201', async () => {
    const res = await request(app)
      .post('/​api/​users')
      .send({ name: 'Alice', email: '[email protected]' });
    expect(res.status).toBe(201);
    expect(res.body).toHaveProperty('id');
  });
});

curl 测试脚本

#!/​bin/​bash
BASE_URL="http://localhost:3000/​api"

# 正常请求
echo "=== GET /​users ==="
curl -s -w "\nHTTP Status: %{http_code}\n" \
  -H "Authorization: Bearer *** \
  "$BASE_URL/​users"

# POST 创建资源
echo "=== POST /​users ==="
curl -s -w "\nHTTP Status: %{http_code}\n" \
  -X POST \
  -H "Content-Type: application/​json" \
  -H "Authorization: Bearer *** \
  -d '{"name":"Alice","email":"[email protected]"}' \
  "$BASE_URL/​users"

# 错误场景 - 空 body
echo "=== POST /​users (empty) ==="
curl -s -w "\nHTTP Status: %{http_code}\n" \
  -X POST \
  -H "Content-Type: application/​json" \
  -d '{}' \
  "$BASE_URL/​users"

注意事项

  • 测试用例应独立运行,不依赖执行顺序
  • 使用 beforeEach 或 fixture 管理测试数据的初始化和清理
  • 敏感信息(API Key、Token)使用环境变量,不要硬编码
  • 集成测试建议使用独立的测试数据库
  • 关注响应时间,添加超时断言

More Backend & APIs skills

Postgres performance optimization and best practices from Supabase. Use this skill when writing, reviewing, or optimizing Postgres queries, schema designs, or database configurations.

2.7K 431.1K
View
prisma-database-setup logo
Backend & APIs

prisma-database-setup

Step-by-step configuration guides for Prisma ORM across PostgreSQL, MySQL, SQLite, MongoDB, SQL Server, CockroachDB, and Prisma Postgres.

67 336.1K
View
prisma-client-api logo
Backend & APIs

prisma-client-api

Complete Prisma Client API reference for model queries, CRUD operations, filtering, relations, and transactions.

67 336.1K
View
prisma-cli logo
Backend & APIs

prisma-cli

Complete reference for Prisma CLI commands, options, and workflows across setup, migrations, and database operations.

67 333.6K
View
supabase logo
Backend & APIs

supabase

Handles the full Supabase workflow from schema changes to deployment, with built-in security guardrails that catch common traps like RLS...

2.7K 308.9K
View
neon-postgres logo
Backend & APIs

neon-postgres

Comprehensive guides and best practices for Neon Serverless Postgres, covering setup, connection methods, authentication, and platform APIs.

98 216.2K
View
firebase-auth-basics logo
Backend & APIs

firebase-auth-basics

Guide for setting up and using Firebase Authentication. Use this skill when the user's app requires user sign-in, user management, or secure data access using auth rules.

462 163.8K
View

Coding & apps AI tools

Opus Clip logo
Coding & apps

Opus Clip

Opus.ai: Revolutionize Your Web Experience

Free
View
Imagica logo
Coding & apps

Imagica

Build a no-code AI app in minutes.

Freemium
View
E
Coding & apps

Emergent.sh

An IDE for code migration from legacy to modern frameworks through coding agents.

Freemium
View
Wonder Dynamics logo
Coding & apps

Wonder Dynamics

Automate CGI animation in live-action scenes

Paid
View
M
Coding & apps

Mixo

Launch a website in seconds with AI.

Paid
View
AI Code Convert logo
Coding & apps

AI Code Convert

Streamline Your Coding Experience with AI Code Helper

Free
View