Skip to content
Latchkey

Tavern test failed: unexpected status_code in CI

A Tavern stage asserted a response status_code and the API returned a different one. Tavern (running under pytest) fails the stage and prints the expected and actual status plus the response body.

What this error means

pytest reports a Tavern test failure with "Status code was 500, expected 200" (or 404/401) for a named stage, and shows the request and response.

tavern
E   tavern.util.exceptions.TestFailError: Test 'Get a single user' failed:
E       - Status code was 500, expected 200

Common causes

The API returned a real error status

The endpoint responded with an error, so the stage correctly failed. The response body Tavern prints shows the cause.

Missing variables or a service not ready

An unset variable (base URL, token) in the Tavern config or common includes, or an API still starting, makes a passing test fail in CI.

How to fix it

Read the response and fix the cause

  1. Check the printed response body for the real error.
  2. Fix the service, its data, or the expected status in the YAML.
  3. Ensure base URL and tokens come from the CI environment.

Drive variables from the environment

Use a Tavern global config or env vars so the stage targets the CI service with valid credentials.

common.yaml
# common.yaml
variables:
  host: "{tavern.env_vars.API_URL}"

How to prevent it

  • Read base URL and secrets from env vars in Tavern config.
  • Wait for the API to be ready before running the suite.
  • Assert the status the endpoint returns for the given request.

Frequently asked questions

What causes ""Status code was ... expected ...""?
The endpoint responded with an error, so the stage correctly failed. The response body Tavern prints shows the cause.
How do I fix "Status code was ... expected ..."?
Read the response and fix the cause

Related guides

References

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