python unit test framework sample code

Ganesh Pal ganesh1pal at gmail.com
Wed Jan 6 08:45:22 EST 2016


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

Iam on Python 2.6 and using Linux

Sample code:

class FileSystemTest(unittest2.TestCase):
    block_address = {}
    report = ""

    @classmethod
    def setUpClass(cls):
        cls.FileSystemSetup()

    @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']))
    @classmethod
    def tearDownClass(cls):
        cls.tearDown()

    @classmethod
    def tearDown(cls):
        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))
    unittest2.main()

if __name__ == '__main__':
    main()


PS : Happy  New year Wishes to all the form members !!

Regards,
Ganesh



More information about the Python-list mailing list