I was working on a unittest which when it failed would say "this string != that string" and because some of these strings were very long (output of a HTML lib I wrote which spits out snippets of HTML code) it became hard to spot how they were different. So I decided to override the usual self.assertEqual(str1, str2)
in Python's unittest class instance with this little baby:
def assertEqualLongString(a, b):
NOT, POINT = '-', '*'
if a != b:
print a
o = ''
for i, e in enumerate(a):
try:
if e != b[i]:
o += POINT
else:
o += NOT
except IndexError:
o += '*'
o += NOT * (len(a)-len(o))
if len(b) > len(a):
o += POINT* (len(b)-len(a))
print o
print b
raise AssertionError, '(see string comparison above)'
It's far from perfect and doesn't really work when you've got Unicode characters that the terminal you use can't print properly. It might not look great on strings that are really really long but I'm sure that's something that can be solved too. After all, this is just a quick hack that helped me spot that the difference between one snippet and another was that one produced <br/>
and the other produced <br />
. Below are some examples of this utility function in action.