1.6The Testing Pyramid for APIs
Traditional testing pyramids emphasize unit tests at the base, integration tests in the middle, and end-to-end tests at the top. For API testing, we use a modified pyramid:
/\
/ \
/ E2E\
/ Flow \
/ Tests \
/----------\
/ Contract \
/ Tests \
/----------------\
/ Schema Validation\
/ & Drift Detection\
/----------------------\
/ Endpoint-Level Tests \
/ (Happy path, errors, \
/ boundaries, auth) \
-----------------------------
Layer 1: Endpoint-Level Tests verify that each endpoint handles valid inputs, rejects invalid inputs, enforces authentication, and returns documented status codes. These are the most numerous tests and run fastest.
Layer 2: Schema Validation and Drift Detection verify that actual API responses match the documented schema. These catch structural regressions and documentation staleness.
Layer 3: Contract Tests verify that services communicate correctly across boundaries. Consumer-driven contracts ensure that API changes do not break downstream services.
Layer 4: End-to-End Flow Tests verify complete business flows that span multiple services and communication channels (synchronous APIs, events, webhooks).