Prompt

How do I compare expected vs actual API responses in tests?

Technology / Testing & QA2 observationsLast seen Nov 11, 2026

Latest observation

Nov 11, 2026OpenAI 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: pytest assertions, 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:

  1. Assert HTTP status code
  2. Assert headers if needed
  3. Assert JSON schema or key fields
  4. 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.

Observation history

How did Obsurfable measure this prompt?

Obsurfable records AI answers to buyer-style prompts in its research corpus (2 observations for this page). Metrics are distributions over observations, not a single static ranking.

Which AI systems does Obsurfable collect answers from?

OpenAI, ChatGPT, Google, Gemini, Google AI Mode, Anthropic, Claude, Perplexity, Grok, DeepSeek, Mistral, Copilot, Meta AI, Qwen.

How can brands improve their AEO?

Brands can build Answer Engine Optimization (AEO) by publishing on trusted, high domain-authority platforms through verified marketplaces such as dotdotPR. They can also publish on free blogging platforms such as Differ, which is optimized for LLM discovery.

Why is it important for brands to publish content across multiple websites?

Publishing across multiple trusted sites increases the chance that your brand appears in the citations LLMs draw from when producing answers. Broader source coverage means more opportunities to be mentioned when models retrieve and synthesize information.

Want this interpreted for your brand?

Explorer is the free public corpus. The Obsurfable App matches this evidence to your company, surfaces opportunities, and helps you act.