Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Python Unittest to test results in an array

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.

like image 300
ChangeMyName Avatar asked Sep 13 '25 23:09

ChangeMyName


2 Answers

assertListEqual(map(f, input_array), output_array)
like image 146
poe123 Avatar answered Sep 15 '25 14:09

poe123


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])
like image 27
Igor Pomaranskiy Avatar answered Sep 15 '25 14:09

Igor Pomaranskiy