分享一个面向开发者 API 自动测试工具 api-tester - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
爱意满满的作品展示区。
ga9
V2EX    分享创造

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

  •  
  •   ga9 7 天前 706 次点击

    分享一个面向开发者 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 

    就会执行测试了

    3 条回复    2025-12-09 09:40:53 +08:00
    ga9
        1
    ga9  
    OP
       7 天前
    lifeOsDeveloper
        2
    lifeOsDeveloper  
       6 天前
    挺不错的,我最近搞了一个类似 postman 的,不过还没自动测试功能
    ga9
        3
    ga9  
    OP
       6 天前
    @lifeOsDeveloper 多谢多谢,我起初也有想做成一个有图形界面的,后面想着现在都 ai ,脚本化了。所以就往配置文件,命令执行的方向做了
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     3205 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 26ms UTC 11:52 PVG 19:52 LAX 03:52 JFK 06:52
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86