Contract Testing with Pact — Consumer & Provider (2026)
Pact contract testing explained: consumer-driven contracts, provider verification, the broker, and CI wiring that catches microservice breakages before they reach staging.

Last updated 2026-07-20 · 13 min read · By Avinash Kamble, reviewed by Priyanka G.
In a microservices world, integration tests scale badly and end-to-end tests scale worst. Consumer-driven contracts with Pact catch cross-service breakages in unit-test time. Every serious platform team runs Pact — this guide gets you productive in an afternoon.
Key takeaways
- What consumer-driven contracts are and why they beat integration tests.
- A consumer test that generates a pact file.
- A provider verification test that fails builds on incompatibility.
- The Pact Broker: publishing pacts and gating deploys with can-i-deploy.
1. Why contract testing
End-to-end tests grow O(n²) with services and go red every deploy. Contracts assert only what the consumer uses from the provider. Consumer-side is fast (a mock server); provider-side runs against the real service. Break the contract, break the build — never in staging.
2. Consumer test — Node.js example
import { PactV3, MatchersV3 } from '@pact-foundation/pact';
import { getOrder } from '../src/orderClient';
const { like, integer } = MatchersV3;
const provider = new PactV3({ consumer: 'web', provider: 'orders-api' });
test('getOrder', async () => {
provider.addInteraction({
states: [{ description: 'order 4211 exists' }],
uponReceiving: 'a request for order 4211',
withRequest: { method: 'GET', path: '/orders/4211' },
willRespondWith: { status: 200, body: like({ id: integer(4211), total: 100 }) },
});
await provider.executeTest(async (mock) => {
const order = await getOrder(mock.url, 4211);
expect(order.id).toBe(4211);
});
});Running this generates pacts/web-orders-api.json — the contract.
3. Provider verification
import { Verifier } from '@pact-foundation/pact';
new Verifier({
provider: 'orders-api',
providerBaseUrl: 'http://localhost:3000',
pactBrokerUrl: 'https://broker.example.com',
publishVerificationResult: true,
providerVersion: process.env.GIT_SHA,
stateHandlers: {
'order 4211 exists': async () => { await seedOrder(4211); },
},
}).verifyProvider();The verifier replays each interaction against the real service. If the provider changed the response, the build fails with a precise diff.
4. Pact Broker + can-i-deploy
# Publish from consumer CI
pact-broker publish pacts --consumer-app-version=$GIT_SHA --tag=main
# Gate provider deploy
pact-broker can-i-deploy --pacticipant=orders-api \
--version=$GIT_SHA --to-environment=productionThe broker stores every pact + verification. can-i-deploy blocks a deploy if any consumer's verified version is incompatible. See the official Pact docs.
5. When contracts beat other approaches
| Situation | Best fit |
|---|---|
| Two internal services owned by 1 team | Integration tests |
| Many services, many teams | Contracts (Pact) |
| Third-party you cannot control | Recorded HTTP + monitoring |
| End-user flows across all services | Small suite of E2E on top of contracts |
Combine Pact with REST Assured or Postman for regression, and see our API testing interview prep for contract-testing questions asked at senior interviews.
Frequently asked questions
1.Is Pact only for REST?
2.Do I still need integration tests with Pact?
3.How does can-i-deploy work?
4.Pact vs OpenAPI-based contract testing?
Practice these questions
Rehearse REST, Postman, REST Assured and contract-testing questions with worked examples.
Was this article helpful?
More from REST API Testing
REST fundamentals — verbs, status codes, contracts.
- Experience-Level QA InterviewsAPI Testing Interview Questions for 1 Year Experience (2026 Complete Guide)
- Experience-Level QA InterviewsAPI Testing Interview Questions for 3 Years Experience (2026 Complete Guide)
- Experience-Level QA InterviewsAPI Testing Interview Questions for Senior Level (2026 Complete Guide)
Keep building your QA edge
Pillar guides- Postman TutorialSoftwareTestPilot's Postman walkthroughPostman from zero to CI — collections, scripts, Newman.
- cURL to Code Converterconvert cURL to Postman, Playwright, or Rest AssuredConvert any cURL command to Postman, Playwright, Rest Assured, k6, Cypress, Python, and more — free, in-browser.
- JSON / JSONPath / JMESPath Testertest JSONPath and JMESPath side by sideDual-engine JSONPath + JMESPath tester with assertion builder and Postman/Playwright/Rest Assured export.
- Postman to Code Converterconvert Postman collection to PlaywrightConvert any Postman collection into a full Playwright, Rest Assured, k6, Cypress, Supertest, Python, or Karate test suite — folders, pm.test assertions, and environments preserved.
- API Tester RoleSoftwareTestPilot's API Tester role pageAPI Tester career guide — Postman, REST Assured, contract testing, and pay.
- Microservices Testing Strategyhow to test microservices with Pact and TestcontainersContract testing with Pact, Testcontainers, service virtualization, chaos & E2E patterns.
Practice these questions live
Rehearse with an AI QA interviewer that scores your answers in real time.
Continue reading
Related concepts, tools & standards around API Testing
A quick reference of the people, companies, frameworks and technologies most often mentioned alongside API Testing in real QA teams — useful when you're mapping a learning path, preparing for interviews, or scoping a new project.
Join the QA Community
Connect with fellow testers, share job leads, and get career advice.
Stop Reinventing the Wheel. Upgrade Your QA Arsenal.
Take your testing skills from beginner to Lead Engineer. Supercharge your daily workflow with our premium digital resources.
- Ready-to-use testing strategy templates
- Advanced API & UI automation guides
- ⏱️ Save 10+ hours a week on test planning



Discussion
Ask a question, share your experience, or correct us. Be kind — real people are reading.