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.
E tavern.util.exceptions.TestFailError: Test 'Get a single user' failed:
E - Status code was 500, expected 200Common 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
- Check the printed response body for the real error.
- Fix the service, its data, or the expected status in the YAML.
- 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
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.