title: Python's unittest and dynamic creation of test cases
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>Is there a way to dynamically create <code>unittest</code> test cases? I have tried the following...</p>
  <pre><code>class test_filenames(unittest.TestCase):
  
      def setUp(self):
          for category, testcases in files.items():
              for testindex, curtest in enumerate(testcases):
                  def thetest():
                      parser = FileParser(curtest['input'])
                      theep = parser.parse()
                      self.assertEquals(theep.episodenumber, curtest['episodenumber'])
  
                  setattr(self, 'test_%s_%02d' % (category, testindex), thetest)
  </code></pre>
  <p>..which creates all the methods correctly (they show up in <code>dir()</code> and are callable), but unittest's test detector, nor <code>nosetest</code> executes them (&quot;Ran 0 tests in ...&quot;)</p>
  <p>Since I may be asking the wrong question - what I am trying to achieve:</p>
  <p>I have a file containing test data, a list of input filenames, and expected data (simplified to <code>episodenumber</code> in the above code), stored in a Python dictionary. The key is the category, the value is a list of test cases, for example...</p>
  <pre><code>test_cases = {}
  test_cases['example_1'] = [
      {'input': 'test.01',
      'episodenumber': 1},
      {'input': 'test.02',
      'episodenumber': 2}
  ]
  
  test_cases['example_2'] = [
      {'input': 'another.123',
      'episodenumber': 123},
      {'input': 'test.e42',
      'episodenumber': 32}
  ]
  </code></pre>
  <p>Currently I just loop over all the data, call <code>self.assertEquals</code> on each test. The problem is, if one fails, I don't see the rest of the failures as they are also grouped into one test, which aborts when an assertion fails.</p>
  <p>The way around this, I thought, would be to (dynamically) create a function for each test case, perhaps there is a better way?</p>