Skip to content
cccskills
FreePublished on GitHub

api-contract-testing

API 契约测试时使用。适用于消费者驱动契约测试(Pact)/ OpenAPI 验证(Dredd / Schemathesis)。融合 Pact + Schemathesis + 契约优先。

The English summary is being prepared. Showing the original description.

See how to install

Files included (2)

  • SKILL.md2.6 KB
  • templates/contract-test-template.md824 B

SKILL.md (original)

Review the instructions the agent will receive before you install.

API 契约测试(API Contract Testing)

适用场景

  • 前后端解耦开发(消费者驱动)
  • OpenAPI 契约验证
  • 微服务间接口验证
  • 防止 API 破坏性变更

核心原则

1. 消费者驱动
   前端定义期望,后端验证能满足

2. 契约是合同
   双方都通过 = 联调安全

3. 不依赖对方在线
   各自独立验证

4. 自动化 + CI
   每次 PR 验证契约

Pact(消费者驱动)

// 消费者侧(前端)
import { PactV3 } from '@pact-foundation/pact';

const provider = new PactV3({ consumer: 'frontend', provider: 'order-service' });

it('GET /orders/:id', () => {
  provider
    .given('order 123 exists')
    .uponReceiving('a request for order 123')
    .withRequest({ method: 'GET', path: '/api/v1/orders/123' })
    .willRespondWith({
      status: 200,
      body: { id: 123, status: 'PAID', total: 9999 },
    });
  
  return provider.executeTest(async (mockServer) => {
    const result = await orderApi.getOrder(123, mockServer.url);
    expect(result.status).toBe('PAID');
  });
});

// 服务方侧(后端)
@Provider('order-service')
@PactBroker(host = 'broker.example.com')
class OrderServiceContractTest {
  @State('order 123 exists')
  void orderExists() {
    orderRepo.save(Order.builder().id(123L).status(PAID).build());
  }
  
  @TestTemplate
  @ExtendWith(PactVerificationInvocationContextProvider.class)
  void verify(PactVerificationContext context) {
    context.verifyInteraction();
  }
}

Schemathesis(OpenAPI 属性测试)

# 从 OpenAPI spec 自动生成测试
schemathesis run http://localhost:8080/openapi.json \
  --checks all \
  --auth "Bearer $TOKEN"

# CI 集成
schemathesis run ./openapi.yaml \
  --base-url http://localhost:8080 \
  --validate-schema true \
  --junit-xml report.xml

配套模板

  • templates/contract-test-template.md — 契约测试模板

质量自检

□ 消费者契约定义
□ 服务方验证通过
□ CI 集成
□ 破坏性变更阻塞
□ Pact Broker 配置
□ OpenAPI 验证

常见坑

  1. 不做契约测试——联调才发现不一致
  2. 契约不进 CI——形同虚设
  3. 消费者不写期望——服务方不知道要满足什么
  4. 不用 Pact Broker——契约无法共享

与其他 skill 的协作

上游:
  api-designer → OpenAPI 契约

下游:
  ci-test-integration → CI 集成

Reviews

No reviews yet. Tell others what you thought.

Comments

No login needed. Share how it worked for you, ask a question, or note where it did not run.

No comments yet. Be the first to write one.

Comments are published right away. Do not include personal information such as email addresses or phone numbers. Up to 2 URLs.

0 / 1,000 characters

This form is protected by reCAPTCHA, and the Google Privacy Policy and Terms of Service apply.

More skills from this repository

A/B 测试设计与分析时使用。适用于产品实验、功能验证、转化率优化。融合假设驱动 + 样本量计算 + 置信度判断。

The English summary is being prepared. Showing the original description.

zhaoxuya520/AI-Fullstack-Delivery-Workflow54Updated May 19, 2026

上线前业务方/产品方验收时使用。适用于功能交付确认、UAT、发布会签。融合 BDD Given-When-Then、Specification by Example、ATDD。

The English summary is being prepared. Showing the original description.

zhaoxuya520/AI-Fullstack-Delivery-Workflow54Updated May 19, 2026

设计无障碍可访问的界面时使用。适用于满足 WCAG 标准、键盘导航、屏幕阅读器支持。优先使用 WCAG 2.2 AA 级标准 + 键盘 + 焦点 + 对比度 + ARIA。

The English summary is being prepared. Showing the original description.

zhaoxuya520/AI-Fullstack-Delivery-Workflow54Updated May 19, 2026

实现可访问性(无障碍)时使用。覆盖 WCAG 2.2 AA / ARIA / 键盘导航 / 屏幕阅读器 / 颜色对比 / 焦点管理。融合 React Aria / Radix UI / Headless UI 实践 + axe-core 自动化测试。

The English summary is being prepared. Showing the original description.

zhaoxuya520/AI-Fullstack-Delivery-Workflow54Updated May 19, 2026

AI 模型部署时使用。适用于推理服务 / GPU / TensorRT / 监控 / 扩缩容。

The English summary is being prepared. Showing the original description.

zhaoxuya520/AI-Fullstack-Delivery-Workflow54Updated May 19, 2026

数据分析报告输出时使用。适用于业务分析报告、周报、专项分析、决策支持。融合洞察 + 建议 + 行动项。

The English summary is being prepared. Showing the original description.

zhaoxuya520/AI-Fullstack-Delivery-Workflow54Updated May 19, 2026

See all skills by zhaoxuya520

Report a problem with this skill