title: Comparison of multi-line strings in Python unit test
body: <p>When I compare two Unicode strings in a Python unit test, it gives a nice failure message highlighting which lines and characters are different. However, comparing two 8-bit strings just shows the two strings with no highlighting.</p>
  <p>How can I get the highlighting for both Unicode and 8-bit strings?</p>
  <p>Here is an example unit test that shows both comparisons:</p>
  <pre><code>import unittest
  
  class TestAssertEqual(unittest.TestCase):
      def testString(self):
          a = 'xax\nzzz'
          b = 'xbx\nzzz'
          self.assertEqual(a, b)
  
      def testUnicode(self):
          a = u'xax\nzzz'
          b = u'xbx\nzzz'
          self.assertEqual(a, b)
  
  if __name__ == '__main__':
      unittest.main()
  </code></pre>
  <p>The results of this test show the difference:</p>
  <pre><code>FF
  ======================================================================
  FAIL: testString (__main__.TestAssertEqual)
  ----------------------------------------------------------------------
  Traceback (most recent call last):
    File &quot;/mnt/data/don/workspace/scratch/scratch.py&quot;, line 7, in testString
      self.assertEqual(a, b)
  AssertionError: 'xax\nzzz' != 'xbx\nzzz'
  
  ======================================================================
  FAIL: testUnicode (__main__.TestAssertEqual)
  ----------------------------------------------------------------------
  Traceback (most recent call last):
    File &quot;/mnt/data/don/workspace/scratch/scratch.py&quot;, line 12, in testUnicode
      self.assertEqual(a, b)
  AssertionError: u'xax\nzzz' != u'xbx\nzzz'
  - xax
  ?  ^
  + xbx
  ?  ^
    zzz
  
  ----------------------------------------------------------------------
  Ran 2 tests in 0.001s
  
  FAILED (failures=2)
  </code></pre>
  <h2>Update for Python 3</h2>
  <p>In Python 3, string literals are Unicode by default, so this is mostly irrelevant. <code>assertMultiLineEqual()</code> no longer supports byte strings, so you're pretty much stuck with regular <code>assertEqual()</code> unless you're willing to decode the byte strings to Unicode.</p>