We frequently want to repeat the same test with different datasets. When using the functionalities of unittest, this requires us to automatically generate test cases with the corresponding methods injected.
To this end, we first construct a test case with one or several methods that will be used, when we later set up test methods. We'll consider the bisection method again and let's check if the values it returns are really zeros of the given function.
We first build the test case and the method that we will use for the tests as follows:
class Tests(unittest.TestCase): def checkifzero(self,fcn_with_zero,interval): result = bisect(fcn_with_zero,*interval,tol=1.e-8) function_value=fcn_with_zero(result) expected=0. self.assertAlmostEqual(function_value, expected)
Then we dynamically create test functions as attributes of this class:
test_data=[ {'name':'identity', 'function...