A

API 测试自动化

作者:鹿Sir开发工具v25

基于 Python 的 API 接口测试自动化工具,支持 REST/GraphQL 功能测试、并发性能测试、OpenAPI/Swagger 契约验证与 Mock 服务,并生成测试报告。当用户需要测试 API 接口、做接口自动化、验证接口契约、压测接口或搭建 Mock 服务时使用。触发词:接口测试、API 测试、自动化测试、Mock 服务、性能压测。

下载量
266
点赞
67
价格
免费

技能文档

---
name: api-test-automation
title: API 测试自动化
category: 开发工具
description: 基于 Python 的 API 接口测试自动化工具,支持 REST/GraphQL 功能测试、并发性能测试、OpenAPI/Swagger 契约验证与 Mock 服务,并生成测试报告。当用户需要测试 API 接口、做接口自动化、验证接口契约、压测接口或搭建 Mock 服务时使用。触发词:接口测试、API 测试、自动化测试、Mock 服务、性能压测。
---

# API 测试自动化

## 概述

本技能提供完整的 API 测试解决方案,支持:

- REST API 功能测试(同步/异步请求、重试、拦截器、Cookie/Session 管理)
- GraphQL 查询测试(Query/Mutation、变量、Fragments、内省、订阅测试)
- 性能测试(并发、响应时间、吞吐量、负载与压力测试)
- 契约测试(OpenAPI/Swagger 验证、JSON Schema 校验)
- Mock 服务(路由配置、动态响应)
- 测试报告生成

## 技能工作流

### 步骤1:确认被测服务与测试目标

明确被测 API 的基地址、协议(REST 或 GraphQL)、认证方式与测试范围(功能/性能/契约),缺失 Mock 需求时确认是否需要先搭建 Mock 服务。

### 步骤2:准备环境与依赖

```bash
pip install -r requirements.txt
```

依赖清单见 `requirements.txt`(requests、httpx、pytest、schemathesis、jsonschema 等,完整列表见下方「依赖」章节)。

### 步骤3:编写并执行测试

参照 `examples/run_tests.py` 编写测试用例,或直接在对话中让 Agent 按下述方式调用:

```python
from api_test_automation import RestClient, GraphQLClient, PerformanceTester

# REST API 测试
client = RestClient(base_url="https://api.example.com")
response = client.get("/users")
assert response.status_code == 200

# GraphQL 测试
graphql = GraphQLClient(endpoint="https://api.example.com/graphql")
result = graphql.query("{ users { id name } }")
```

性能测试使用 `PerformanceTester` 配置并发数、持续时间后执行;契约测试使用 `ContractTester` 加载 OpenAPI/Swagger 文档校验响应结构。

### 步骤4:启动 Mock 服务(可选)

被测服务不可用或依赖未就绪时,用 `MockServer` 配置路由并启动本地 Mock:

```python
from api_test_automation import MockServer, MockRoute

server = MockServer(port=8000)
server.add_route(MockRoute("/users", method="GET", response={"users": []}))
server.start()
```

### 步骤5:生成报告并交付

用 `TestReporter` 汇总测试结果生成报告,向用户输出:用例通过率、失败用例明细、性能指标(响应时间分布/吞吐量)与修复建议。

## 文件结构

```text
api-test-automation/
├── SKILL.md                  # 本文件
├── README.md                 # 使用文档
├── requirements.txt          # 依赖声明
├── examples/
│   └── run_tests.py          # 使用示例
├── tests/
│   └── test_api_suite.py     # 单元测试
└── src/
    ├── rest_client.py        # REST API 客户端
    ├── graphql_client.py     # GraphQL 客户端
    ├── performance.py        # 性能测试工具
    ├── contract_tester.py    # 契约测试
    ├── mock_server.py        # Mock 服务
    └── reporter.py           # 报告生成
```

## 依赖

- Python >= 3.8
- requests >= 2.28.0、httpx >= 0.24.0
- pytest >= 7.0.0、pytest-asyncio >= 0.21.0
- schemathesis >= 3.19.0、hypothesis >= 6.82.0
- aiohttp >= 3.8.0、uvicorn >= 0.23.0、starlette >= 0.27.0
- jsonschema >= 4.19.0、pyyaml >= 6.0、allure-pytest >= 2.13.0

## 许可证

MIT

使用说明

# API 测试自动化

基于 Python 的 API 接口测试自动化工具,支持 REST/GraphQL 功能测试、性能压测、契约验证与 Mock 服务,可生成测试报告。

## 使用

```python
from api_test_automation import RestClient, GraphQLClient, PerformanceTester, MockServer, MockRoute

# REST 功能测试
client = RestClient(base_url="https://api.example.com")
assert client.get("/users").status_code == 200

# GraphQL 测试
graphql = GraphQLClient(endpoint="https://api.example.com/graphql")
graphql.query("{ users { id name } }")

# 并发压测
tester = PerformanceTester(base_url="https://api.example.com")
report = tester.run(path="/users", concurrency=50, duration=30)

# 本地 Mock 服务
server = MockServer(port=8000)
server.add_route(MockRoute("/users", method="GET", response={"users": []}))
server.start()
```

也可以直接告诉 Agent 测试需求(如「对这个接口做 50 并发压测」),Agent 会自动组织用例并执行。

## 工作原理

`src/` 提供 REST/GraphQL 客户端、性能测试器、契约测试器(基于 OpenAPI/Swagger + JSON Schema)、Mock 服务器与报告生成器;测试用例可用 pytest 组织,报告输出通过率、失败明细与性能指标。

## 依赖

Python >= 3.8,详见 `requirements.txt`(requests、httpx、pytest、schemathesis、jsonschema 等)。

如何安装此技能?

访问技能市场,点击「安装」按钮,按提示将技能包放入 AI 编程助手的 skills 目录即可。

浏览技能市场

支持平台:Qoder · QoderWork · Claude · Codex 等 AI 编程助手