Reputation: 7408
I've implemented a function which calculate annual mortgage repayment, and I want to test whether it generates the correct results.
I've put input mortgage values in an array, such as:
input_array([ 50000., 100000., 150000., ...])
and the corresponding results in another array:
output_array([ 3200.60, 6401.20, 9601.79, ...])
I'd like to take each of the elements in the input array as the input of my function and test the result is the same as output_array.
Is it possible to do it automatically rather than input the mortgage value and expected output in the assertEqual
function manually?
Many thanks.
Upvotes: 4
Views: 8108
Reputation: 1403
For numpy arrays, something like:
self.assertTrue(
numpy.allclose(calc_mortgage([10, 20, 30, 42], [1, 2, 3, 4]),
msg="calc_mortgage error"
)
Upvotes: 1
Reputation: 5611
You have two options. The first, just compare whole arrays:
expected_list = [1, 2, 3, 4]
output_list = calc_mortgage([10, 20, 30, 42])
self.assertEqual(expected_list, output_list)
The second, you can compare each element:
expected_list = [1, 2, 3, 4]
output_list = calc_mortgage([10, 20, 30, 42])
for pair in zip(expected_list, output_list):
self.assertEqual(pair[0], pair[1])
Upvotes: 4