Back to index

python-biopython  1.60
Public Member Functions | Public Attributes
run_tests.ComparisonTestCase Class Reference

List of all members.

Public Member Functions

def __init__
def shortDescription
def runTest
def generate_output

Public Attributes

 name
 output

Detailed Description

Run a print-and-compare test and compare its output against expected output.

Definition at line 203 of file run_tests.py.


Constructor & Destructor Documentation

def run_tests.ComparisonTestCase.__init__ (   self,
  name,
  output = None 
)
Initialize with the test to run.

Arguments:
o name - The name of the test. The expected output should be
  stored in the file output/name.
o output - The output that was generated when this test was run.

Definition at line 207 of file run_tests.py.

00207 
00208     def __init__(self, name, output=None):
00209         """Initialize with the test to run.
00210 
00211         Arguments:
00212         o name - The name of the test. The expected output should be
00213           stored in the file output/name.
00214         o output - The output that was generated when this test was run.
00215         """
00216         unittest.TestCase.__init__(self)
00217         self.name = name
00218         self.output = output


Member Function Documentation

Generate the golden output for the specified test.

Definition at line 275 of file run_tests.py.

00275 
00276     def generate_output(self):
00277         """Generate the golden output for the specified test.
00278         """
00279         outputdir = os.path.join(TestRunner.testdir, "output")
00280         outputfile = os.path.join(outputdir, self.name)
00281 
00282         output_handle = open(outputfile, 'w')
00283 
00284         # write the test name as the first line of the output
00285         output_handle.write(self.name + "\n")
00286 
00287         # remember standard out so we can reset it after we are done
00288         save_stdout = sys.stdout
00289         try:
00290             # write the output from the test into a string
00291             sys.stdout = output_handle
00292             __import__(self.name)
00293         finally:
00294             output_handle.close()
00295             # return standard out to its normal setting
00296             sys.stdout = save_stdout
00297 

Here is the call graph for this function:

Definition at line 222 of file run_tests.py.

00222 
00223     def runTest(self):
00224         # check the expected output to be consistent with what
00225         # we generated
00226         outputdir = os.path.join(TestRunner.testdir, "output")
00227         outputfile = os.path.join(outputdir, self.name)
00228         try:
00229             if sys.version_info[0] >= 3:
00230                 #Python 3 problem: Can't use utf8 on output/test_geo
00231                 #due to micro (\xb5) and degrees (\xb0) symbols
00232                 expected = open(outputfile, encoding="latin")
00233             else:
00234                 expected = open(outputfile, 'rU')
00235         except IOError:
00236             self.fail("Warning: Can't open %s for test %s" % (outputfile, self.name))
00237 
00238         self.output.seek(0)
00239         # first check that we are dealing with the right output
00240         # the first line of the output file is the test name
00241         expected_test = expected.readline().strip()
00242 
00243         if expected_test != self.name:
00244             expected.close()
00245             raise ValueError("\nOutput:   %s\nExpected: %s" \
00246                   % (self.name, expected_test))
00247 
00248         # now loop through the output and compare it to the expected file
00249         while True:
00250             expected_line = expected.readline()
00251             output_line = self.output.readline()
00252 
00253             # stop looping if either of the info handles reach the end
00254             if not(expected_line) or not(output_line):
00255                 # make sure both have no information left
00256                 assert expected_line == '', "Unread: %s" % expected_line
00257                 assert output_line == '', "Extra output: %s" % output_line
00258                 break
00259 
00260             # normalize the newlines in the two lines
00261             expected_line = expected_line.strip("\r\n")
00262             output_line = output_line.strip("\r\n")
00263 
00264             # if the line is a doctest or PyUnit time output like:
00265             # Ran 2 tests in 0.285s
00266             # ignore it, so we don't have problems with different running times
00267             if re.compile("^Ran [0-9]+ tests? in ").match(expected_line):
00268                 pass
00269             # otherwise make sure the two lines are the same
00270             elif expected_line != output_line:
00271                 expected.close()
00272                 raise ValueError("\nOutput  : %s\nExpected: %s" \
00273                       % (repr(output_line), repr(expected_line)))
00274         expected.close()

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 219 of file run_tests.py.

00219 
00220     def shortDescription(self):
00221         return self.name


Member Data Documentation

Definition at line 216 of file run_tests.py.

Definition at line 217 of file run_tests.py.


The documentation for this class was generated from the following file: