home / skills / amnadtaowsoam / cerebraskills / contract-testing-pact

contract-testing-pact skill

/16-testing/contract-testing-pact

This skill helps you implement consumer-driven contract testing with Pact in Python, generating and verifying pact files and ensuring provider compatibility.

npx playbooks add skill amnadtaowsoam/cerebraskills --skill contract-testing-pact

Review the files below or copy the command above to add this skill to your agents.

Files (1)
SKILL.md
3.8 KB
---
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](#what-is-contract-testing)
2. [Consumer-Driven Contracts](#consumer-driven-contracts)
3. [Pact Fundamentals](#pact-fundamentals)
4. [Pact vs Integration Testing](#pact-vs-integration-testing)
5. [Writing Consumer Tests](#writing-consumer-tests)
6. [Provider Verification](#provider-verification)
7. [Pact Broker](#pact-broker)
8. [CI/CD Integration](#cicd-integration)
9. [Bi-Directional Contracts](#bi-directional-contracts)
10. [Async Messaging](#async-messaging)
11. [GraphQL](#graphql)
12. [Language Support](#language-support)
13. [Patterns and Anti-Patterns](#patterns-and-anti-patterns)
14. [Breaking Change Strategy](#breaking-change-strategy)
15. [Monitoring Compliance](#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):
```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.

## Related Skills
- `16-testing/integration-testing`
- `09-microservices/service-design`
- `03-backend-api/express-rest`

Overview

This skill covers implementing consumer-driven contract testing for microservices using Pact. It explains how to write consumer contracts, publish them, and verify providers to prevent integration breakages. It focuses on practical Pact workflows for Python teams and CI/CD integration.

How this skill works

It guides you to write consumer tests that generate Pact files describing expected requests and responses, using matchers to allow flexible assertions. Pacts are published to a Pact Broker and then used by provider verification jobs in CI to validate that providers meet consumer expectations. The flow supports pending/WIP pacts, messaging, and GraphQL scenarios for broader compatibility.

When to use it

  • When microservices are developed by separate teams to avoid integration surprises
  • Before releases to validate provider compatibility without full end-to-end tests
  • When shifting to consumer-driven API design to give consumers faster feedback
  • For async messaging or GraphQL APIs where schema/shape validation is necessary
  • To gate deployments with Can-I-Deploy checks in CI/CD

Best practices

  • Write consumer tests that focus on interaction shape using matchers rather than exact values
  • Publish pact files to a Pact Broker and automate provider verification in CI
  • Use state handlers on the provider side to reproduce required provider states for verification
  • Treat pacts as part of your deployment pipeline: use Can-I-Deploy and webhooks to automate promotion
  • Keep contracts small and focused; avoid one contract covering unrelated scenarios

Example use cases

  • A frontend team generates pacts for user APIs and publishes them so backend CI verifies compatibility before deploys
  • A payments service uses pact message contracts to validate async events between producer and consumer
  • A GraphQL consumer uses schema-based contracts to validate response shapes against provider verification
  • A team uses pending/WIP pacts to introduce compatible changes while letting consumers migrate gradually
  • CI pipeline runs consumer tests, publishes pacts, then provider pipelines verify and block deployment on failures

FAQ

How do matchers help in consumer tests?

Matchers let you assert types or patterns instead of exact values, reducing brittle contracts and accommodating reasonable provider data variations.

What role does a Pact Broker play?

The Pact Broker stores and versions pact files, exposes webhooks, and enables Can-I-Deploy checks so teams can control promotion based on verification status.