Prompt

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

Technology / Testing & QA1 observationLast seen Oct 6, 2026

Latest observation

Oct 6, 2026OpenAI APIWeb search: off

A good way to compare expected vs actual API responses in tests is to assert on the shape and important fields rather than doing a brittle full-string comparison.

Common approaches

1. Deep equality for exact responses

If the response should match exactly:

assert actual_response == expected_response

Use this when:

  • field order doesn’t matter
  • the response is deterministic
  • you want to verify every field

2. Compare only important fields

Often APIs include timestamps, IDs, or metadata that change. In that case, assert field-by-field:

assert actual["status"] == "ok"
assert actual["data"]["id"] == expected_id
assert actual["data"]["name"] == "Alice"

This makes tests less brittle.


3. Use partial matching

If you only care about part of the JSON:

expected = {
    "status": "ok",
    "data": {"name": "Alice"}
}

assert actual["status"] == expected["status"]
assert actual["data"]["name"] == expected["data"]["name"]

In some test frameworks, you can use helpers for partial matches:

  • pytest: plain assert
  • unittest: assertEqual, assertDictEqual, etc.
  • Jest: toMatchObject
  • RSpec: include, match

4. Normalize before comparing

For responses with dynamic values, normalize them first:

actual["data"]["createdAt"] = "<ignored>"
expected["data"]["createdAt"] = "<ignored>"

assert actual == expected

Or remove fields you don’t care about:

actual.pop("timestamp", None)
expected.pop("timestamp", None)

assert actual == expected

5. Compare JSON after parsing

If responses come back as JSON strings, parse them first:

import json

actual = json.loads(response.text)
expected = {"status": "ok"}

assert actual["status"] == expected["status"]

Best practices

  • Prefer field-level assertions for important behavior.
  • Avoid asserting on the entire raw response if it contains unstable values.
  • Use schema validation if you want to verify structure and types.
  • Keep one test focused on one behavior.

Example in pytest

def test_get_user(client):
    response = client.get("/user/1")
    actual = response.json()

    assert actual["id"] == 1
    assert actual["name"] == "Alice"
    assert actual["active"] is True

If you want, I can show the best way to do this in a specific language/framework like Python/pytest, JavaScript/Jest, Java/JUnit, or Postman.

Observation history

How did Obsurfable measure this prompt?

Obsurfable records AI answers to buyer-style prompts in its research corpus (1 observation 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 Circuit. 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.