Contract testing verify API contract (format, structure, behavior) giữa service provider và consumer mà không cần cả hai chạy đồng thời — critical trong microservices.
- Consumer-driven contract testing với Pact: Consumer viết tests định nghĩa expectations về provider responses (pact file); Provider chạy pact file để verify nó thỏa mãn consumer expectations.
- Pact Broker: central repository cho pact files, track which consumer versions are compatible với which provider versions.
- Can-I-Deploy tool: query Pact Broker để check nếu service version có thể deploy safely — 'can consumer v2 deploy if provider is v3?'.
- Tại sao quan trọng hơn E2E tests trong microservices: E2E tests cần tất cả services chạy đồng thời (expensive, slow, flaky); contract tests chạy independently per service — fast, reliable, pinpoint exactly which service broke contract.
- Breaking change detection: nếu provider thay đổi response schema, pact verification fail ngay trong CI trước khi deploy — không phải phát hiện trong production.
- Schema registry (Avro, Protobuf) cho event-driven architecture: schema evolution rules, backward/forward compatibility checks — contract testing cho async messaging.
- OpenAPI contract testing: Dredd, Spectral verify API implementation against OpenAPI spec — simpler than Pact nhưng ít powerful.
Contract testing verifies the API contract (format, structure, behavior) between a service provider and a consumer without requiring both to run simultaneously — critical in microservices.
- Consumer-driven contract testing with Pact: the Consumer writes tests that define expectations about provider responses (a pact file); the Provider runs the pact file to verify it satisfies the consumer's expectations.
- Pact Broker: a central repository for pact files that tracks which consumer versions are compatible with which provider versions.
- Can-I-Deploy tool: queries the Pact Broker to check whether a service version can be deployed safely — 'can consumer v2 deploy if provider is at v3?'.
- Why it matters more than E2E tests in microservices: E2E tests require all services to run simultaneously (expensive, slow, flaky); contract tests run independently per service — fast, reliable, and pinpoint exactly which service broke a contract.
- Breaking change detection: if the provider changes a response schema, pact verification fails immediately in CI before deployment — the issue is not discovered in production.
- Schema registry (Avro, Protobuf) for event-driven architecture: schema evolution rules, backward/forward compatibility checks — contract testing for async messaging.
- OpenAPI contract testing: Dredd and Spectral verify an API implementation against an OpenAPI spec — simpler than Pact but less powerful.