Download A Python Book - Dave Kuhlman dot org

Transcript
A Python Book
●
methods, see the library documentation for the unittest module TestCase Objects ­­ http://docs.python.org/lib/testcase­objects.html.
If you want to change (1) the test method prefix or (2) the function used to sort (the order of) execution of tests within a test fixture, then you can create your own
instance of class unittest.TestLoader and customize it. For example:
def main():
my_test_loader = unittest.TestLoader()
my_test_loader.testMethodPrefix = 'check'
my_test_loader.sortTestMethodsUsing = my_cmp_func
unittest.main(testLoader=my_test_loader)
if __name__ == '__main__':
main()
But, see the notes in section Additional unittest features for instructions on a (possibly) better way to do this.
1.9.3.2 Unit test suites
Here is another, not quite so simple, example:
#!/usr/bin/env python
import sys, popen2
import getopt
import unittest
class GenTest(unittest.TestCase):
def test_1_generate(self):
cmd = 'python ../generateDS.py ­f ­o out2sup.py ­s out2sub.py
people.xsd'
outfile, infile = popen2.popen2(cmd)
result = outfile.read()
outfile.close()
infile.close()
self.failUnless(len(result) == 0)
def test_2_compare_superclasses(self):
cmd = 'diff out1sup.py out2sup.py'
outfile, infile = popen2.popen2(cmd)
outfile, infile = popen2.popen2(cmd)
result = outfile.read()
outfile.close()
infile.close()
#print 'len(result):', len(result)
# Ignore the differing lines containing the date/time.
#self.failUnless(len(result) < 130 and result.find('Generated') > ­1)
Page 81