I had to do something similar. I created simple TestCase
subclasses that took a value in their __init__
, like this:
class KnownGood(unittest.TestCase):
def __init__(self, input, output):
super(KnownGood, self).__init__()
self.input = input
self.output = output
def runTest(self):
self.assertEqual(function_to_test(self.input), self.output)
I then made a test suite with these values:
def suite():
suite = unittest.TestSuite()
suite.addTests(KnownGood(input, output) for input, output in known_values)
return suite
You can then run it from your main method:
if __name__ == '__main__':
unittest.TextTestRunner().run(suite())
The advantages of this are:
- As you add more values, the number of reported tests increases, which makes you feel like you are doing more.
- Each individual test case can fail individually
- It’s conceptually simple, since each input/output value is converted into one TestCase