I have some kind of test data and want to create a unit test for each item. My first idea was to do it like this:
import unittest l = [["foo", "a", "a",], ["bar", "a", "b"], ["lee", "b", "b"]] class TestSequence(unittest.TestCase): def testsample(self): for name, a,b in l: print "test", name self.assertEqual(a,b) if __name__ == '__main__': unittest.main()
The downside of this is that it handles all data in one test. I would like to generate one test for each item on the fly. Any suggestions?
This is called "parametrization".
There are several tools that support this approach. E.g.:
The resulting code looks like this:
Which will generate the tests:
For historical reasons I’ll leave the original answer circa 2008):
I use something like this: