api-contract-testing
它能做什么
这个工具帮助检查 API 契约(即系统之间通信的规则)的改动是否会引发问题。它能确保 API 的提供方和使用方在改动之后仍然可以正常协作。
适用场景
- ✓当 API 响应中的某个字段或规则发生变化时。
- ✓当代码能够通过编译,但系统在运行时通信失败时。
- ✓当你需要判断某项改动是会破坏兼容性,还是只是新增功能时。
技能链接
https://github.com/sickn33/agentic-awesome-skills/blob/main/plugins/agentic-awesome-skills-claude/skills/api-contract-testing/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“检查这次 API 改动。列出受影响的使用方,以及支持的版本组合。使用现有的测试运行器,为有效、无效和错误响应添加本地契约测试。报告实际运行过的检查,以及仍需验证的内容。”
每周 5 个新技能,1 分钟读完。
工作原理
它会把官方的 API 定义与实际行为进行对比,并使用虚拟数据编写小型测试。这样可以确保改动不会破坏使用该 API 的系统之间的通信。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
API Contract Testing
Overview
Turn an API change into explicit provider and consumer checks. Start from the actual contract and affected clients, then verify wire-level behavior rather than relying on generated types or a successful build. Default to local fixtures and existing test tools; this skill does not provision a testing service.
When to Use
- A response field, endpoint, event schema or error envelope changes.
- A client and server build successfully but disagree at runtime.
- A team needs to distinguish a breaking change from a compatible extension.
- A reported regression needs a minimal provider/consumer reproduction.
Do not use this as authorization to call production endpoints, bypass access controls or publish a new API version.
How It Works
1. Bind the contract and scope
Identify the provider, transport, contract source and version, known consumers and the requested behavior. Read the real OpenAPI/JSON Schema/protobuf definition or documented examples and the corresponding server/client code. Record missing consumers and stale specifications as coverage gaps; do not invent interfaces or claim complete coverage from one client.
Keep these distinct: documented schema, actual provider output, generated client types and runtime parsing. A type assertion alone is not validation. Preserve the existing protocol and test runner instead of introducing a new platform by default.
2. Classify the change
Review fields, requiredness, nullability, enum values, status codes, headers, content type, pagination and error behavior. Inspect whether clients tolerate extra fields and unknown enum values. Even an added optional field can break a strict consumer. Explicitly identify affected versions and assumptions before calling a change compatible.
For events, include duplicate delivery, ordering and version envelopes. For request changes, check serialization and server validation as well as responses.
3. Build a small test matrix
Use deterministic synthetic fixtures for:
- Valid success, including optional and empty values.
- Missing required fields, wrong types and explicit nulls.
- Extra fields and unknown enums according to the actual compatibility policy.
- Declared error envelopes, non-success statuses and malformed bodies.
- Old consumer against new provider and new consumer against retained old provider, where those combinations are supported.
Assert the behavior promised by the contract. Do not silently coerce malformed values or make the fixture match a defect just to get a passing test.
4. Verify each boundary
Run the existing schema validator against fixtures. Exercise the provider handler locally and pass its serialized result through the real consumer parser. Check failure cases reject or map errors as specified. Use an authorized isolated integration environment only when fixtures cannot establish the required property; report that boundary explicitly.
A mock verifies assumptions encoded in the mock. It does not establish that the real provider implements them. Record exact commands, fixture versions and observed outcomes separately from unrun checks.
5. Report and hand off
Deliver the change classification, affected consumers, test matrix, observed failures, minimal reproduction and remaining coverage gaps. For a breaking change, propose versioning, a compatibility adapter or a staged migration with owners and rollback criteria. Deployment and consumer migration require their own task authorization.
Example
Synthetic documented response:
{"id":"invoice-demo","amount_minor":1250,"currency":"EUR"}
Contract: id and currency are strings; amount_minor is a non-negative integer in minor units. This fixture does not represent an actual customer or payment.
| Fixture change | Expected check |
|---|---|
amount_minor becomes "1250" |
Runtime parser rejects the string; compile-time generated types alone cannot detect wire drift. |
amount_minor is missing or null |
Reject when the field is required and non-nullable. |
Extra display_label field |
Verify the consumer's documented extra-field policy before declaring compatibility. |
| HTTP error with an HTML body | Produce the declared transport/protocol error, rather than treating it as a valid invoice. |
Reusable prompt:
Review this API change against the actual contract and supplied client/server code.
…
常见问题
什么是“API 契约”?
它是一份文档,定义了一个系统(提供方)与另一个系统(使用方)如何通信,包括数据格式、端点以及预期遵循的规则。
为什么不使用生产环境的真实数据?
使用真实数据可能会泄露敏感信息,而且也无法保证所有错误场景都被测试到。最小化的合成数据更安全,测试效果也更好。
这个工具会部署或管理 API 吗?
不会。这个工具专注于规划和验证 API 契约,不负责部署 API,也不管理测试服务。
详情
作者的原始描述
Design and review API contract tests across providers and consumers. Use for schema changes, compatibility regressions and client integration failures.
热度
- GitHub 星标
- 48k
- 排名
- #3765
- 许可证
- MIT
- 更新时间
- 2026/10/15
评论
- …
同一仓库的更多技能
00-andruia-consultant
48k帮你的智能体启动或改进 AI 项目。它可以分析代码、推荐工具,并用西班牙语制定行动计划。
007
48k让你的智能体执行安全审计和威胁建模。它有助于保护项目免受攻击和漏洞的威胁。
10-andruia-skill-smith
48k帮你的智能体创建或改进 AI 工具。它可以定义工具的工作方式、编写文档,并验证其结构。
20-andruia-niche-intelligence
48k让你的智能体创建某个特定行业的摘要。它有助于借助可靠的信息来源,了解项目的背景、规则、使用模式和风险。