diff options
author | Stefan van der Walt <stefan@sun.ac.za> | 2009-07-04 09:55:36 +0000 |
---|---|---|
committer | Stefan van der Walt <stefan@sun.ac.za> | 2009-07-04 09:55:36 +0000 |
commit | bc2a006e82b110892f675c95e3f0f696b61a60b7 (patch) | |
tree | c428f709f2fd347470a4c2b5044c0c00287d39c3 | |
parent | d35eac6f1095727b430ca6416b76a9b43c715c1c (diff) | |
download | numpy-bc2a006e82b110892f675c95e3f0f696b61a60b7.tar.gz |
Wrap long lines.
-rw-r--r-- | doc/TESTS.txt | 187 |
1 files changed, 151 insertions, 36 deletions
diff --git a/doc/TESTS.txt b/doc/TESTS.txt index 00d89d21e..63de7327d 100644 --- a/doc/TESTS.txt +++ b/doc/TESTS.txt @@ -8,41 +8,99 @@ NumPy/SciPy Testing Guidelines Introduction '''''''''''' -SciPy uses the `Nose testing system <http://www.somethingaboutorange.com/mrl/projects/nose>`__, with some minor convenience features added. Nose is an extension of the unit testing framework offered by `unittest.py <http://docs.python.org/lib/module-unittest.html>`__. Our goal is that every module and package in SciPy should have a thorough set of unit tests. These tests should exercise the full functionality of a given routine as well as its robustness to erroneous or unexpected input arguments. Long experience has shown that by far the best time to write the tests is before you write or change the code - this is `test-driven development <http://en.wikipedia.org/wiki/Test-driven_development>`__. The arguments for this can sound rather abstract, but we can assure you that you will find that writing the tests first leads to more robust and better designed code. Well-designed tests with good coverage make an enormous difference to the ease of refactoring. Whenever a new bug is found in a routine, you should write a new test for that specific case and add it to the test suite to prevent that bug from creeping back in unnoticed. +SciPy uses the `Nose testing system +<http://www.somethingaboutorange.com/mrl/projects/nose>`__, with some +minor convenience features added. Nose is an extension of the unit +testing framework offered by `unittest.py +<http://docs.python.org/lib/module-unittest.html>`__. Our goal is that +every module and package in SciPy should have a thorough set of unit +tests. These tests should exercise the full functionality of a given +routine as well as its robustness to erroneous or unexpected input +arguments. Long experience has shown that by far the best time to +write the tests is before you write or change the code - this is +`test-driven development +<http://en.wikipedia.org/wiki/Test-driven_development>`__. The +arguments for this can sound rather abstract, but we can assure you +that you will find that writing the tests first leads to more robust +and better designed code. Well-designed tests with good coverage make +an enormous difference to the ease of refactoring. Whenever a new bug +is found in a routine, you should write a new test for that specific +case and add it to the test suite to prevent that bug from creeping +back in unnoticed. To run SciPy's full test suite, use the following:: >>> import scipy >>> scipy.test() -SciPy uses the testing framework from NumPy (specifically ``numpy.testing``), so all the SciPy examples shown here are also applicable to NumPy. So NumPy's full test suite can be run as follows:: +SciPy uses the testing framework from NumPy (specifically +``numpy.testing``), so all the SciPy examples shown here are also +applicable to NumPy. So NumPy's full test suite can be run as +follows:: >>> import numpy >>> numpy.test() -The test method may take two or more arguments; the first is a string label specifying what should be tested and the second is an integer giving the level of output verbosity. See the docstring for numpy.test for details. The default value for the label is 'fast' - which will run the standard tests. The string 'full' will run the full battery of tests, including those identified as being slow to run. If the verbosity is 1 or less, the tests will just show information messages about the tests that are run; but if it is greater than 1, then the tests will also provide warnings on missing tests. So if you want to run every test and get messages about which modules don't have tests:: +The test method may take two or more arguments; the first is a string +label specifying what should be tested and the second is an integer +giving the level of output verbosity. See the docstring for numpy.test +for details. The default value for the label is 'fast' - which will +run the standard tests. The string 'full' will run the full battery +of tests, including those identified as being slow to run. If the +verbosity is 1 or less, the tests will just show information messages +about the tests that are run; but if it is greater than 1, then the +tests will also provide warnings on missing tests. So if you want to +run every test and get messages about which modules don't have tests:: - >>> scipy.test(label='full', verbosity=2) # or - >>> scipy.test('full', 2) + >>> scipy.test(label='full', verbosity=2) # or scipy.test('full', 2) -Finally, if you are only interested in testing a subset of SciPy, for example, the ``integrate`` module, use the following:: +Finally, if you are only interested in testing a subset of SciPy, for +example, the ``integrate`` module, use the following:: >>> scipy.integrate.test() -The rest of this page will give you a basic idea of how to add unit tests to modules in SciPy. It is extremely important for us to have extensive unit testing since this code is going to be used by scientists and researchers and is being developed by a large number of people spread across the world. So, if you are writing a package that you'd like to become part of SciPy, please write the tests as you develop the package. Also since much of SciPy is legacy code that was originally written without unit tests, there are still several modules that don't have tests yet. Please feel free to choose one of these modules to develop test for either after or even as you read through this introduction. +The rest of this page will give you a basic idea of how to add unit +tests to modules in SciPy. It is extremely important for us to have +extensive unit testing since this code is going to be used by +scientists and researchers and is being developed by a large number of +people spread across the world. So, if you are writing a package that +you'd like to become part of SciPy, please write the tests as you +develop the package. Also since much of SciPy is legacy code that was +originally written without unit tests, there are still several modules +that don't have tests yet. Please feel free to choose one of these +modules to develop test for either after or even as you read through +this introduction. Writing your own tests '''''''''''''''''''''' -Every Python module, extension module, or subpackage in the SciPy package directory should have a corresponding ``test_<name>.py`` file. The nose framework picks up tests by first looking for any functions in the file that have test-related names (see below), or classes that inherit from ``unittest.TestCase`` (which is also made available as ``numpy.testing.TestCase``. Any methods of these classes, that also have test-related names, are considered tests. A test-related name is simply a function or method name containing 'test'. - -Suppose you have a SciPy module ``scipy/xxx/yyy.py`` containing a function ``zzz()``. To test this you would start by creating a test module called ``test_yyy.py``. There are several different ways to implement tests using the nose / SciPy system. There is the standard unittest way and the nose test function way. +Every Python module, extension module, or subpackage in the SciPy +package directory should have a corresponding ``test_<name>.py`` file. +The nose framework picks up tests by first looking for any functions +in the file that have test-related names (see below), or classes that +inherit from ``unittest.TestCase`` (which is also made available as +``numpy.testing.TestCase``. Any methods of these classes, that also +have test-related names, are considered tests. A test-related name is +simply a function or method name containing 'test'. + +Suppose you have a SciPy module ``scipy/xxx/yyy.py`` containing a +function ``zzz()``. To test this you would start by creating a test +module called ``test_yyy.py``. There are several different ways to +implement tests using the nose / SciPy system. There is the standard +unittest way and the nose test function way. Standard unit test classes -------------------------- -You can use the traditional unittest system by making your test file include a class that tests ``zzz()``. The test class inherits from the TestCase class, and has test methods that test various aspects of ``zzz()``. Within these test methods, ``assert()`` is used to test whether some case is true. If the assert fails, the test fails. The line ``nose.run(...)`` function actually runs the test suite. A minimal example of a ``test_yyy.py`` file that implements tests for a Scipy package module ``scipy.xxx.yyy``, is shown below:: +You can use the traditional unittest system by making your test file +include a class that tests ``zzz()``. The test class inherits from the +TestCase class, and has test methods that test various aspects of +``zzz()``. Within these test methods, ``assert()`` is used to test +whether some case is true. If the assert fails, the test fails. The +line ``nose.run(...)`` function actually runs the test suite. A +minimal example of a ``test_yyy.py`` file that implements tests for a +Scipy package module ``scipy.xxx.yyy``, is shown below:: from numpy.testing import * @@ -58,12 +116,17 @@ You can use the traditional unittest system by making your test file include a c run_module_suite() -Note that all classes that are inherited from ``TestCase`` class, are picked up by the test runner. For more detailed information on defining test classes see the official documentation for the `Python Unit testing framework <http://docs.python.org/lib/module-unittest.html>`__. +Note that all classes that are inherited from ``TestCase`` class, are +picked up by the test runner. For more detailed information on +defining test classes see the official documentation for the `Python +Unit testing framework +<http://docs.python.org/lib/module-unittest.html>`__. Using test functions with nose ------------------------------ -This is as simple as making a function or functions with names including 'test':: +This is as simple as making a function or functions with names +including 'test':: from numpy.testing import * @@ -78,12 +141,16 @@ This is as simple as making a function or functions with names including 'test': run_module_suite() -You can mix nose test functions and TestCase classes in a single test file. +You can mix nose test functions and TestCase classes in a single test +file. Labeling tests with nose ------------------------ -Unlabeled tests like the ones above are run in the default ``scipy.test()`` run. If you want to label your test as slow - and therefore reserved for a full ``scipy.test(label='full')`` run, you can label it with a nose decorator:: +Unlabeled tests like the ones above are run in the default +``scipy.test()`` run. If you want to label your test as slow - and +therefore reserved for a full ``scipy.test(label='full')`` run, you +can label it with a nose decorator:: # numpy.testing module includes 'import decorators as dec' from numpy.testing import * @@ -101,7 +168,8 @@ Similarly for methods:: Easier setup and teardown functions / methods --------------------------------------------- -Nose looks for module level setup and teardown functions by name; thus:: +Nose looks for module level setup and teardown functions by name; +thus:: def setup(): """Module-level setup""" @@ -112,7 +180,8 @@ Nose looks for module level setup and teardown functions by name; thus:: print 'doing teardown' -You can add setup and teardown functions to functions and methods with nose decorators:: +You can add setup and teardown functions to functions and methods with +nose decorators:: import nose from numpy.testing import * @@ -138,7 +207,9 @@ You can add setup and teardown functions to functions and methods with nose deco Parametric tests ---------------- -One very nice feature of nose is allowing easy testing across a range of parameters - a nasty problem for standard unit tests. It does this with test generators:: +One very nice feature of nose is allowing easy testing across a range +of parameters - a nasty problem for standard unit tests. It does this +with test generators:: def check_even(n, nn): """A check function to be used in a test generator.""" @@ -148,31 +219,46 @@ One very nice feature of nose is allowing easy testing across a range of paramet for i in range(0,4,2): yield check_even, i, i*3 -Note that 'check_even' is not itself a test (no 'test' in the name), but 'test_evens' is a generator that returns a series of tests, using 'check_even', across a range of inputs. Nice. +Note that 'check_even' is not itself a test (no 'test' in the name), +but 'test_evens' is a generator that returns a series of tests, using +'check_even', across a range of inputs. Nice. Doctests -------- -Doctests are a convenient way of documenting the behavior a function and allowing that behavior to be tested at the same time. The output of an interactive Python session can be included in the docstring of a function, and the test framework can run the example and compare the actual output to the expected output. +Doctests are a convenient way of documenting the behavior a function +and allowing that behavior to be tested at the same time. The output +of an interactive Python session can be included in the docstring of a +function, and the test framework can run the example and compare the +actual output to the expected output. -The doctests can be run by adding the ``doctests`` argument to the ``test()`` call; for example, to run all tests (including doctests) for numpy.lib:: +The doctests can be run by adding the ``doctests`` argument to the +``test()`` call; for example, to run all tests (including doctests) +for numpy.lib:: >>> import numpy as np >>> np.lib.test(doctests=True) -The doctests are run as if they are in a fresh Python instance which has executed ``import numpy as np`` (tests that are part of the SciPy package also have an implicit ``import scipy as sp``). +The doctests are run as if they are in a fresh Python instance which +has executed ``import numpy as np`` (tests that are part of the SciPy +package also have an implicit ``import scipy as sp``). ``tests/`` ---------- -Rather than keeping the code and the tests in the same directory, we put all the tests for a given subpackage in a ``tests/`` subdirectory. For our example, if it doesn't all ready exist you will need to create a ``tests/`` directory in ``scipy/xxx/``. So the path for ``test_yyy.py`` is ``scipy/xxx/tests/test_yyy.py``. +Rather than keeping the code and the tests in the same directory, we +put all the tests for a given subpackage in a ``tests/`` +subdirectory. For our example, if it doesn't all ready exist you will +need to create a ``tests/`` directory in ``scipy/xxx/``. So the path +for ``test_yyy.py`` is ``scipy/xxx/tests/test_yyy.py``. Once the ``scipy/xxx/tests/test_yyy.py`` is written, its possible to run the tests by going to the ``tests/`` directory and typing:: python test_yyy.py -Or if you add ``scipy/xxx/tests/`` to the Python path, you could run the tests interactively in the interpreter like this:: +Or if you add ``scipy/xxx/tests/`` to the Python path, you could run +the tests interactively in the interpreter like this:: >>> import test_yyy >>> test_yyy.test() @@ -180,14 +266,18 @@ Or if you add ``scipy/xxx/tests/`` to the Python path, you could run the tests i ``__init__.py`` and ``setup.py`` -------------------------------- -Usually however, adding the ``tests/`` directory to the python path isn't desirable. Instead it would better to invoke the test straight from the module ``xxx``. To this end, simply place the following lines at the end of your package's ``__init__.py`` file:: +Usually however, adding the ``tests/`` directory to the python path +isn't desirable. Instead it would better to invoke the test straight +from the module ``xxx``. To this end, simply place the following lines +at the end of your package's ``__init__.py`` file:: ... def test(level=1, verbosity=1): from numpy.testing import Tester return Tester().test(level, verbosity) -You will also need to add the tests directory in the configuration section of your setup.py:: +You will also need to add the tests directory in the configuration +section of your setup.py:: ... def configuration(parent_package='', top_path=None): @@ -201,10 +291,11 @@ Now you can do the following to test your module:: >>> import scipy >>> scipy.xxx.test() -Also, when invoking the entire SciPy test suite, your tests will be found and run:: +Also, when invoking the entire SciPy test suite, your tests will be +found and run:: >>> import scipy - >>> scipy.test() + >>> scipy.test() # your tests are included and run automatically! Tips & Tricks @@ -213,7 +304,12 @@ Tips & Tricks Creating many similar tests --------------------------- -If you have a collection of tests that must be run multiple times with minor variations, it can be helpful to create a base class containing all the common tests, and then create a subclass for each variation. Several examples of this technique exist in NumPy; below are excerpts from one in `numpy/linalg/tests/test_linalg.py <http://svn.scipy.org/svn/numpy/trunk/numpy/linalg/tests/test_linalg.py>`__:: +If you have a collection of tests that must be run multiple times with +minor variations, it can be helpful to create a base class containing +all the common tests, and then create a subclass for each variation. +Several examples of this technique exist in NumPy; below are excerpts +from one in `numpy/linalg/tests/test_linalg.py +<http://svn.scipy.org/svn/numpy/trunk/numpy/linalg/tests/test_linalg.py>`__:: class LinalgTestCase: def test_single(self): @@ -240,12 +336,24 @@ If you have a collection of tests that must be run multiple times with minor var assert_almost_equal(dot(a, a_inv), identity(asarray(a).shape[0])) assert imply(isinstance(a, matrix), isinstance(a_inv, matrix)) -In this case, we wanted to test solving a linear algebra problem using matrices of several data types, using ``linalg.solve`` and ``linalg.inv``. The common test cases (for single-precision, double-precision, etc. matrices) are collected in ``LinalgTestCase``. Note that ``LinalgTestCase`` is not descended from ``TestCase``--if it were, then nose would attempt to run ``LinalgTestCase.test_single`` and ``LinalgTestCase.test_double``, which would fail because ``LinalgTestCase`` has no ``do`` method. Since ``TestSolve`` and ``TestInv`` inherit from ``LinalgTestCase`` and ``TestCase``, nose will run ``test_single`` and ``test_double`` for each class. +In this case, we wanted to test solving a linear algebra problem using +matrices of several data types, using ``linalg.solve`` and +``linalg.inv``. The common test cases (for single-precision, +double-precision, etc. matrices) are collected in ``LinalgTestCase``. +Note that ``LinalgTestCase`` is not descended from ``TestCase``--if it +were, then nose would attempt to run ``LinalgTestCase.test_single`` +and ``LinalgTestCase.test_double``, which would fail because +``LinalgTestCase`` has no ``do`` method. Since ``TestSolve`` and +``TestInv`` inherit from ``LinalgTestCase`` and ``TestCase``, nose +will run ``test_single`` and ``test_double`` for each class. Known failures & skipping tests ------------------------------- -Sometimes you might want to skip a test or mark it as a known failure, such as when the test suite is being written before the code it's meant to test, or if a test only fails on a particular architecture. The decorators from numpy.testing.dec can be used to do this. +Sometimes you might want to skip a test or mark it as a known failure, +such as when the test suite is being written before the code it's +meant to test, or if a test only fails on a particular architecture. +The decorators from numpy.testing.dec can be used to do this. To skip a test, simply use ``skipif``:: @@ -255,7 +363,10 @@ To skip a test, simply use ``skipif``:: def test_something(foo): ... -The test is marked as skipped if ``SkipMyTest`` evaluates to nonzero, and the message in verbose test output is the second argument given to ``skipif``. Similarly, a test can be marked as a known failure by using ``knownfailureif``:: +The test is marked as skipped if ``SkipMyTest`` evaluates to nonzero, +and the message in verbose test output is the second argument given to +``skipif``. Similarly, a test can be marked as a known failure by +using ``knownfailureif``:: from numpy.testing import * @@ -263,8 +374,12 @@ The test is marked as skipped if ``SkipMyTest`` evaluates to nonzero, and the me def test_something_else(foo): ... -Of course, a test can be unconditionally skipped or marked as a known failure by passing ``True`` as the first argument to ``skipif`` or ``knownfailureif``, respectively. - -A total of the number of skipped and known failing tests is displayed at the end of the test run. Skipped tests are marked as ``'S'`` in the test results (or ``'SKIPPED'`` for ``verbosity > 1``), and known failing tests are marked as ``'K'`` (or ``'KNOWN'`` if ``verbosity > 1``). - +Of course, a test can be unconditionally skipped or marked as a known +failure by passing ``True`` as the first argument to ``skipif`` or +``knownfailureif``, respectively. +A total of the number of skipped and known failing tests is displayed +at the end of the test run. Skipped tests are marked as ``'S'`` in +the test results (or ``'SKIPPED'`` for ``verbosity > 1``), and known +failing tests are marked as ``'K'`` (or ``'KNOWN'`` if ``verbosity > +1``). |