停止编写 API 测试脚本。改用普通英文。

发布: (2026年2月25日 GMT+8 00:13)
4 分钟阅读
原文: Dev.to

Source: Dev.to

概述

Octrafic 让你可以用自然语言编写 API 测试。描述你想要测试的内容,AI 会根据你的 OpenAPI/Swagger 规范生成相应的 HTTP 请求,执行它们,并报告通过/失败的结果。

安装

Linux/macOS

curl -fsSL https://octrafic.com/install.sh | bash

Homebrew

brew install octrafic/tap/octrafic

Windows (PowerShell)

iex (iwr -useb https://octrafic.com/install.ps1)

设置

Octrafic 支持 Claude、OpenAI、OpenRouter、Gemini、Ollama 和 llama.cpp。你自行提供 API 密钥;不会有任何信息通过开发者的服务器传输。

首次运行 Octrafic 并按照交互式设置向导进行配置。

本地 LLM(无需外部 API 密钥)

ollama pull qwen2.5:7b

在设置过程中,将 Octrafic 指向本地模型。

使用 TUI

octrafic -u https://api.example.com -s openapi.json -n "My API"
  • -u – API 基础 URL
  • -s – 你的 OpenAPI/Swagger 规范文件路径
  • -n – 项目名称

进入 TUI 后,用自然语言描述测试,例如:

  • test the login endpoint with valid credentials
  • test the login endpoint with a wrong password
  • test creating a new user and check the response structure
  • run edge cases on the /users endpoint

AI 会判断正确的 HTTP 方法、URL、请求头和负载,执行请求,并显示带有通过/失败结果的响应。

认证

在启动会话时通过 CLI 参数传递认证信息。

Bearer token

octrafic -u https://api.example.com -s spec.json \
  --auth bearer --token "your-token-here"

API key

octrafic -u https://api.example.com -s spec.json \
  --auth apikey --key X-API-Key --value "your-key-here"

Basic auth

octrafic -u https://api.example.com -s spec.json \
  --auth basic --user admin --pass secret123

或者使用环境变量,以避免在 shell 历史中存储凭证:

export OCTRAFIC_AUTH_TYPE=bearer
export OCTRAFIC_AUTH_TOKEN=your-token-here
octrafic -u https://api.example.com -s spec.json

注意: 你的实际令牌和密码永远不会离开你的机器;AI 只会收到认证类型和请求头名称。

导出测试

交互会话结束后,你可以让 AI 导出生成的测试:

  • export these tests to postman
  • export tests as a shell script
  • export to pytest and name it test_users_api.py

导出文件会保存到 ~/Documents/octrafic/tests/

如果你想稍后运行测试,也可以在执行前导出测试计划。

CI / 流水线

对于非交互式使用,使用 octrafic test 子命令:

octrafic test \
  --url https://api.example.com \
  --path tests/api-tests.json \
  --auth bearer \
  --token $API_TOKEN

或者直接从提示生成并运行测试:

octrafic test \
  --url https://api.example.com \
  --spec openapi.json \
  --prompt "test all user endpoints"

如果所有测试通过,命令返回 0;否则返回 1

GitHub Actions 示例

- name: Run API tests
  run: |
    octrafic test \
      --url ${{ secrets.API_URL }} \
      --path tests/api-tests.json \
      --auth bearer \
      --token ${{ secrets.API_TOKEN }}

持久化

默认情况下,新会话是临时的。使用 TUI 中的 /save 命令来持久化你的项目,或留意状态栏中的临时指示器。

贡献与资源

Octrafic 是开源且仍在早期开发阶段。如果你遇到问题或有想法,请提交 issue。

0 浏览
Back to Blog

相关文章

阅读更多 »

没人想负责的 Systemd Bug

TL;DR:存在一个命名空间 bug,影响 Ubuntu 20.04、22.04 和 24.04 服务器,导致随机服务故障。自 2021 年起已在系统中报告……