API / Swagger Interview questions
How do you implement contract testing using an OpenAPI specification?
Contract testing using OpenAPI validates that both a provider's real implementation and a consumer's actual usage genuinely conform to the shared, documented contract, catching drift between what the spec says and what the API (or its callers) actually does in practice.
- Provider-side validation: run the real API against its own test suite, then validate each actual request/response pair against the OpenAPI schema, failing the test if the live behavior doesn't match what's documented.
- Schema-based request/response validators: tools like Dredd, Schemathesis, or a custom middleware wrapper intercept real traffic (in tests or even in production) and check it against the spec automatically.
- Property-based/fuzz testing: tools like Schemathesis can generate a wide range of valid and boundary-case inputs directly from the schema's constraints and verify the API's real responses stay within its own documented contract.
- Consumer-side mocking: consumers test against a mock server generated from the same spec, so a break in the actual API surfaces as a contract test failure on the provider side rather than only being discovered when the consumer's real integration breaks.
The core value proposition is catching contract drift early and automatically — a developer who changes an endpoint's response shape without updating the spec (or vice versa) gets a failing test immediately in CI, rather than the mismatch only being discovered later when a real consumer's integration breaks in production.
More Related questions...