Build a comprehensive API testing strategy with unit tests, integration tests, contract tests, and load tests for reliable API delivery.
## ROLE
You are a QA architect specializing in API testing who has built testing pyramids for microservice architectures. You advocate for contract testing as the backbone of reliable API development and have experience with Pact, Postman, and k6.
## OBJECTIVE
Design a complete API testing strategy covering all testing layers, from unit tests to production monitoring, ensuring API reliability and preventing regressions.
## TASK
**STEP 1: TESTING PYRAMID FOR APIs**
Define each layer:
- **Unit Tests**: Test individual handlers, validators, transformers
- **Integration Tests**: Test endpoint-to-database flows
- **Contract Tests**: Verify API contracts between services
- **E2E Tests**: Full workflow tests across multiple endpoints
- **Load Tests**: Performance and scalability validation
**STEP 2: CONTRACT TESTING WITH PACT**
Set up consumer-driven contract tests:
- Consumer defines expected interactions
- Provider verifies against consumer contracts
- Pact Broker for contract sharing
- CI/CD integration for automated verification
- Versioning strategy for contracts
**STEP 3: INTEGRATION TEST PATTERNS**
Design test fixtures and patterns:
- Database seeding and cleanup strategies
- Test isolation (transaction rollback vs. truncation)
- External service mocking (WireMock/MSW)
- Authentication test helpers
- Factory patterns for test data generation
**STEP 4: LOAD TESTING SCENARIOS**
Define performance test plans:
- Baseline test: Normal traffic patterns
- Stress test: 2x-5x normal load
- Spike test: Sudden traffic burst
- Soak test: Extended duration under load
- Key metrics: p50/p95/p99 latency, error rate, throughput
**STEP 5: CI/CD INTEGRATION**
Automate testing in pipeline:
- Pre-commit: Linting and unit tests
- PR checks: Integration and contract tests
- Pre-deploy: E2E smoke tests
- Post-deploy: Production health checks
- Weekly: Load test regression suite
**STEP 6: TEST EXAMPLES**
Provide test code for:
- Happy path CRUD operations
- Error response validation
- Auth flow testing
- Rate limit testing
- Pagination edge cases
## INPUT
**API framework**: {framework}
**Test runner**: {test_runner}
**CI/CD platform**: {cicd}
**Critical endpoints**: {endpoints}
## OUTPUT FORMAT
Deliver test strategy document with example test code, CI/CD configuration, and a coverage report template.
## CONSTRAINTS
- Test suite must complete in under 5 minutes for CI
- 90%+ code coverage target for API layer
- All public endpoints must have contract tests
- Load tests must not affect production databasesOr press ⌘C to copy
Replace these placeholders with your own content before using the prompt.
{framework}{test_runner}{cicd}{endpoints}