python unit test frame work
Peter Otten
__peter__ at web.de
Thu Dec 10 10:50:29 EST 2015
Ganesh Pal wrote:
> Hello Team,
>
> Iam on python 2.7 and linux. Iam trying to understand the python unit
> test frame work and also trying to fix the below code , any help in
> this regard would be appreciated ?
I recommend that you reread the unittest documentation.
setUpClass() should be a class method, and if it succeeds you can release
the ressources it required in the corresponding tearDownClass() method. As
written the flags and the setUp()/tearDown() seem unnecessary.
If a necessary ressource cannot be acquired you can raise a SkipTest
exception and the test or tests will not run.
> logging.info("### Executing test01: inode corruption ###")
In --verbose mode unittest will print the first line of the test methods'
docstring. That should be sufficient most of the time.
Only loosely related:
Drop the habit to sprinkle sys.exit() all over the place. A well-behaved
application has one exit point, at the end of the main module.
> self.__class__.report
When reading the report attribute this is the same as just
self.report
(unless you shaded it in the instance).
> # Sample code starts here
> inject_failure = {}
> report = ""
> ClassIsSetup = False
> ClassCleanup = False
>
> class Test_filesystem(unittest.TestCase):
> """ """
> def TestSetup(self):
> """ Initial setup before unittests run """
> logging.info("SETUP.....Started !!!")
> if not os.path.ismount("/tmp"): # Comment 1.
> logging.error("Error: /tmp is not mounted")
> sys.exit("/tmp is not mounted ...Exiting !!!")
>
> if self.create_dataset() and capture_chksum():
> try:
> test01_log = os.path.join(LOG_DIR, "test01_corrupt.log")
> self.inject_failure['test01'] = tool.run_tool(test01_log)
> time.sleep(10)
> test02_log = os.path.join(LOG_DIR, "test01_corrupt.log")
> self.inject_failure['test01'] = tool.run_tool(test02_log)
> time.sleep(10)
> except Exception, e:
> logging.error(e)
> sys.exit(1)
> if not run_scanner():
> sys.exit(1)
> else:
> try:
> self.__class__.report = report_tool()
> except Exception, e:
> logging.error(e)
> sys.exit("Running Reporting tool failed")
> logging.info("SETUP.....Done !!!")
>
> def setUp(self):
> if not self.ClassIsSetup:
> self.__class__.ClassIsSetup = True
> self.setupClass()
>
> def setupClass(self):
> self.TestSetup()
>
> def test_01_inode_test(self):
> """ test01: """
> logging.info("### Executing test01: inode corruption ###")
> self.assertTrue(run_db_tool(self.__class__.report,
>
self.find_failure['test01']))
> def test_02_hardlink_test(self):
> """ test02: """
> logging.info("### Executing test01: inode corruption ###")
> self.assertTrue(run_db_tool(self.__class__.report,
>
> def tearDown(self):
> if self.ClassCleanup:
> self.tearDownClass()
>
> def tearDownClass(self):
> self.cleanup()
>
> # Comment 2
> def cleanup(self):
> """ Cleanup all the data & logs """
> logging.info("Cleaning all data")
> os.system("rm -rf /tmp/data_set")
>
> def main():
> unittest.main()
>
> if __name__ == '__main__':
> main()
>
> # Sample code ends here
>
> Questions :
>
> 1. If the setUp() fails the code still tries to run through the test
> and assert's with error. How do I avoid these Error on the console ,
> actually test01, test02, . etc , shouldn't run if the setup Failed ?
>
> Example: If the mount fails i.e if not os.path.ismount("/tmp"): in
> TestSetup(). we will get the below output:
>
> #c_t.py
> EEEE
> ======================================================================
> ERROR: test01: test_01_inode_test
> ----------------------------------------------------------------------
> Traceback (most recent call last):
> File "c_t.py", line xx, in setUp
> self.setupClass()
> File "c_t.py", line xxx, in TestSetup
> self.TestSetup()
> File "c_t.py", line xx, in corruptSetup
> sys.exit("/tmp is not mounted ...Exiting !!!")
> SystemExit: /tmp is not mounted ...Exiting !!!
> ======================================================================
> ERROR: test02
> ----------------------------------------------------------------------
> Traceback (most recent call last):
> File "c_t.py", line 162, in test_02_hardlink_test
> self.inject_failures['test02']))
> KeyError: 'test02'
>
> Ran 2 tests in 0.003s
> FAILED (errors=2)
>
> 2. The cleanup() never gets executed at the end of the test.
> 3. Any better idea or suggestions to improve the above code ?
>
> Regards,
> Ganesh
More information about the Python-list
mailing list