Using assertTrue(==) vs assertEqual in unittest

Always use assertEqual(), as it customizes failure output.

The method delegates to various helper methods to show you how, for example, two strings or two lists differ when the assertion fails, provided the type of both arguments match and have a type-specific helper method registered.

assertTrue() can only tell you about the assertion failing, not show you why.

From the assertEqual() documentation:

In addition, if first and second are the exact same type and one of list, tuple, dict, set, frozenset or str or any type that a subclass registers with addTypeEqualityFunc() the type-specific equality function will be called in order to generate a more useful default error message (see also the list of type-specific methods).

Only use assertTrue() if there is no more specific assertion available.


If you want to check a value other than True choose assertEqual to get a meaningful error message. It's also more readable in my opinion as you define what is the expected part compared to the value you want to check.

Choose assertTrue over assertEqual if you want to check for a True boolean result such as assertTrue(user.hasAdminRole())