mimirswell
mimirswell

Reputation: 35

My unittest reports a failure (AssertionError: List differ) but they don't appear to differ, why?

This is my unittest code:

def testExtractObject(self):
    self.maxDiff = None
    objects = Script("compare.py").extractObjects()
    self.assertListEqual([Object('ClassA',2,5,None),
                    Object('ClassB',1,3,None),
                    Object('ClassC',0,2,None)], objects)

This is the failure message:

AssertionError: Lists differ: [Object(name='ClassA', meth... != [Object(name='ClassA', meth...

First differing element 0:
Object(name='ClassA', methods=2, lines=5, description=None)
Object(name='ClassA', methods=2, lines=5, description=None)

[Object(name='ClassA', methods=2, lines=5, description=None),
 Object(name='ClassB', methods=1, lines=3, description=None),
 Object(name='ClassC', methods=0, lines=2, description=None)]

I cannot figure out what the difference between the lists are from this message, anyone have some insight into this?

Upvotes: 1

Views: 5337

Answers (1)

Winston Ewert
Winston Ewert

Reputation: 45039

You have to tell python how to determine whether or not two objects are equal. You do this by adding an __eq__ function to your class

class SomeObject(object):
    def __eq__(self, other):
        return self.a == other.a and self.b == other.b

Then objects which pass this test will be considered equal. By default the equality test for objects is left is right which is only true when they are the same object, not two objects with the same values.

Upvotes: 5

Related Questions