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.