Skip to main content
This guide shows how to test API contracts by validating response schemas and status codes. Perfect for ensuring API consistency and catching breaking changes.

Use Cases

  • Validate API response schemas
  • Check expected status codes
  • Test required response fields
  • Ensure API contract compliance

Simple Implementation

Setup Instructions

  1. Define your API contracts with expected fields and types
  2. Add endpoints you want to test to the contracts dictionary
  3. Customize field validation rules for your specific API
  4. Run tests to catch contract violations

What This Tests

  • Response Schemas: Validates JSON structure and required fields
  • Status Codes: Ensures endpoints return expected HTTP codes
  • Field Types: Checks that fields have correct data types
  • Error Handling: Tests error response formats

Contract Validation

The guide validates:
  • Required Fields: Ensures critical fields are always present
  • Data Types: Validates field types (string, integer, etc.)
  • Status Codes: Checks HTTP response codes match expectations
  • Error Responses: Validates error message structure

Common Contract Issues

  • Missing Fields: Required fields not in response
  • Wrong Types: Fields with unexpected data types
  • Status Code Changes: Endpoints returning different codes
  • Schema Drift: API responses changing over time

Extending Contracts

Add more validation rules:
  • Field Formats: Email, date, URL validation
  • Value Ranges: Min/max values for numbers
  • Enum Values: Restricted field values
  • Nested Objects: Complex object validation