Prompt
How do I compare expected vs actual API responses in tests?
Latest observation
Nov 11, 2026
OpenAI APIWeb search: off
A good way to compare expected vs actual API responses in tests is to compare structure and important fields, not necessarily every byte of the raw response.
Common approaches
1) Exact match for small, stable responses
If the response is simple and deterministic:
assert actual_response == expected_response
Best when:
- field order doesn’t matter or your data structure preserves it
- no timestamps, IDs, or other dynamic values
- the whole payload is important
2) Compare specific fields
For APIs with dynamic or irrelevant fields:
assert actual_response["status"] == "ok"
assert actual_response["data"]["id"] == expected_id
assert actual_response["data"]["name"] == "Alice"
Best when:
- only certain fields matter
- some values vary per request
- you want clearer failure messages
3) Deep comparison with ignored fields
If you need to compare nested objects but ignore dynamic keys like id, createdAt, or updatedAt:
def remove_keys(obj, keys_to_ignore):
if isinstance(obj, dict):
return {
k: remove_keys(v, keys_to_ignore)
for k, v in obj.items()
if k not in keys_to_ignore
}
if isinstance(obj, list):
return [remove_keys(item, keys_to_ignore) for item in obj]
return obj
expected_clean = remove_keys(expected_response, {"id", "createdAt"})
actual_clean = remove_keys(actual_response, {"id", "createdAt"})
assert actual_clean == expected_clean
4) Schema-based validation
Instead of matching exact values, validate the response shape and types:
- JSON Schema
- OpenAPI schema validation
- Pydantic models
- Marshmallow serializers
Example with JSON Schema:
from jsonschema import validate
validate(instance=actual_response, schema=expected_schema)
Best when:
- you care about contract correctness
- response values can vary
- you want maintainable tests
5) Use assertion helpers from testing libraries
Some libraries provide better diffs than plain assert.
Examples:
- Python:
pytestassertions,deepdiff - JavaScript:
expect(actual).toEqual(expected)in Jest - Java: AssertJ / JSONAssert
- C#: FluentAssertions
Example in Jest:
expect(actualResponse).toEqual(expectedResponse);
Best practices
- Ignore unstable fields like timestamps, UUIDs, and request IDs.
- Compare lists carefully: decide whether order matters.
- Prefer schema checks for contract tests.
- Use exact comparisons only when the payload is deterministic.
- Make failures easy to read by comparing specific fields or using diff-friendly tools.
Practical pattern
A common test strategy is:
- Assert HTTP status code
- Assert headers if needed
- Assert JSON schema or key fields
- Compare cleaned expected vs actual payload
Example:
assert response.status_code == 200
assert response.json()["success"] is True
assert response.json()["data"]["name"] == "Alice"
If you tell me your language/framework (e.g. Python/pytest, JavaScript/Jest, Postman, Java/JUnit), I can show the best idiomatic way.