If the elements must be matched in the order in which they appear, you can create a utility function that accepts parameters and checks that they satisfy some condition:
def close_round(item1, item2, rounding_param): """Determines closeness for our test purposes""" return round(item1, rounding_param) == round(item2, rounding_param)
Then you can use this in a test case as follows:
assert len(yields1) == len(list_of_yields) index = 0 for result, expected in zip(yields, list_of_yields): self.assertTrue(close_round(result, expected, 7), msg="Test failed: got {0} expected {1} at index {2}".format(result, expected, index)) index+=1
You may find this type of template useful, in which case you could create a function that does this:
def test_lists_close(self, lst1, lst2, comp_function): """Tests if lst1 and lst2 are equal by applying comp_function to every element of both lists""" assert len(lst1) == len(lst2) index = 0 for result, expected in zip(yields, list_of_yields): self.assertTrue(comp_function(result, expected), msg="Test failed: got {0} expected {1} at index {2}".format(result, expected, index)) index+=1
If you have used it a lot, you probably want to check this feature as well.
source share