Testing

In this sub-package are two classes and some useful utilities for writing unit-tests

NumpyTestCase a subclass of unittest.TestCase which adds a measure method that can determine the elasped time to execute a code string and enhances the __call__ method

NumpyTest the test manager for NumPy which was extracted originally from the SciPy code base. This test manager makes it easy to add unit-tests to a package simply by creating a tests sub-directory with files named test_<module>.py. These test files should then define sub-classes of NumpyTestCase (or unittest.TestCase) named "test*". These classes should then define functions named "test*" or "bench*" or "check*" that contain the actual unit-tests. The first keyword argument should specify the level above which this test should be run.

To run the tests excecute NumpyTest(<package>).test(level=1, verbosity=1) which will run all tests above the given level using the given verbosity. Here <package> can be either a string or a previously imported module. You can get the level and verbosity arguments from sys.argv using NumpyTest(<package>).run() with -v or -verbosity and -l or -level as command-line arguments.

set_local_path (reldir=", level=l)

prepend local directory (+ reldir) to sys.path. The caller is responsible for removing this path using restore_path().

set _package_path (level= 1)

prepend package directory to sys.path. This should be called from a testJile.py that satisfies the tree structure: <somepath>/<somedir>/test_file.py. The, the first existing path name from the list <somepath>/build/lib.<platform>-<version>, <somepath>/.. is pre-pended to sys.path. The caller is responsible for removing this path using restore_path().

restore_path ()

Remove the first entry from sys.path.

assert_equal (actual, desired, err_msg=", verbose=l)

Raise an assertion error if the two items are not equal. Automatically calls as-sert_array_equal if actual or desired is an ndarray.

assert_almost_equal (actual, desired, decimal=7, err_msg=", verbose=l)

Raise an assertion error if the two items are not equal within decimal places. Automatically calls assert_array_almost_equal if actual or desired is an ndarray.

assert_approx_equal (actual, desired, significant=7, err_msg=", verbose=l)

Raise an assertion error if the two items are not equal to within the given significant digits. Does not work on arrays.

Raise an error if the two arrays x and y are not equal at every element.

Raise an error if the two arrays x and y have different shapes or if x is not less than y at every element.

assert_array_almost_equal (x, y, decimal=6, err_msg=")

Raise an error if x and y are not equal to decimal places at every element.

jiffies ()

Return a number of 1/100ths of a second that this process has been scheduled in user mode. Implemented using time.time() unless on Linux where the special /proc directory filesystem is used.

memusage ()

Return the virtual memory size in bytes of the running python. If the operation is not supported on the platform, then return None. This works only on linux for now.

Return an array of random numbers with the given shape using only the standard library random number generator.

runstring (astr, dict)

Run the given string in the dictionary provided. Functional form for (exec astr in dict) that is useful for the failUnlessRaises method of unittest.TestCase class.

0 0

Post a comment