When a test that compares a complex expected value to a complex actual value fails, it can be tricky to work out what went wrong. A standard equality assertion (such as assertEquals in Python's unittest or @=? in Haskell's HUnit) that prints the expected and actual values in full can be rather unhelful, leaving you to manually compare two large chunks of text for minute differences.

A few times I've ended up writing new assertions that perform a diff when the expected and actual values don't match; this week I found myself writing one for HUnit using the Diff and pretty-show packages.

a @==? b = do
  when (a /= b) $ assertFailure diff

     diff = diffStr (getDiff (lines $ ppShow b) (lines $ ppShow a)) ""
     diffStr xs _ = '\n' : concatMap
                    (\(di, s) -> diChar di : ' ' : s ++ "\n") xs

     diChar B = ' '
     diChar F = '-'
     diChar S = '+'

And of course you can define (@?==) = flip (@==?).

Python's doctests have this feature built in, but doctests are considered harmful. It's not too difficult to implement something similar using difflib.