Files
2025-12-12 01:44:41 +08:00
..
2025-12-12 01:44:41 +08:00
2025-12-12 01:44:41 +08:00
2025-12-12 01:44:41 +08:00
2025-12-12 01:44:41 +08:00
2025-12-12 01:44:41 +08:00
2025-12-12 01:44:41 +08:00

武术比赛报名系统 - 测试方案指南

📋 目录

  1. API接口测试
  2. 自动化测试脚本
  3. 压力测试
  4. 数据验证测试

1. API接口测试

方式A使用Apifox/Postman推荐

优点

  • 图形化界面,操作简单
  • 支持环境变量、前置脚本、断言
  • 可以导入导出测试集合
  • 支持Mock Server
  • 团队协作方便

步骤

  1. 安装工具

  2. 导入测试集合

    导入文件: test/api-test-collection.json
    
  3. 配置环境变量

    baseUrl: http://your-api-domain.com
    username: your_username
    password: your_password
    
  4. 运行测试

    • 单个接口测试:点击"发送"按钮
    • 批量测试:使用"测试套件"功能
    • 定时测试:设置定时任务自动运行
  5. 查看测试报告

    • Apifox自动生成测试报告
    • 包含通过率、响应时间、错误详情

方式B使用curl命令简单快速

# 1. 登录获取token
curl -X POST http://your-api-domain.com/martial/auth/login \
  -H "Content-Type: application/json" \
  -d '{"username":"test_user","password":"test_password"}'

# 2. 获取赛事列表替换YOUR_TOKEN
curl -X GET "http://your-api-domain.com/martial/competition/list?current=1&size=20" \
  -H "Blade-Auth: Bearer YOUR_TOKEN"

# 3. 获取选手列表
curl -X GET "http://your-api-domain.com/martial/athlete/list?current=1&size=100" \
  -H "Blade-Auth: Bearer YOUR_TOKEN"

# 4. 新增选手
curl -X POST http://your-api-domain.com/martial/athlete/submit \
  -H "Content-Type: application/json" \
  -H "Blade-Auth: Bearer YOUR_TOKEN" \
  -d '{"name":"测试选手","idCard":"110101199001011234","team":"测试队伍"}'

2. 自动化测试脚本

使用Node.js测试脚本

优点

  • 完全自动化,无需手动操作
  • 可集成到CI/CD流程
  • 详细的测试报告
  • 自动清理测试数据

安装依赖

cd test
npm init -y
npm install axios

配置测试 编辑 test/api-test.js 文件,修改以下配置:

const config = {
  baseURL: 'http://your-api-domain.com',  // 修改为实际API地址
  timeout: 10000,
  testUser: {
    username: 'test_user',    // 修改为测试账号
    password: 'test_password'  // 修改为测试密码
  }
};

运行测试

node test/api-test.js

测试输出示例

============================================================
武术比赛报名系统 - API自动化测试
============================================================
测试开始时间: 2025-12-11 10:30:00
API地址: http://your-api-domain.com

【测试1】用户登录
✓ 登录请求HTTP状态码
✓ 登录业务状态码
✓ 返回Token
  Token: eyJhbGciOiJIUzI1NiI...

【测试2】获取轮播图列表
✓ 轮播图请求HTTP状态码
✓ 轮播图业务状态码
✓ 返回数据格式
  轮播图数量: 3

...(省略其他测试)

============================================================
测试结果汇总
============================================================
总测试数: 42
通过: 40 ✓
失败: 2 ✗
成功率: 95.24%
耗时: 3.52秒
测试结束时间: 2025-12-11 10:30:04
============================================================

3. 压力测试

使用Apache Bench (ab)

测试并发性能

# 安装ab工具Windows用户可以安装Apache
# Ubuntu: sudo apt-get install apache2-utils
# Mac: brew install ab

# 测试赛事列表接口100个请求10个并发
ab -n 100 -c 10 \
  -H "Blade-Auth: Bearer YOUR_TOKEN" \
  http://your-api-domain.com/martial/competition/list?current=1&size=20

# 测试选手列表接口1000个请求50个并发
ab -n 1000 -c 50 \
  -H "Blade-Auth: Bearer YOUR_TOKEN" \
  http://your-api-domain.com/martial/athlete/list?current=1&size=100

使用wrk更强大的压力测试工具

# 安装wrk
# Ubuntu: sudo apt-get install wrk
# Mac: brew install wrk

# 基础压力测试持续30秒12个线程400个并发连接
wrk -t12 -c400 -d30s \
  -H "Blade-Auth: Bearer YOUR_TOKEN" \
  http://your-api-domain.com/martial/competition/list

# POST请求压力测试
wrk -t12 -c400 -d30s -s post.lua \
  -H "Content-Type: application/json" \
  -H "Blade-Auth: Bearer YOUR_TOKEN" \
  http://your-api-domain.com/martial/athlete/submit

4. 数据验证测试

验证数据完整性

创建数据验证脚本 test/data-validation.js

// 验证所有必填字段
async function validateCompetitionData() {
  const res = await api.get('/martial/competition/list');
  const competitions = res.data.data.records || res.data.data;

  const issues = [];
  competitions.forEach((item, index) => {
    if (!item.id) issues.push(`赛事${index}: 缺少id字段`);
    if (!item.name && !item.title) issues.push(`赛事${index}: 缺少name/title字段`);
    if (!item.location && !item.address) issues.push(`赛事${index}: 缺少location/address字段`);
  });

  return issues;
}

5. 测试最佳实践

5.1 测试环境隔离

开发环境: http://dev-api.example.com
测试环境: http://test-api.example.com
生产环境: http://api.example.com

5.2 测试数据管理

  • 使用专门的测试账号
  • 测试后自动清理数据
  • 使用可识别的测试数据前缀(如"测试_"

5.3 定期自动化测试

使用cron定时执行测试

# 每天凌晨2点执行API测试
0 2 * * * cd /path/to/project && node test/api-test.js >> test.log 2>&1

5.4 CI/CD集成

.gitlab-ci.yml.github/workflows/test.yml 中:

test:
  stage: test
  script:
    - cd test
    - npm install
    - node api-test.js
  only:
    - main
    - develop

6. 快速开始检查清单

  • 修改 test/api-test.js 中的 baseURL 和测试账号
  • 安装 axios: npm install axios
  • 运行测试: node test/api-test.js
  • 检查测试报告
  • 如有失败,查看错误详情
  • 修复问题后重新测试

7. 常见问题

Q1: 如何获取测试账号?

A: 联系后端开发人员创建测试账号,或使用已有的开发账号。

Q2: 测试失败怎么办?

A:

  1. 检查API地址是否正确
  2. 检查网络连接
  3. 检查Token是否过期
  4. 查看具体错误信息
  5. 联系后端开发人员

Q3: 如何测试特定功能模块?

A: 修改 api-test.js,注释掉不需要测试的部分。

Q4: 能否测试小程序端?

A: 本测试方案主要测试后端API前端页面需要使用UniApp开发工具或真机测试。


8. 联系与支持

  • 技术文档: 参考 API对接方案.md
  • 问题反馈: 提交到项目Issue
  • 紧急联系: 联系项目负责人

最后更新: 2025-12-11