Package Biskit :: Module test
[hide private]
[frames] | no frames]

Module test

source code

Automatic Biskit module testing

Module tests are extremely important to keep the Biskit package manageable and functional. Every Biskit module should contain a class derrived from BiskitTest (conventionally called Test) that comprises one or more test functions. BiskitTestLoader then automatically extracts all BiskitTest child classes from the whole package and bundles them into a FilteredTestSuite.

Test cases that are LONG, depend on PVM or external programs (EXE) or are obsolete (OLD) should be marked with the appropriate tag(s) by overriding BiskitTest.TAGS on the class level. Test cases can then be filtered out or included into the FilteredTestSuite according to their TAGS.

Originally, testing code was simply in the __main__ section of each module where we could execute it directly from emacs (or with python -i) for interactive debugging. By comparison, unittest test fixtures are less easy to execute stand-alone and intermediate variables remain hidden within the test instance. The localTest() removes this hurdle and runs the test code of a single module as if it would be executed directly in __main__. Simply putting the localTest() function without parameters into the __main__ section of your module is enough. Test.test_* methods should assign intermediate and final results to self.|something| variables -- localTest will then push all self.* fields into the global namespace for interactive debugging.

Usage

By way of example, a Test case for MyModule would look like this:
 class MyClass:
     ...

 ### Module testing ###
 import Biskit.test as BT

 class Test(BT.BiskitTest):
     """Test MyModule"""

     TAGS = [ BT.LONG ]

     def test_veryLongComputation( self ):
         """MyModule.veryLongComputation test"""

         self.m = MyClass()
         self.result = self.m.veryLongComputation()

         if self.local:   ## only if the module is executed directly
             print self.result 
             
         self.assertEqual( self.result, 42, 'unexpected result' )


 if __name__ == '__main__':

     ## run Test and push self.* fields into global namespace
     BT.localTest( )

     print result  ## works thanks to some namespace magic in localTest
Note:

Classes [hide private]
  BiskitTestError
  BiskitTest
Base class for Biskit test cases.
  FilteredTestSuite
Collection of BiskitTests filtered by category tags.
  Flushing_TextTestResult
Helper class for (Flushing)TextTestRunner.
  FlushingTextTestRunner
Convince TextTestRunner to use the flushing text output rather than the default one.
  BiskitTestLoader
A replacement for the unittest TestLoaders.
  Test
Mock test, test doesn't test itself

Functions [hide private]
dict getOuterNamespace()
Fetch the namespace of the module/script running as __main__.
[ class ] extractTestCases(namespace)
Returns all BisktTest child classes found in given namespace
unittest.TestResult localTest(testclass=None, verbosity=BiskitTest.VERBOSITY, debug=BiskitTest.DEBUG, log=BiskitTest.TESTLOG)
Perform the BiskitTest(s) found in the scope of the calling module.
  _use(defaults)
  _str2tags(s)
convert list of string options to list of valid TAGS
  _convertOptions(o)

Variables [hide private]
  NORMAL = 0
  LONG = 1
  PVM = 2
  EXE = 3
  OLD = 5
  defaults = {'i': '', 'e': 'old', 'p': ['Biskit', 'Biskit.Dock',...
  o = T.cmdDict(defaults)
  l = BiskitTestLoader(allowed= o ['i'], forbidden= o ['e'...

Function Details [hide private]

getOuterNamespace()

source code 

Fetch the namespace of the module/script running as __main__.
Returns: dict
the namespace of the outermost calling stack frame

extractTestCases(namespace)

source code 
Returns: [ class ]
all BisktTest child classes found in given namespace
Raises:

localTest(testclass=None, verbosity=BiskitTest.VERBOSITY, debug=BiskitTest.DEBUG, log=BiskitTest.TESTLOG)

source code 

Perform the BiskitTest(s) found in the scope of the calling module. After the test run, all fields of the BiskitTest instance are pushed into the global namespace so that they can be inspected in the interactive interpreter. The BiskitTest instance itself is also put into the calling namespace as variable 'self', so that test code fragments referring to it can be executed interactively.
Parameters:
  • testclass (class) - BiskitTest-derived class [default: first one found]
  • verbosity (int) - verbosity level of TextTestRunner
  • debug (int) - don't delete temporary files (skipp cleanUp) [0]
Returns: unittest.TestResult
the test result object
Raises:

_use(defaults)

source code 

_str2tags(s)

source code 

convert list of string options to list of valid TAGS

_convertOptions(o)

source code 

Variables Details [hide private]

NORMAL

Value:
0                                                                      
      

LONG

Value:
1                                                                      
      

PVM

Value:
2                                                                      
      

EXE

Value:
3                                                                      
      

OLD

Value:
5                                                                      
      

defaults

Value:
{'i': '', 'e': 'old', 'p': ['Biskit', 'Biskit.Dock', 'Biskit.Mod', 'Bi
skit.PVM', 'Biskit.Statistics'], 'v': '2', 'log': '',}                 
      

o

Value:
T.cmdDict(defaults)                                                    
      

l

Value:
BiskitTestLoader(allowed= o ['i'], forbidden= o ['e'], verbosity= o ['
v'], log= o ['log'], debug= o ['debug'])