title: How to write a custom `.assertFoo()` method in Python?
body: <p>I'm writing some test cases for my application using Python's <a href="http://docs.python.org/library/unittest.html" rel="noreferrer"><code>unittest</code></a>. Now I need to compare a list of objects with a list of another objects to check if the objects from the first list are what I'm expecting.</p>
  
  <p>How can I write a custom <code>.assertFoo()</code> method? What should it do? Should it raise an exception on failure? If yes, which exception? And how to pass the error message? Should the error message be a unicode string or a bytestring?</p>
  
  <p>Unfortunately, the <a href="http://docs.python.org/library/unittest.html" rel="noreferrer">official documentation</a> doesn't explain how to write custom assertion methods.</p>
  
  <p>If you need a real-world example for this, continue reading.</p>
  
  <hr>
  
  <p>The code I'm writing is somewhat like this:</p>
  
  <pre><code>def assert_object_list(self, objs, expected):
      for index, (item, values) in enumerate(zip(objs, expected)):
          self.assertEqual(
              item.foo, values[0],
              'Item {0}: {1} != {2}'.format(index, item.foo, values[0])
          )
          self.assertEqual(
              item.bar, values[1],
              'Item {0}: {1} != {2}'.format(index, item.bar, values[1])
          )
  
  def test_foobar(self):
      objs = [...]  # Some processing here
      expected = [
          # Expected values for ".foo" and ".bar" for each object
          (1, 'something'),
          (2, 'nothing'),
      ]
      self.assert_object_list(objs, expected)
  </code></pre>
  
  <p>This approach makes it extremely easy to describe the expected values of each object in a very compact way, and without needing to actually create full objects.</p>
  
  <p>However... When one object fails the assertion, no further objects are compared, and this makes debugging a bit more difficult. I would like to write a custom method that would unconditionally compare all objects, and then would display all objects that failed, instead of just the first one.</p>