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