分享一个面向开发者 API 自动测试工具 api-tester

2025 年 12 月 8 日
 ga9

分享一个面向开发者 API 自动测试工具 api-tester

已经在多个项目中使用了,大大节约了测试时间。

不在用 curl, postman 来回调参做测了


API-Tester 是一个基于 Go 语言开发的 API 自动化测试工具。

核心功能:

解决的问题:

安装 方式一:使用 go install

go install github.com/gaoyong06/api-tester/cmd/api-tester@latest

使用: 编写一个 api-test-config.yaml

# API 规范文件路径(相对于配置文件目录)
spec: ../../openapi.yaml

# API 基础 URL
base_url: http://localhost:8106
# 请求超时时间(秒)
timeout: 30

# 请求配置
request:
  headers:
    Content-Type: application/json

# 输出目录
output_dir: ./test/reports
# 是否详细输出
verbose: true

# 日志配置
logging:
  verbose: true
  request_response: true
  variable_processing: true

# 全局变量定义
variables:
  # 正常测试数据
  test_user_id: "user-001"
  test_user_id_2: "user-002"
  test_user_id_3: "user-003"
  
  # 边界测试数据
  min_user_id: "a"  # 最短 user_id ( 1 字符)
  max_user_id: "user-id-with-maximum-length-of-36"  # 最长 user_id ( 36 字符)
  empty_string: ""
  
  # 异常测试数据
  invalid_user_id_short: ""  # 空 user_id
  invalid_user_id_long: "user-id-exceeding-maximum-length-of-36-characters"  # 超过 36 字符
  invalid_user_id_special: "user@id#with$special%chars"  # 特殊字符(如果验证规则不允许)
  sql_injection: "'; DROP TABLE api_key; --"  # SQL 注入测试
  xss_payload: "<script>alert('XSS')</script>"  # XSS 测试
  
  # 特殊字符测试
  special_chars: "!@#$%^&*()_+-=[]{}|;:',.<>?/~`"
  
  # 无效 API Key 格式
  invalid_api_key_short: "dsh_"  # 只有前缀
  invalid_api_key_wrong_prefix: "wrong_prefix_abc123"  # 错误前缀
  invalid_api_key_empty: ""  # 空 Key
  
  # App 测试数据
  test_app_key_1: "my_todo_app"
  test_app_key_2: "my_blog_app"
  test_app_key_3: "my_shop_app"
  test_app_name_1: "我的待办事项"
  test_app_name_2: "我的博客应用"
  test_app_name_3: "我的商店应用"
  test_app_type_web: "web"
  test_app_type_mobile: "mobile"
  test_app_type_desktop: "desktop"
  test_app_type_miniprogram: "miniprogram"
  test_description: "这是一个测试应用"
  test_website_url: "https://my-app.com"
  test_package_name: "com.example.app"
  test_miniprogram_appid: "wx1234567890abcdef"
  
  # 无效 App Key 格式
  invalid_app_key_short: "ab"  # 太短(少于 3 字符)
  invalid_app_key_long: "app_key_exceeding_maximum_length_of_fifty_characters"  # 超过 50 字符
  invalid_app_key_uppercase: "MyApp"  # 包含大写字母
  invalid_app_key_special: "my-app"  # 包含特殊字符(连字符)
  invalid_app_key_space: "my app"  # 包含空格
  invalid_app_key_empty: ""  # 空 app_key

# 测试场景
scenarios:
  # ==================== 基础功能测试 ====================
  
  # 1. 健康检查
  - name: 01-健康检查
    description: 测试服务健康状态
    steps:
      - name: 检查服务健康
        endpoint: /health
        method: GET
        assert:
          status: 200
          body:
            $.data.status: UP
            $.data.service: api-key-service
            $.success: true

  # ==================== API Key 管理接口测试 ====================
  
  # 2. 创建 API Key 正常流程
  - name: 02-创建 API Key 正常流程
    description: 测试创建 API Key 的正常流程
    steps:
      - name: 创建 API Key-用户 1
        endpoint: /api/v1/api-keys
        method: POST
        body:
          user_id: "{{.test_user_id}}"
        assert:
          status: 200
          body:
            $.data.apiKey: "!null"
            $.data.apiKeyId: "!null"
            $.data.keyPrefix: "!null"
            $.success: true
        extract:
          api_key_id_1: $.data.apiKeyId
          api_key_spec_1: $.data.apiKey

      - name: 创建 API Key-用户 2
        endpoint: /api/v1/api-keys
        method: POST
        body:
          user_id: "{{.test_user_id_2}}"
        assert:
          status: 200
          body:
            $.data.apiKey: "!null"
            $.data.apiKeyId: "!null"
            $.success: true
        extract:
          api_key_id_2: $.data.apiKeyId
          api_key_spec_2: $.data.apiKey

运行测试

api-tester run --config api-test-config.yaml

就会执行测试了

1323 次点击
所在节点    分享创造
3 条回复
ga9
2025 年 12 月 8 日
lifeOsDeveloper
2025 年 12 月 9 日
挺不错的,我最近搞了一个类似 postman 的,不过还没自动测试功能
ga9
2025 年 12 月 9 日
@lifeOsDeveloper 多谢多谢,我起初也有想做成一个有图形界面的,后面想着现在都 ai ,脚本化了。所以就往配置文件,命令执行的方向做了

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/1177529

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX