Skip to main content
This guide walks through setting up contract testing with hitspec from scratch.

What is Contract Testing?

Contract testing verifies that an API provider honors the agreements made with its consumers. Instead of running full integration tests, you define the expected interactions and verify them independently.

Step 1: Define Contracts

Create a contracts/ directory and define expected interactions:

Step 2: Create a State Handler (Optional)

If your contracts specify @contract.state, create a script that sets up those states:
Make it executable: chmod +x setup-states.sh

Step 3: Verify Contracts

Step 4: CI/CD Integration

Run contract verification as part of the provider’s CI pipeline:

Best Practices

  1. Keep contracts minimal — only assert on fields your consumer actually uses
  2. Use a dedicated directory — separate contracts from integration tests
  3. Version your contracts — treat them as API documentation
  4. Run on both sides — consumers generate contracts, providers verify them
  5. Use state handlers — ensure reproducible provider states for each interaction
Last modified on July 14, 2026