Prompt
How do I troubleshoot API test failures caused by bad test data?
Latest observation
To troubleshoot API test failures caused by bad test data, work through it like a data-quality problem first and a code problem second.
1. Confirm it’s really a data issue
- Re-run the test with the same setup.
- Compare a failing request with a known-good one.
- Check whether the failure is consistent or only happens with certain records.
2. Inspect the actual request payload
Look at:
- Missing required fields
- Wrong data types
- Invalid enum/value sets
- Bad formatting
- Null/empty values where they aren’t allowed
- Boundary issues like too long strings, invalid dates, negative numbers, etc.
3. Verify test fixtures and seed data
If your tests depend on seeded data:
- Make sure the seed ran successfully
- Check that IDs and foreign keys exist
- Confirm the data is in the expected state before the test starts
- Reset or isolate test data between runs if needed
4. Check assumptions in the test
Tests often fail because they assume:
- A user exists when it doesn’t
- A record is in a specific status
- A token or reference ID is still valid
- Previous tests created data in a certain order
5. Validate API error responses
If the API returns validation errors:
- Read the response body carefully
- Map each error back to the specific field
- Don’t just check the status code; inspect the error message and path
6. Compare against schema/contracts
Use:
- OpenAPI/Swagger schema
- JSON Schema
- Database constraints
- Business rules
This helps determine whether the test data violates an API rule or whether the API is rejecting valid data incorrectly.
7. Log or capture the failing payload
For every failed run, capture:
- Full request body
- Headers
- Response status/body
- Correlation/request ID
- Timestamp
This makes it easier to reproduce and spot patterns.
8. Minimize the test case
Strip the request down to the smallest payload that still fails.
This helps identify the exact field causing the issue.
9. Check environment differences
Bad test data can appear “bad” only in certain environments because of:
- Different reference data
- Different validation rules
- Stale database state
- Timezone/locale differences
- Sanitization or transformation steps
10. Fix the data source, not just the assertion
If the issue is in generated test data:
- Update the data factory/generator
- Add validation before sending requests
- Use deterministic values where possible
- Build reusable valid fixtures
Good debugging pattern
- Reproduce the failure
- Capture request/response
- Identify the offending field
- Check schema and seed data
- Correct the fixture or generator
- Add a regression test so it doesn’t happen again
Example
If a test creates a user with:
{
"email": "not-an-email",
"age": -2
}
and the API returns 400, the issue is likely bad test data, not the API.
Fix the test fixture to use a valid email and age within allowed range.
If you want, I can also give you:
- a troubleshooting checklist,
- examples for Postman/Newman,
- or a debugging workflow for automated API tests in CI.