Skip to content

Instantly share code, notes, and snippets.

@kephircheek
Created March 2, 2021 14:17
Show Gist options
  • Select an option

  • Save kephircheek/2dad8c56a861f54d1efa79bdce1b21b7 to your computer and use it in GitHub Desktop.

Select an option

Save kephircheek/2dad8c56a861f54d1efa79bdce1b21b7 to your computer and use it in GitHub Desktop.
Confusing unittest diff of failed test.
import unittest
from collections import namedtuple
class TestToyExample(unittest.TestCase):
def setUp(self):
User = namedtuple('User', ['name', 'age', 'sex'])
bob = User('Bob', 43, 'male')
self.groups = [
{
"id": u"xxxxxxxx",
"name": "right",
"admin": bob,
},
]
self.groups_dump = [
{
"id": "xxxxxxxx",
"name": "right",
"admin": tuple(bob),
},
]
self.groups_dump_ = [
{
"id": "xxxxxxxx",
"name": "left",
"admin": tuple(bob),
},
]
def test_dumping(self):
self.assertEqual(self.groups, self.groups_dump)
def test_dumping_with_mistake(self):
self.assertEqual(self.groups, self.groups_dump_)
if __name__ == "__main__":
unittest.main(verbosity=2)
# [OUTPUT]
# test_dumping (__main__.TestToyExample) ... ok
# test_dumping_with_mistake (__main__.TestToyExample) ... FAIL
#
# ======================================================================
# FAIL: test_dumping_with_mistake (__main__.TestToyExample)
# ----------------------------------------------------------------------
# Traceback (most recent call last):
# File "lindsey-unittest.py", line 40, in test_dumping_with_mistake
# self.assertEqual(self.groups, self.groups_dump_)
# AssertionError: Lists differ: [{'admin': User(name='Bob', ag... != [{'admin': ('Bob', 43, 'male')...
#
# First differing element 0:
# {'admin': User(name='Bob', age=43, sex='male'), 'id': u'xxxxxxxx', 'name': 'right'}
# {'admin': ('Bob', 43, 'male'), 'id': 'xxxxxxxx', 'name': 'left'}
#
# + [{'admin': ('Bob', 43, 'male'), 'id': 'xxxxxxxx', 'name': 'left'}]
# - [{'admin': User(name='Bob', age=43, sex='male'),
# - 'id': u'xxxxxxxx',
# - 'name': 'right'}]
#
# ----------------------------------------------------------------------
# Ran 2 tests in 0.002s
#
# FAILED (failures=1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment