Question unit testing numerical algorithms

Skip Montanaro skip at pobox.com
Sun Dec 8 16:22:43 EST 2002


    Brian> However, when run there is a significantly small enough error to
    Brian> allow failure in the assertion A+B == SUM.

    Brian> How can I in general unit test algorithms and allow for a small
    Brian> enough error?

How about:

    import math

    class Tests(unittest.TestCase):
        def testAddition(self):
            EPS = 10e-6
            desired = Interval(1.28, 11.87)
            a = Interval(3.52, 5.83)
            b = Interval(-2.24, 6.04)
            sum = a + b
            self.failUnless(math.abs(sum.lower-desired.lower) <= EPS and
                            math.abs(sum.upper-desired.upper) <= EPS)

That is, you check to see if the absolute value of the difference between
the floating point values of interest is "close enough".  Also, check the
unittest.TestCase lib ref page for the various assertion methods defined.
In general you shouldn't use assert statements directly in your unit tests.

-- 
Skip Montanaro - skip at pobox.com
http://www.mojam.com/
http://www.musi-cal.com/




More information about the Python-list mailing list