I would like to iterate over a list of items, and run an assertion on each of them. One example might be checking whether each number in a list is odd.
TestCase
:
class TestOdd(unittest.TestCase):
def runTest(self):
"""Assert that the item is odd"""
self.assertTrue( NUMBER %2==1, "Number should be odd")
Test suite
:
if __name__ == '__main__':
suite = unittest.TestSuite()
suite.addTest(TestOdd())
# I would like to have:
# suite.addTest(TestOdd(1))
# suite.addTest(TestOdd(2))
# suite.addTest(TestOdd(3))
# ...
unittest.main()
How can I instantiate a TestOdd
object with an argument - for example, the number to be tested?
Update: According to a blog post from 2011 (posted as answer), there is no built-in mechanism for parametrized tests. I will be happy to accept any cleaner solutions.