Changeset 7097
- Timestamp:
- 07/04/09 04:56:11 (7 months ago)
- Files:
-
- 1 modified
-
trunk/doc/TESTS.txt (modified) (10 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/doc/TESTS.txt
r7096 r7097 78 78 Every Python module, extension module, or subpackage in the SciPy 79 79 package directory should have a corresponding ``test_<name>.py`` file. 80 The nose framework picks up tests by first looking for any functions 81 in the file that have test-related names (see below), or classes that 82 inherit from ``unittest.TestCase`` (which is also made available as 83 ``numpy.testing.TestCase``. Any methods of these classes, that also 84 have test-related names, are considered tests. A test-related name is 85 simply a function or method name containing 'test'. 80 Nose examines these files for test methods (named test*) and test 81 classes (named Test*). 86 82 87 83 Suppose you have a SciPy module ``scipy/xxx/yyy.py`` containing a 88 function ``zzz()``. To test this you would start by creating a test 89 module called ``test_yyy.py``. There are several different ways to 90 implement tests using the nose / SciPy system. There is the standard 91 unittest way and the nose test function way. 92 93 Standard unit test classes 94 -------------------------- 95 96 You can use the traditional unittest system by making your test file 97 include a class that tests ``zzz()``. The test class inherits from the 98 TestCase class, and has test methods that test various aspects of 99 ``zzz()``. Within these test methods, ``assert()`` is used to test 100 whether some case is true. If the assert fails, the test fails. The 101 line ``nose.run(...)`` function actually runs the test suite. A 102 minimal example of a ``test_yyy.py`` file that implements tests for a 103 Scipy package module ``scipy.xxx.yyy``, is shown below:: 84 function ``zzz()``. To test this function you would create a test 85 module called ``test_yyy.py``. If you only need to test one aspect of 86 ``zzz``, you can simply add a test function:: 87 88 def test_zzz(): 89 assert zzz() == 'Hello from zzz' 90 91 More often, we need to group a number of tests together, so we create 92 a test class:: 104 93 105 94 from numpy.testing import * … … 108 97 from scipy.xxx.yyy import zzz 109 98 110 class test_zzz(TestCase):99 class TestZzz: 111 100 def test_simple(self): 112 assert zzz()=='Hello from zzz' 113 #... 101 assert zzz() == 'Hello from zzz' 102 103 def test_invalid_parameter(self): 104 assert_raises(...) 105 106 Within these test methods, ``assert()`` is used to test whether a 107 certain assumption is valid. If the assert fails, the test fails. 108 109 Sometimes it is convenient to run ``test_yyy.py`` by itself, so we add 110 111 :: 114 112 115 113 if __name__ == "__main__": 116 114 run_module_suite() 117 115 118 119 Note that all classes that are inherited from ``TestCase`` class, are 120 picked up by the test runner. For more detailed information on 121 defining test classes see the official documentation for the `Python 122 Unit testing framework 123 <http://docs.python.org/lib/module-unittest.html>`__. 124 125 Using test functions with nose 126 ------------------------------ 127 128 This is as simple as making a function or functions with names 129 including 'test':: 130 131 from numpy.testing import * 132 133 # import xxx symbols 134 from scipy.xxx.yyy import zzz 135 136 def test_simple(self): 137 assert zzz()=='Hello from zzz' 138 139 140 if __name__ == "__main__": 141 run_module_suite() 142 143 144 You can mix nose test functions and TestCase classes in a single test 145 file. 116 at the bottom. 146 117 147 118 Labeling tests with nose … … 155 126 # numpy.testing module includes 'import decorators as dec' 156 127 from numpy.testing import * 128 157 129 @dec.slow 158 130 def test_big(self): … … 161 133 Similarly for methods:: 162 134 163 class test_zzz (TestCase):135 class test_zzz: 164 136 @dec.slow 165 137 def test_simple(self): 166 assert zzz() =='Hello from zzz'138 assert zzz() == 'Hello from zzz' 167 139 168 140 Easier setup and teardown functions / methods … … 222 194 Note that 'check_even' is not itself a test (no 'test' in the name), 223 195 but 'test_evens' is a generator that returns a series of tests, using 224 'check_even', across a range of inputs. Nice. 196 'check_even', across a range of inputs. 197 198 .. warning:: 199 200 Parametric tests cannot be implemented on classes derived from 201 UnitTest. 225 202 226 203 Doctests … … 254 231 for ``test_yyy.py`` is ``scipy/xxx/tests/test_yyy.py``. 255 232 256 Once the ``scipy/xxx/tests/test_yyy.py`` is written, its possible to run the tests by going to the ``tests/`` directory and typing:: 233 Once the ``scipy/xxx/tests/test_yyy.py`` is written, its possible to 234 run the tests by going to the ``tests/`` directory and typing:: 257 235 258 236 python test_yyy.py … … 267 245 -------------------------------- 268 246 269 Usually however, adding the ``tests/`` directory to the python path247 Usually, however, adding the ``tests/`` directory to the python path 270 248 isn't desirable. Instead it would better to invoke the test straight 271 249 from the module ``xxx``. To this end, simply place the following lines … … 325 303 ... 326 304 327 class TestSolve(LinalgTestCase , TestCase):305 class TestSolve(LinalgTestCase): 328 306 def do(self, a, b): 329 307 x = linalg.solve(a, b) … … 331 309 assert imply(isinstance(b, matrix), isinstance(x, matrix)) 332 310 333 class TestInv(LinalgTestCase , TestCase):311 class TestInv(LinalgTestCase): 334 312 def do(self, a, b): 335 313 a_inv = linalg.inv(a) … … 341 319 ``linalg.inv``. The common test cases (for single-precision, 342 320 double-precision, etc. matrices) are collected in ``LinalgTestCase``. 343 Note that ``LinalgTestCase`` is not descended from ``TestCase``--if it344 were, then nose would attempt to run ``LinalgTestCase.test_single``345 and ``LinalgTestCase.test_double``, which would fail because346 ``LinalgTestCase`` has no ``do`` method. Since ``TestSolve`` and347 ``TestInv`` inherit from ``LinalgTestCase`` and ``TestCase``, nose348 will run ``test_single`` and ``test_double`` for each class.349 321 350 322 Known failures & skipping tests
