Question unit testing numerical algorithms

Tom Loredo loredo at astro.cornell.edu
Mon Dec 9 16:58:54 EST 2002


The scipy_test module in the scipy package (http://www.scipy.org/)
has an assert_almost_equal function that raises an assertion if
two arguments don't agree to a specified # of decimal places.  This
is so widely needed that I think this kind of thing belongs in
unittest.py, not in an obscure module in scipy that is only used
for scipy internal testing and is not well documented.  

-Tom Loredo

>From scipy:

def assert_almost_equal(actual,desired,decimal=7,err_msg='',verbose=1):
    """ Raise an assertion if two items are not
        equal.  I think this should be part of unittest.py
    """
    msg = '\nItems are not equal:\n' + err_msg
    desired = round(desired,decimal)
    actual = round(actual,decimal)
    try:
        if ( verbose and len(str(desired)) < 100 and len(str(actual)) ):
            msg =  msg \
                 + 'DESIRED: ' + str(desired) \
                 + '\nACTUAL: ' + str(actual)
    except:
        msg =  msg \
             + 'DESIRED: ' + str(desired) \
             + '\nACTUAL: ' + str(actual)
    assert desired == actual, msg



More information about the Python-list mailing list