Tom
Tom

Reputation: 2661

assertEqual fails even though the values are the same in python/django

Can someone explain why this fails:

def test_test(self):
   ...
   print Test.id
   print name[0]
   self.assertEqual(name[0], Test.id)

The output is

Creating test database for alias 'default'... ......

c8124e1d-c01c-4762-bcc0-d32df93e0824

c8124e1d-c01c-4762-bcc0-d32df93e0824 F.................... ====================================================================== FAIL: ... ---------------------------------------------------------------------- Traceback (most recent call last): ... self.assertEqual(name[0], Test.id)

AssertionError: u'c8124e1d-c01c-4762-bcc0-d32df93e0824' != UUID('c8124e1d-c01c-4762-bcc0-d32df93e0824')

any ideas?

Upvotes: 3

Views: 1757

Answers (1)

Daniel
Daniel

Reputation: 42758

Look at the error message: name[0] is a unicode string and Test.id is a UUID. They have the same representation but are different objects. To test equality simply convert one object to the type of the other:

self.assertEqual(name[0], str(Test.id))

Upvotes: 7

Related Questions