My favorite tool for such a test is parameterized test cases that look like this:
from nose_parameterized import parameterized class MyTestCase(unittest.TestCase): @parameterized.expand([(1,), (2,), (3,)]) def test_read_from_disk(self, file_number): input = read_from_disk('input%02d' % file_number) expected = read_from_disk('output%02d' % file_number) actual = run(input) self.assertEquals(expected, actual)
You write a test case to take whatever parameters you need, wrap the parameterized function in the @parameterized.expand decorator and provide sets of input parameters inside the expand () call. Then the test runner conducts an individual test for each set of parameters!
In this case, there is only one parameter, so the expand() call has an unsuccessful additional level of nesting, but the template becomes especially nice when your use case is a little more complicated and you use param objects to provide args and kwargs to your test function:
from nose_parameterized import parameterized, param class MyTestCase(unittest.TestCase): @parameterized.expand([ param(english='father', spanish='padre'), param(english='taco', spanish='taco'), ('earth', 'tierra'),
The template allows you to easily and clearly indicate many sets of input parameters and only write the testing logic once.
I use nose for testing, so my example uses nose-parameterized , but there is also a unittest compatible version .
waterproof
source share