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.
E tavern.util.exceptions.TestFailError: Test 'Get user' failed:
E - Status code was 500, expected 200
E - Key 'name' was not found in responseCommon 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
- Run pytest with
-vvso Tavern prints the full expected-versus-actual diff. - Fix the API or correct the expected block to match the real contract.
- Ensure base URL and auth variables are set for the stage.
BASE_URL=http://localhost:8000 \
API_TOKEN=${API_TOKEN} \
pytest tests/tavern/ -vvInject variables through the Tavern common block
Reference environment variables in the YAML so base URL and token come from CI, not hard-coded values.
stages:
- name: Get user
request:
url: "{tavern.env_vars.BASE_URL}/users/1"
headers:
Authorization: "Bearer {tavern.env_vars.API_TOKEN}"
response:
status_code: 200How to prevent it
- Run Tavern with
-vvin 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.