スキル一覧に戻る
AmnadTaowsoam

contract-testing-with-pact

by AmnadTaowsoam

0🍴 0📅 2026年1月24日
GitHubで見るManusで実行

SKILL.md


name: Contract Testing with Pact description: Implementing consumer-driven contract testing for microservices using Pact.

Contract Testing with Pact

Overview

Contract testing validates that service consumers and providers agree on request/response expectations. Pact implements consumer-driven contracts (CDC) with shareable pact files and provider verification.

Table of Contents

  1. What is Contract Testing
  2. Consumer-Driven Contracts
  3. Pact Fundamentals
  4. Pact vs Integration Testing
  5. Writing Consumer Tests
  6. Provider Verification
  7. Pact Broker
  8. CI/CD Integration
  9. Bi-Directional Contracts
  10. Async Messaging
  11. GraphQL
  12. Language Support
  13. Patterns and Anti-Patterns
  14. Breaking Change Strategy
  15. Monitoring Compliance

What is Contract Testing

Contract tests validate API interactions without full end-to-end setups. They prevent breaking changes by verifying expectations explicitly.

Consumer-Driven Contracts

Consumers define expectations; providers must satisfy them:

  • Faster feedback for consumers
  • Clear API expectations
  • Reduced integration surprises

Pact Fundamentals

  • Consumer tests: Generate pact files.
  • Provider verification: Validate pact files.
  • Pact files: JSON contracts.
  • Pact Broker: Store and manage contracts.

Pact vs Integration Testing

  • Pact: Validates interface compatibility.
  • Integration: Validates full system behavior.

Use both for comprehensive coverage.

Writing Consumer Tests

Define interactions with matchers:

  • Exact value
  • Type-based matcher
  • Regex matcher

Example (TypeScript):

await provider.addInteraction({
  state: 'user exists',
  uponReceiving: 'get user',
  withRequest: { method: 'GET', path: '/users/123' },
  willRespondWith: {
    status: 200,
    body: { id: like(123), name: like('Alice') }
  }
});

Provider Verification

Provider verifies against published contracts:

  • Implement state handlers
  • Validate response headers/body/status
  • Support pending and WIP pacts for safe changes

Pact Broker

Key features:

  • Publish and version contracts
  • Webhooks on new contracts
  • Can-I-Deploy checks for promotion
  • Visibility into compatibility

CI/CD Integration

Recommended flow:

  1. Consumer tests generate pact
  2. Publish pact to broker
  3. Provider verifies in CI
  4. Use Can-I-Deploy before release

Bi-Directional Contracts

Support for provider-defined expectations plus consumer constraints:

  • Useful for GraphQL or schema-first APIs
  • Prevents provider drift

Async Messaging

Pact supports message-based contracts:

  • Define message payload expectations
  • Verify consumer and provider handlers

GraphQL

Contract test schema and query responses:

  • Use schema as a contract baseline
  • Validate query response shapes

Language Support

SDKs available for:

  • JavaScript/TypeScript
  • Python
  • Java
  • Go

Patterns and Anti-Patterns

Good:

  • Use flexible matchers
  • Keep interactions focused

Avoid:

  • Over-specifying exact values
  • One contract covering multiple unrelated cases

Breaking Change Strategy

  • Use pending pacts for backward-compatible changes.
  • Version APIs when breaking changes are required.
  • Maintain old contract until consumers migrate.

Monitoring Compliance

Track contract verification in CI and alert on failed verification runs.

  • 16-testing/integration-testing
  • 09-microservices/service-design
  • 03-backend-api/express-rest

スコア

総合スコア

60/100

リポジトリの品質指標に基づく評価

SKILL.md

SKILL.mdファイルが含まれている

+20
LICENSE

ライセンスが設定されている

+10
説明文

100文字以上の説明がある

0/10
人気

GitHub Stars 100以上

0/15
最近の活動

3ヶ月以内に更新がある

0/10
フォーク

10回以上フォークされている

0/5
Issue管理

オープンIssueが50未満

+5
言語

プログラミング言語が設定されている

+5
タグ

1つ以上のタグが設定されている

0/5

レビュー

💬

レビュー機能は近日公開予定です