Changes between Version 25 and Version 26 of TestingGuidelines

Show
Ignore:
Timestamp:
09/17/08 21:38:39 (5 years ago)
Author:
alan.mcintyre
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • TestingGuidelines

    v25 v26  
    1 '''CAUTION:  This page is in the process of being migrated from an older page.  Until this process is completed, information found here may be incorrect.''' 
    2 [[PageOutline]] 
    3 = Introduction = 
    4 !SciPy uses the [http://www.somethingaboutorange.com/mrl/projects/nose Nose testing system], with some additions from the !NumPy testing routines.  Nose is an extension of the unit testing framework offered by [http://docs.python.org/lib/module-unittest.html unittest.py]. 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 [http://en.wikipedia.org/wiki/Test-driven_development 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. 
    5  
    6 To run !SciPy's full test suite, use the following: 
    7 {{{ 
    8 >>> import scipy 
    9 >>> scipy.test() 
    10 }}} 
    11 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 scipy.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: 
    12 {{{ 
    13 >>> scipy.test(label='full', verbosity=2) # or 
    14 >>> scipy.test('full', 2) 
    15 }}} 
    16 Finally, if you are only interested in testing a subset of !SciPy, for example, the {{{integrate}}} module, use the following: 
    17 {{{ 
    18 >>> scipy.integrate.test() 
    19 }}} 
    20 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. 
    21  
    22 == Writing your own tests == 
    23 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}}}.  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'.  
    24  
    25 === {{{test_yyy.py}}} === 
    26 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. 
    27  
    28 ==== Standard unit test classes ==== 
    29  
    30 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: 
    31 {{{ 
    32 import sys 
    33 from scipy.testing import * 
    34  
    35 # import xxx symbols 
    36 from scipy.xxx.yyy import zzz 
    37  
    38 #Optional:  
    39 set_local_path() 
    40 # import modules that are located in the same directory as this file. 
    41 restore_path() 
    42  
    43 class test_zzz(TestCase): 
    44     def test_simple(self): 
    45         assert zzz()=='Hello from zzz' 
    46     #... 
    47  
    48 if __name__ == "__main__": 
    49     nose.run(argv=['', __file__]) 
    50 }}} 
    51  
    52 Note that all classes that are inherited from {{{TestCase}}} class, are picked up by the test runner when using {{{test()}}}. For more detailed information on defining test classes see the official documentation for the [http://docs.python.org/lib/module-unittest.html Python Unit testing framework]. 
    53  
    54 ==== Using test functions with nose ==== 
    55  
    56 This is as simple as making a function or functions with names including 'test': 
    57  
    58 {{{ 
    59 import sys 
    60 from scipy.testing import * 
    61  
    62 # import xxx symbols 
    63 from scipy.xxx.yyy import zzz 
    64  
    65 def test_simple(self): 
    66     assert zzz()=='Hello from zzz' 
    67  
    68  
    69 if __name__ == "__main__": 
    70     nose.run(argv=['', __file__]) 
    71 }}} 
    72  
    73 You can mix nose test functions and !TestCase classes in a single test file. 
    74  
    75 ==== Labeling tests with nose ==== 
    76  
    77 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: 
    78  
    79 {{{ 
    80 # scipy.testing module includes 'import decorators as dec' 
    81 from scipy.testing import * 
    82 @dec.slow 
    83 def test_big(self): 
    84     print 'Big, slow test' 
    85 }}} 
    86  
    87 Similarly for methods: 
    88  
    89 {{{ 
    90 class test_zzz(TestCase): 
    91     @dec.slow 
    92     def test_simple(self): 
    93         assert zzz()=='Hello from zzz' 
    94 }}} 
    95  
    96 ==== Easier setup and teardown functions / methods ==== 
    97  
    98 Nose looks for module level setup and teardown functions by name; thus: 
    99  
    100 {{{ 
    101 def setup(): 
    102     """Module-level setup""" 
    103     print 'doing setup' 
    104  
    105 def teardown(): 
    106     """Module-level teardown""" 
    107     print 'doing teardown' 
    108 }}} 
    109  
    110 You can add setup and teardown functions to functions and methods with nose decorators: 
    111  
    112 {{{ 
    113 from scipy.testing import * 
    114 def setup_func(): 
    115     """A trivial setup function.""" 
    116     global helpful_variable 
    117     helpful_variable = 'pleasant' 
    118     print "In setup_func" 
    119  
    120 def teardown_func(): 
    121     """A trivial teardown function.""" 
    122     global helpful_variable 
    123     del helpful_variable 
    124     print "In teardown_func" 
    125  
    126 @nose.with_setup(setup_func, teardown_func) 
    127 def test_with_extras(): 
    128     """This test uses the setup/teardown functions.""" 
    129     global helpful_variable 
    130     print "  In test_with_extras" 
    131     print "  Helpful is %s" % helpful_variable 
    132 }}} 
    133  
    134 ==== Parametric tests ==== 
    135  
    136 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: 
    137  
    138 {{{ 
    139 def check_even(n, nn): 
    140     """A check function to be used in a test generator.""" 
    141     assert n % 2 == 0 or nn % 2 == 0 
    142  
    143 def test_evens(): 
    144     for i in range(0,4,2): 
    145         yield check_even, i, i*3 
    146 }}} 
    147  
    148 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. 
    149  
    150 === {{{tests/}}} === 
    151 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}}}. 
    152  
    153 Once the {{{scipy/xxx/tests/test_yyy.py}}} is written, its possible to run the tests by going to the {{{tests/}}} directory and typing: 
    154 {{{ 
    155 python test_yyy.py 
    156 }}} 
    157 Or if you add {{{scipy/xxx/tests/}}} to the Python path, you could run the tests interactively in the interpreter like this: 
    158 {{{ 
    159 >>> import test_yyy.py 
    160 >>> test_yyy.test() 
    161 }}} 
    162  
    163 === {{{__init__.py}}} and {{{setup.py}}} === 
    164 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: 
    165 {{{ 
    166 ... 
    167 def test(level=1, verbosity=1): 
    168     from numpy.testing import NumpyTest 
    169     return NumpyTest().test(level, verbosity) 
    170 }}} 
    171 You will also need to add the tests directory in the configuration section of your setup.py: 
    172 {{{ 
    173 ... 
    174 def configuration(parent_package='', top_path=None): 
    175     ... 
    176     config.add_data_dir('tests') 
    177     return config 
    178 ... 
    179 }}} 
    180 Now you can do the following to test your module: 
    181 {{{ 
    182 >>> import scipy 
    183 >>> scipy.xxx.test() 
    184 }}} 
    185  
    186 Also, when invoking the entire !SciPy test suite, your tests will be found and run: 
    187 {{{ 
    188 >>> import scipy 
    189 >>> scipy.test()  
    190 # your tests are included and run automatically! 
    191 }}} 
     1Please see [http://projects.scipy.org/scipy/numpy/wiki/TestingGuidelines TestingGuidelines]