Python Testing Tip #10 / Sept. 16, 2025

How to customize assertion error message

Many times, it’s hard to understand why a test is failing just by reading the assertion error. For example, when writing end-to-end tests and asserting the expected status code. Without a custom message, you’ll know only that the API call failed, but you’ll have no idea why.

Customizing assertion error message

Fortunately, it’s simple to customize the assertion error message. You can simply add a comma and a message after the assertion itself. This comes very handy when testing for:

  • boolean flags
  • API status codes

Customizing the message makes it much easier to understand what went wrong and how to fix it. In the API testing case, it’s very helpful to retrieve the response’s text when the response status doesn’t match the expectations and use it as a custom message. There are also other cases where this comes handy, but these are the two where I use the custom assertion message the most frequently.

# test_custom_assertion_error_message.py
import requests
from http import HTTPStatus

def test_default_assertion_message():
    user = {"email": "<EMAIL>", "is_happy": False}
    assert user["is_happy"] is True
    # >       assert user["is_happy"] is True
    # E       assert False is True

def test_custom_assertion_message():
    user = {"email": "<EMAIL>", "is_happy": False}
    assert user["is_happy"] is True, "Failure: user is not happy!"
    # >       assert user["is_happy"] is True, "Failure: user is not happy!"
    # E       AssertionError: Failure: user is not happy!
    # E       assert False is True

def test_weather_api_bad_request_with_default_assertion_message():
    url = "https://api.openweathermap.org/data/2.5/weather"
    params = {"q": "London"}

    response = requests.get(url, params=params)

    assert response.status_code == HTTPStatus.OK
    # >       assert response.status_code == HTTPStatus.OK
    # E       assert 401 == <HTTPStatus.OK: 200>
    # E        +  where 401 = <Response [401]>.status_code
    # E        +  and   <HTTPStatus.OK: 200> = HTTPStatus.OK

def test_weather_api_bad_request_with_custom_assertion_message():
    url = "https://api.openweathermap.org/data/2.5/weather"
    params = {"q": "London"}

    response = requests.get(url, params=params)

    assert response.status_code == HTTPStatus.OK, response.text
    # >       assert response.status_code == HTTPStatus.OK, response.text
    # E       AssertionError: {"cod":401, "message": "Invalid API key. Please see https://openweathermap.org/faq#error401 for more info."}
    # E       assert 401 == <HTTPStatus.OK: 200>
    # E        +  where 401 = <Response [401]>.status_code
    # E        +  and   <HTTPStatus.OK: 200> = HTTPStatus.OK

Share this tip

The complete testing system, not just tips.

Stop piecing together advice from blog posts. This course gives you a structured approach to Python testing that scales with your codebase and keeps your AI agents in check.

Get the Course $20