I'm writing some code that works with both very large and very small floating point numbers (for example, 1e-150 could be a valid answer). To unit test this I'd like to compare floats to a number of significant figures instead of decimal places, so I have the following.
import unittest as ut
from numpy.testing import assert_approx_equal
class newTestCase(ut.TestCase):
"""Extends the basic unittest TestCase."""
def assertSFAlmostEqual(self, a, b, places=7):
"""Uses numpy to test if two floats are the same but to a defined
number of significant figures rather than decimal places.
Args:
a: float to be compared
b: float to be compared
places: number of significant figures to match. unittest default
for assertAlmostEqual is 7, so 7 is the default here
"""
if isinstance(a, float) != True or isinstance(b, float) != True:
raise TypeError
raised = False
try:
assert_approx_equal(a, b, significant=places)
except:
raised = True
self.assertFalse(raised, "FLoats %g and %g are not equal to %i "
"significant figures" % (a, b, places))
Which seems to work fine, but I'm planning to use this in a lot of places, so I'd like to be certain that it really works correctly. My question is how can I do this most sensibly? Is there an proper mechanism to unit test a unit test?
I found what may be an answer here,
How to unittest unittest TestCases
but I don't understand how this works.
Thanks very much in advance!