title: Python library 'unittest': Generate multiple tests programmatically
body: <blockquote>
  <p><strong>Possible Duplicate:</strong> <br/>
  <a href="https://stackoverflow.com/questions/32899/how-do-you-generate-dynamic-parameterized-unit-tests-in-python">How do you generate dynamic (parameterized) unit tests in Python?</a></p>
  </blockquote>
  
  <p>I have a function to test, <code>under_test</code>, and a set of expected input/output pairs:</p>
  <pre><code>[
  (2, 332),
  (234, 99213),
  (9, 3),
  # ...
  ]
  </code></pre>
  <p>I would like each one of these input/output pairs to be tested in its own <code>test_*</code> method. Is that possible?</p>
  <p>This is sort of what I want, but forcing every single input/output pair into a single test:</p>
  <pre><code>class TestPreReqs(unittest.TestCase):
  
      def setUp(self):
          self.expected_pairs = [(23, 55), (4, 32)]
  
      def test_expected(self):
          for exp in self.expected_pairs:
              self.assertEqual(under_test(exp[0]), exp[1])
  
  if __name__ == '__main__':
      unittest.main()
  </code></pre>
  <p>(Also, do I really want to be putting that definition of <code>self.expected_pairs</code> in <code>setUp</code>?)</p>
  <p><strong>UPDATE:</strong> Trying <a href="https://stackoverflow.com/questions/2798956/python-library-unittest-generate-multiple-tests-programmatically/2799009#2799009"><strong>doublep</strong>'s advice</a>:</p>
  <pre><code>class TestPreReqs(unittest.TestCase):
  
      def setUp(self):
          expected_pairs = [
                            (2, 3),
                            (42, 11),
                            (3, None),
                            (31, 99),
                           ]
  
          for k, pair in expected_pairs:
              setattr(TestPreReqs, 'test_expected_%d' % k, create_test(pair))
  
      def create_test (pair):
          def do_test_expected(self):
              self.assertEqual(get_pre_reqs(pair[0]), pair[1])
          return do_test_expected
  
  
  if __name__ == '__main__':
      unittest.main()
  </code></pre>
  <p>This does not work. 0 tests are run. Did I adapt the example incorrectly?</p>