V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
爱意满满的作品展示区。
ga9
V2EX  ›  分享创造

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

  •  
  •   ga9 · 17 小时 33 分钟前 · 255 次点击

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

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

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


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

    核心功能:

    • 根据 YAML 配置文件自动执行 API 测试
    • 支持测试场景编排和步骤依赖
    • 支持变量提取和数据传递
    • 支持多种断言验证
    • 生成详细的测试报告

    解决的问题:

    • 自动化 API 接口测试,减少手工测试工作量
    • 支持复杂的测试场景和数据依赖关系
    • 可集成到 CI/CD 流程中实现持续测试

    安装 方式一:使用 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
    

    就会执行测试了

    2 条回复    2025-12-09 03:57:01 +08:00
    ga9
        1
    ga9  
    OP
       17 小时 25 分钟前
    lifeOsDeveloper
        2
    lifeOsDeveloper  
       58 分钟前
    挺不错的,我最近搞了一个类似 postman 的,不过还没自动测试功能
    关于   ·   帮助文档   ·   自助推广系统   ·   博客   ·   API   ·   FAQ   ·   Solana   ·   909 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 22ms · UTC 20:55 · PVG 04:55 · LAX 12:55 · JFK 15:55
    ♥ Do have faith in what you're doing.