API Test Automation
You add a new field to your API response and forget to update the mobile client’s type definitions. The web app handles it gracefully because it ignores unknown fields. The iOS app crashes because its strict decoder rejects the unexpected property. Three different consumers, three different expectations, and no contract tests to catch the mismatch. API testing is the glue that holds distributed systems together.
What You’ll Walk Away With
Section titled “What You’ll Walk Away With”- Comprehensive REST API test generation from OpenAPI specs or route analysis
- GraphQL query and mutation testing with schema validation
- Contract testing patterns that prevent consumer-provider drift
- Authentication and authorization testing for every endpoint
- AI prompts for testing error handling, edge cases, and rate limiting
REST API Test Generation
Section titled “REST API Test Generation”@src/routes/ @src/schemas/Generate API tests for all endpoints in our Express application:
For each route file in /src/routes/:1. Read the route definitions and Zod validation schemas2. Generate tests for: - Valid request (200 response with correct shape) - Missing required fields (400 with validation errors) - Invalid field types (400 with specific error messages) - Authentication required (401 without token) - Authorization check (403 with wrong role) - Resource not found (404 with appropriate message)
Use supertest for HTTP assertions.Use our test factories for request body generation.Follow patterns in @tests/api/auth.api.test.tsSave to /tests/api/{route-name}.api.test.tsclaude "Generate comprehensive API tests for our Express application:
1. Discover all routes: scan /src/routes/ for Express router definitions2. For each endpoint (method + path), generate: - Happy path test with valid data - Validation failure tests (one per required field) - Auth tests (no token, expired token, wrong role) - 404 test for non-existent resources - Duplicate/conflict tests where applicable (409)
3. Create test infrastructure: - /tests/api/helpers/request.ts (authenticated request helpers) - /tests/api/helpers/seed.ts (database seeding for API tests)
4. Run all API tests against a test server5. Fix any failures
Use supertest + Jest. Save to /tests/api/"Generate API test coverage for all endpoints:1. Discover routes from the application code2. Generate tests for success, validation, auth, and error cases3. Set up test database seeding4. Run tests and fix failures5. Create PR with the test suite and coverage reportGraphQL Testing
Section titled “GraphQL Testing”Contract Testing
Section titled “Contract Testing”Ensuring Consumer-Provider Compatibility
Section titled “Ensuring Consumer-Provider Compatibility”API Response Validation
Section titled “API Response Validation”When This Breaks
Section titled “When This Breaks”“API tests are brittle because they depend on database state.” Use database transactions that roll back after each test, or seed specific data for each test in a beforeEach hook. Never share database state between test files.
“Contract tests fail because the provider changed intentionally.” This is working as designed. The contract test caught a breaking change before it reached consumers. Update the consumer contracts, notify consumer teams, and verify they can handle the change before deploying the provider.
“Tests pass but the API behaves differently in production.” Check for environment-specific behavior: feature flags, rate limits, caching, middleware that runs only in production. Your test environment should mirror production configuration as closely as possible.
“GraphQL tests are hard to maintain as the schema evolves.” Generate tests from the schema itself. When the schema changes, regenerate the tests. Keep a separate layer of business logic tests that test behavior independent of schema structure.