用途:该技能面向已有 OpenAPI 规范的 API 测试,使用 Schemathesis 读取本地或远程 schema,自动生成测试以校验 API 行为,支持 schema 校验、模糊测试(fuzzing)与 CI 报告。
主要能力:支持认证、不同测试阶段(examples 与 fuzzing)、并发执行以及测试报告生成。核心模式包括:schema 优先(保持 OpenAPI 与代码同步)、认证头(为受保护端点传递 token)、阶段选择(--phases examples,fuzzing)、并发(--workers)、限流(config rate-limit)、CI 报告(--report junit --report-dir ./reports)以及可复现运行(--seed)。
使用方式:安装命令为 uv pip install schemathesis。运行示例:schemathesis run https://api.example.com/openapi.json;schemathesis run ./openapi.yaml --url https://api.example.com。带认证头:schemathesis run https://api.example.com/openapi.json --header "Authorization: Bearer $TOKEN"。指定阶段:schemathesis run ./openapi.yaml --url https://api.example.com --phases examples,fuzzing。并发:schemathesis run ./openapi.yaml --url https://api.example.com --workers 4。JUnit 报告:schemathesis run ./openapi.yaml --url https://api.example.com --report junit --report-dir ./reports。限流配置示例:rate-limit = "100/m"。
适用场景:已有 OpenAPI 规范并希望提升覆盖率;需要模糊测试或 schema 驱动测试;需要 CI 友好的 JUnit 报告。
限制:不适用于没有 schema 的情况(需先生成);不适用于 UI/浏览器测试。常见问题处理:schema 不匹配时从代码重新生成 OpenAPI;认证失败时确认请求头或 basic auth;端点不稳定时隔离破坏性端点;运行缓慢时限制阶段或减少 workers。参考文档:https://schemathesis.readthedocs.io/ 与 https://github.com/schemathesis/schemathesis。