Package schrodinger :: Package application :: Package desmond :: Module util :: Class TestSuite
[hide private]
[frames] | no frames]

Class TestSuite

object --+
         |
        TestSuite

A small and tight class designed for unit tests.

With this utility, unit test code can be written much faster than with the 'unittest' module.

Instance Methods [hide private]
 
__init__(self, title)
Sets the title of the suite to 'title' and all internal counters to 0.
 
__del__(self)
Finishes testing untested cases and prints a simple summary.
 
__lshift__(self, case)
Adds a case into the suite.
 
run(self)
Performs testing of all test cases accumulated so far and print information indicating a particular case is passed or failed.

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __sizeof__, __str__, __subclasshook__

Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__init__(self, title)
(Constructor)

 

Sets the title of the suite to 'title' and all internal counters to 0.

Overrides: object.__init__

__lshift__(self, case)

 

Adds a case into the suite. Each case must be a tuple of at least two elements:

  • 'case[0]' must be one of the following:
    • a boolean value:
      • False - means the test is failed.
      • True - means the test is passed.
    • or a callable object that can return a boolean value. The object will be called later on by this 'TestSuite' object (via calling to the 'run' method), and the returned value will be used to judge whether the test is passed on the same rule as above. If an exception is thrown out of the call to the object, the test will be considered failed.
  • 'case[1]' must be a string, representing the name of the case.

If there are more than two elements, the remaining elements will be printed or called (if callable) if the test is failed. This helps diagnose the problem.

run(self)

 

Performs testing of all test cases accumulated so far and print information indicating a particular case is passed or failed. The user can explicitly call this function as many times as like. Already tested cases will not be tested again. This means that the user can do something like this:

  tsuite = TestSuite( 'util' )
  ...
  tsuite << case1
  tsuite.run()    # Testing of 'case1' will be performed
  tsuite.run()    # Allowed, but meaningless since 'case1' will be tested again.

and so this:

  tsuite << case1
  tsuite.run()   
  tsuite << case1 
  tsuite.run()    # Allowed. The 'tsuite' takes the 2nd 'case1' as a new case and performs the testing.

The user usually does NOT need to call this function explicitly because when the 'TestSuite' object is about to be destructed, it will automatically check and test any untested cases.