[Numpy-discussion] using assertEqual in unittest to test two np.ndarray?

josef.pktd@gmai... josef.pktd@gmai...
Fri Mar 20 21:15:46 CDT 2009


2009/3/20 Grissiom <chaos.proton@gmail.com>:
> On Sat, Mar 21, 2009 at 05:03, <josef.pktd@gmail.com> wrote:
>>
>> for testing purposes it is available in numpy testing:
>> from numpy.testing import assert_equal,  assert_almost_equal,
>> assert_array_equal
>> >>> a = np.array([  1.,   2.,  np.NaN,   4.])
>> >>> assert_array_equal(a,a)
>>
>> does not raise AssertionError
>>
>> >>> assert_array_equal(a,a+1)
>> Traceback (most recent call last):
>>  File "<pyshell#6>", line 1, in <module>
>>    assert_array_equal(a,a+1)
>>  File "C:\Programs\Python25\lib\site-packages\numpy\testing\utils.py",
>> line 303, in assert_array_equal
>>    verbose=verbose, header='Arrays are not equal')
>>  File "C:\Programs\Python25\lib\site-packages\numpy\testing\utils.py",
>> line 295, in assert_array_compare
>>    raise AssertionError(msg)
>> AssertionError:
>> Arrays are not equal
>>
>> (mismatch 100.0%)
>>  x: array([  1.,   2.,  NaN,   4.])
>>  y: array([  2.,   3.,  NaN,   5.])
>
> Great! Thanks! In my case, a NaN is indicating something goes wrong and I
> want testing fail on it. So it meet my demand.
>
> One thing more, when I help(np.testing) I only got this:
> =============================================
> Help on package numpy.testing in numpy:
>
> NAME
>     numpy.testing - Common test support for all numpy test scripts.
>
> FILE
>     /usr/lib/python2.5/site-packages/numpy/testing/__init__.py
>
> DESCRIPTION
>     This single module should provide all the common functionality for numpy
> tests
>     in a single location, so that test scripts can just import it and work
> right
>     away.
>
> PACKAGE CONTENTS
>     decorators
>     noseclasses
>     nosetester
>     nulltester
>     numpytest
>     parametric
>     setup
>     setupscons
>     utils
>
> DATA
>     verbose = 0
> ================================================
>
> So I have to dir it to see is there any other useful functions. It will be
> perfect to document package method like assert_equal here.
>
> Thanks very much~;)
>
> --
> Cheers,
> Grissiom
>

The testing assert functions are not well documented, I usually just
use assert_array_almost_equal with decimal precision for float arrays.
useful is also assert_()  which is better than the assert statement
since it survives optimization flag for python compile.

You can browse the help editor
http://docs.scipy.org/numpy/docs/numpy.testing.utils/
To see the precise definition and difference between the different
asserts you have to check the source, source button on editor page.

There are also the
http://projects.scipy.org/numpy/wiki/TestingGuidelines , if you
haven't seen them yet, they describe the general test setup with nose
but not the assert functions.

Josef

If you know where to look there is some information:

>>> help(numpy.testing.utils)
Help on module numpy.testing.utils in numpy.testing:

NAME
    numpy.testing.utils - Utility function to facilitate testing.

FILE
    c:\programs\python25\lib\site-packages\numpy\testing\utils.py

FUNCTIONS
    assert_almost_equal(actual, desired, decimal=7, err_msg='', verbose=True)
        Raise an assertion if two items are not equal.

        I think this should be part of unittest.py

        The test i

...


More information about the Numpy-discussion mailing list