how to add a custom type to unittest.TestCase.assertEqual

If you would like your TestCase class to understand one of your custom types as if it were standard, use addTypeEqualityFunc to supply a custom comparison function.

source: unittest/case.py:415

1
2
3
4
5
6
7
8
9
10
11
12
def addTypeEqualityFunc(self, typeobj, function):
"""Add a type specific assertEqual style function to compare a type.
This method is for use by TestCase subclasses that need to register
their own type equality functions to provide nicer error messages.
Args:
typeobj: The data type to call this function on when both values
are of the same type in assertEqual().
function: The callable taking two arguments and an optional
msg= argument that raises self.failureException with a
useful error message when the two arguments are not equal.
"""
self._type_equality_funcs[typeobj] = function