Skip to content
Latchkey

Tavern "Test failed" response body mismatch in CI

Tavern runs its YAML stages through pytest and compares each response to the response block. When the returned status or body does not match what the stage expects, pytest reports the Tavern test as failed with the diff.

What this error means

pytest reports a Tavern test failure such as "Test 'Get user' failed" with a message that the response status or a body key did not match the expected value.

tavern
E   tavern.util.exceptions.TestFailError: Test 'Get user' failed:
E       - Status code was 500, expected 200
E       - Key 'name' was not found in response

Common causes

The API returned a different status or body

The endpoint answered with a status or JSON shape that does not match the stage's response block, so Tavern fails the comparison.

A missing variable or wrong base URL

An unset {tavern.env_vars.BASE_URL} or auth token makes the request hit the wrong place or lack auth, producing an unexpected response.

How to fix it

Run with verbose output and fix the mismatch

  1. Run pytest with -vv so Tavern prints the full expected-versus-actual diff.
  2. Fix the API or correct the expected block to match the real contract.
  3. Ensure base URL and auth variables are set for the stage.
Terminal
BASE_URL=http://localhost:8000 \
API_TOKEN=${API_TOKEN} \
pytest tests/tavern/ -vv

Inject variables through the Tavern common block

Reference environment variables in the YAML so base URL and token come from CI, not hard-coded values.

test_user.tavern.yaml
stages:
  - name: Get user
    request:
      url: "{tavern.env_vars.BASE_URL}/users/1"
      headers:
        Authorization: "Bearer {tavern.env_vars.API_TOKEN}"
    response:
      status_code: 200

How to prevent it

  • Run Tavern with -vv in CI so failures show the full diff.
  • Inject base URL and auth token from environment variables.
  • Keep the expected response block in sync with the API contract.

Frequently asked questions

What causes "Tavern "Test failed" mismatch"?
The endpoint answered with a status or JSON shape that does not match the stage's response block, so Tavern fails the comparison.
How do I fix Tavern "Test failed" mismatch?
Run with verbose output and fix the mismatch

Related guides

References

Latchkey auto-heals failures like this one - detected, fixed, and retried without you. Start free → 30-day trial · No credit card