python unit test framework sample code

Terry Reedy tjreedy at udel.edu
Wed Jan 6 16:49:58 EST 2016


On 1/6/2016 8:45 AM, Ganesh Pal wrote:
> Hello Team,
>
> I have written a small program using python unit test framework . I
> need your guidance to find out
>
> 1. If I have used the fixtures and classes properly ( first oop program) :) )
> 2. why does unittest2.SkipTest not print the message when the failures
> are encountered  ?
> 3. Also sys.stderr.write("Test run failed ({e})".format(e=e) ) does
> not display error on failure ?
> 4. Any other general comment's
>
> I am on Python 2.6 and using Linux

Unless you have a large program already in 2.6 and are just adding tests 
(perhaps so you can more easily upgrade someday), consider upgrading to 
a newer version.

> Sample code:
>
> class FileSystemTest(unittest2.TestCase):
>      block_address = {}
>      report = ""
>
>      @classmethod
>      def setUpClass(cls):
>          cls.FileSystemSetup()

This is senseless.  Put the body of FileSystemSetup here.

>      @classmethod
>      def FileSystemSetup(cls):
>          """
>          Initial setup before unittest is run
>          """
>          logging.info("SETUP.....Started !!!")
>          try:
>              inode_01 =
> corrupt.get_lin(os.path.join(CORRUPT_DIR,"inode_lin.txt"))
>              test_01_log = os.path.join(LOG_DIR, "test_01_corrupt.log")
>              cls.block_address['test_01'] =
> corrupt.inject_corruption(test_01_log,
>                      lin=inode_01, object="inode", offset="18",
> size="4", optype="set")
>              time.sleep(10)
>
>          except Exception, e:
>              logging.error(e)
>              raise unittest2.SkipTest("class setup failed")
>          if not corrupt.run_scanner():
>             raise unittest2.SkipTest("class setup failed")
>
>          else:
>               try:
>                   cls.report = corrupt.run_report()
>               except Exception, e:
>                  logging.error(e)
>                  raise unittest2.SkipTest("class setup failed")
>          logging.info("SETUP.....Done !!!")
>
>      def inode_corruption(self):
>          """ test_01: inode  corruption """
>          self.assertTrue(corrupt.run_query_tool(self.__class__.report,
 
self.block_address['test_01']))

Assuming that unittest2 is same as unittest, test methods must be called 
test_xyz.  So this is not run, hence no error.


>      @classmethod
>      def tearDownClass(cls):
>          cls.tearDown()

Ditto.  Put real body here.

>      @classmethod
>      def tearDown(cls):

This is worse than setup.  The name 'tearDown' is reserved for and 
called to teardown after each test_xyz method is called.  So once you 
change 'inode'corruption' to 'test_inode_corruption', this should fail.

>          print "Entered tearDown()"
>          try:
>              cls.cleanup = cleanup()
>          except Exception, e:
>                logging.error(e)
>
> def main():
>      """ ---MAIN--- """
>      global LOG_DIR
>      try:
>          if len(sys.argv) > 1:
>              LOG_DIR = str(sys.argv[1])
>      except Exception, e:
>          print(USAGE)
>          return errno.EINVAL
>      functions = [create_logdir,create_dataset,corrupt.prep_cluster]
>      for func in functions:
>          try:
>              func()
>          except Exception, e:
>                  logging.error(e)
>                  sys.stderr.write("Test run failed ({e})".format(e=e))

The above refers to functions you did not post.  For current unittest, 
it looks likes you should be using a setUpModule (possible 
tearDownModule) functions, but I don't know if those are available in 
the older unittest2.

>      unittest2.main()
>
> if __name__ == '__main__':
>      main()

I think the biggest mistake people make is to do too much new stuff at 
once.  Python, with IDLE (or equivalent), makes incremental development 
easy.  My first unittest program was about 10 lines.  I expanded from 
there.  I am still 'expanding' from my current knowledge.


-- 
Terry Jan Reedy




More information about the Python-list mailing list