Changeset 7097

Show
Ignore:
Timestamp:
07/04/09 04:56:11 (7 months ago)
Author:
stefan
Message:

Remove mentions of TestCase? subclassing and simplify.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/doc/TESTS.txt

    r7096 r7097  
    7878Every Python module, extension module, or subpackage in the SciPy 
    7979package 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'. 
     80Nose examines these files for test methods (named test*) and test 
     81classes (named Test*). 
    8682 
    8783Suppose 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:: 
     84function ``zzz()``.  To test this function you would create a test 
     85module 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 
     91More often, we need to group a number of tests together, so we create 
     92a test class:: 
    10493 
    10594  from numpy.testing import * 
     
    10897  from scipy.xxx.yyy import zzz 
    10998 
    110   class test_zzz(TestCase): 
     99  class TestZzz: 
    111100      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 
     106Within these test methods, ``assert()`` is used to test whether a 
     107certain assumption is valid. If the assert fails, the test fails. 
     108 
     109Sometimes it is convenient to run ``test_yyy.py`` by itself, so we add 
     110 
     111:: 
    114112 
    115113  if __name__ == "__main__": 
    116114      run_module_suite() 
    117115 
    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. 
     116at the bottom. 
    146117 
    147118Labeling tests with nose 
     
    155126  # numpy.testing module includes 'import decorators as dec' 
    156127  from numpy.testing import * 
     128 
    157129  @dec.slow 
    158130  def test_big(self): 
     
    161133Similarly for methods:: 
    162134 
    163   class test_zzz(TestCase): 
     135  class test_zzz: 
    164136      @dec.slow 
    165137      def test_simple(self): 
    166           assert zzz()=='Hello from zzz' 
     138          assert zzz() == 'Hello from zzz' 
    167139 
    168140Easier setup and teardown functions / methods 
     
    222194Note that 'check_even' is not itself a test (no 'test' in the name), 
    223195but '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. 
    225202 
    226203Doctests 
     
    254231for ``test_yyy.py`` is ``scipy/xxx/tests/test_yyy.py``. 
    255232 
    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:: 
     233Once the ``scipy/xxx/tests/test_yyy.py`` is written, its possible to 
     234run the tests by going to the ``tests/`` directory and typing:: 
    257235 
    258236  python test_yyy.py 
     
    267245-------------------------------- 
    268246 
    269 Usually however, adding the ``tests/`` directory to the python path 
     247Usually, however, adding the ``tests/`` directory to the python path 
    270248isn't desirable. Instead it would better to invoke the test straight 
    271249from the module ``xxx``. To this end, simply place the following lines 
     
    325303      ... 
    326304 
    327   class TestSolve(LinalgTestCase, TestCase): 
     305  class TestSolve(LinalgTestCase): 
    328306      def do(self, a, b): 
    329307          x = linalg.solve(a, b) 
     
    331309          assert imply(isinstance(b, matrix), isinstance(x, matrix)) 
    332310 
    333   class TestInv(LinalgTestCase, TestCase): 
     311  class TestInv(LinalgTestCase): 
    334312      def do(self, a, b): 
    335313          a_inv = linalg.inv(a) 
     
    341319``linalg.inv``.  The common test cases (for single-precision, 
    342320double-precision, etc. matrices) are collected in ``LinalgTestCase``. 
    343 Note that ``LinalgTestCase`` is not descended from ``TestCase``--if it 
    344 were, then nose would attempt to run ``LinalgTestCase.test_single`` 
    345 and ``LinalgTestCase.test_double``, which would fail because 
    346 ``LinalgTestCase`` has no ``do`` method.  Since ``TestSolve`` and 
    347 ``TestInv`` inherit from ``LinalgTestCase`` and ``TestCase``, nose 
    348 will run ``test_single`` and ``test_double`` for each class. 
    349321 
    350322Known failures & skipping tests