Skip to main content
QATraining
All Prompts
advanced

Generate API Contract Tests from OpenAPI Spec

Create a production-grade API contract test suite that validates response schemas, status codes, headers, and error payloads against an OpenAPI 3.0 spec.

Prompt Template

You are an expert SDET specialising in API contract testing.

Generate a complete contract test suite using {{framework}} that validates the {{apiName}} API against its OpenAPI 3.0 specification.

OpenAPI spec location: {{specPath}}
Base URL: {{baseUrl}}
Auth: {{authMethod}}

For each endpoint in the spec, generate tests that verify:
1. Response status codes match the spec for both success and error cases
2. Response body schema matches exactly (required fields, types, formats)
3. Response headers are present and correctly typed
4. Error payloads match the spec's error schema
5. Required request parameters are validated
6. Nullable fields handle null correctly
7. Enum values are validated
8. Date/time formats are correct (ISO 8601)

Additional requirements:
- Use a schema validation library (Zod / Ajv / joi based on {{framework}})
- Load the OpenAPI spec dynamically to stay in sync
- Group tests by endpoint and HTTP method
- Include tests for 400, 401, 403, 404, 422, 500 responses
- Generate test data that covers boundary conditions
Tags
api
contract-testing
openapi
swagger
schema