Introduction
When different software parts need to work together, they must agree on how to communicate. Without this agreement, one part might send data the other doesn't understand, causing errors and delays.
Imagine two friends agreeing to exchange letters. One promises to write in English and include a greeting and a question. The other friend prepares to read only English letters with those parts. This agreement helps them understand each other without confusion.
┌─────────────┐ Contract ┌─────────────┐
│ Provider │─────────────────────▶│ Consumer │
│ (Service) │ │ (Client) │
└─────────────┘ └─────────────┘
▲ │
│ │
│ Contract Testing │
└──────────────────────────────────┘from pact import Consumer, Provider # Define consumer and provider consumer = Consumer('ClientApp') provider = Provider('UserService') # Create a pact between consumer and provider pact = consumer.has_pact_with(provider) # Define expected interaction pact.given('User exists')\ .upon_receiving('a request for user data')\ .with_request('get', '/user/123')\ .will_respond_with(200, body={'id': 123, 'name': 'Alice'}) # Verify the contract with pact: result = provider.get_user(123) # hypothetical function assert result['name'] == 'Alice'