4

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.

3 Answers 3

5
assertListEqual(map(f, input_array), output_array)
Sign up to request clarification or add additional context in comments.

Comments

4

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])

Comments

1

For numpy arrays, something like:

self.assertTrue(
    numpy.allclose(calc_mortgage([10, 20, 30, 42], [1, 2, 3, 4]),
    msg="calc_mortgage error"
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.