Unittest's assertEqual and iterables - only check the contents

Lucas Hoepner picture Lucas Hoepner · Sep 19, 2011 · Viewed 42.9k times · Source

Is there a 'decent' way in unittest to check the equality of the contents of two iterable objects? I am using a lot of tuples, lists and numpy arrays and I usually only want to test for the contents and not for the type. Currently I am simply casting the type:

self.assertEqual (tuple (self.numpy_data), tuple (self.reference_list))

I used this list comprehension a while ago:

[self.assertEqual (*x) for x in zip(self.numpy_data, self.reference_list)]

But this solution seems a bit inferior to the typecast because it only prints single values if it fails and also it does not fail for different lengths of reference and data (due to the zip-function).

Answer

Cédric Julien picture Cédric Julien · Sep 19, 2011

Python 3

Python >= 2.7