title: How do I concisely implement multiple similar unit tests in the Python unittest framework?
body: <p>I'm implementing unit tests for a family of functions that all share a number of invariants. For example, calling the function with two matrices produce a matrix of known shape.</p>
  
  <p>I would like to write unit tests to test the entire family of functions for this property, without having to write an individual test case for each function (particularly since more functions might be added later). </p>
  
  <p>One way to do this would be to iterate over a list of these functions:</p>
  
  <pre><code>import unittest
  import numpy
  
  from somewhere import the_functions
  from somewhere.else import TheClass
  
  class Test_the_functions(unittest.TestCase):
    def setUp(self):
      self.matrix1 = numpy.ones((5,10))
      self.matrix2 = numpy.identity(5)
  
    def testOutputShape(unittest.TestCase):
       """Output of functions be of a certain shape"""
       for function in all_functions:
         output = function(self.matrix1, self.matrix2)
         fail_message = "%s produces output of the wrong shape" % str(function)
         self.assertEqual(self.matrix1.shape, output.shape, fail_message)
  
  if __name__ == "__main__":
    unittest.main()
  </code></pre>
  
  <p>I got the idea for this from <a href="http://www.diveintopython.net/unit_testing/romantest.html" rel="noreferrer">Dive Into Python</a>. There, it's not a list of functions being tested but a list of known input-output pairs. The problem with this approach is that if any element of the list fails the test, the later elements don't get tested.</p>
  
  <p>I looked at subclassing unittest.TestCase and somehow providing the specific function to test as an argument, but as far as I can tell that prevents us from using unittest.main() because there would be no way to pass the argument to the testcase.</p>
  
  <p>I also looked at dynamically attaching "testSomething" functions to the testcase, by using setattr with a lamdba, but the testcase did not recognize them.</p>
  
  <p>How can I rewrite this so it remains trivial to expand the list of tests, while still ensuring every test is run?</p>