Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Peter Otten <__peter__@web.de> Newsgroups: comp.lang.python Subject: Re: python unit test frame work Date: Thu, 10 Dec 2015 16:50:29 +0100 Organization: None Lines: 151 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Trace: news.uni-berlin.de jokEjdzzgYQ4piOCa1pliA/356Uo80/iprSlTC+/ICPw== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; '"""': 0.05; 'error:': 0.05; 'method.': 0.05; 'sufficient': 0.05; 'tries': 0.05; '__name__': 0.07; 'cleanup': 0.07; 'executed': 0.07; 'exit': 0.07; 'main()': 0.07; 'method,': 0.07; 'skip:/ 10': 0.07; 'subject:test': 0.07; 'unittest': 0.07; '-rf': 0.09; 'exception,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'example:': 0.10; 'skip:= 70': 0.10; 'python': 0.10; '2.7': 0.13; 'exception': 0.13; 'def': 0.13; 'subject:python': 0.14; 'acquired': 0.15; "'__main__':": 0.16; '--verbose': 0.16; 'failed")': 0.16; 'i.e': 0.16; 'main():': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'received:t-ipconnect.de': 0.16; 'ressources': 0.16; 'run.': 0.16; 'setup(self):': 0.16; 'skiptest': 0.16; 'sys.exit(1)': 0.16; 'unittests': 0.16; 'unnecessary.': 0.16; 'xxx,': 0.16; 'wrote:': 0.16; 'attribute': 0.18; "shouldn't": 0.18; 'skip:l 30': 0.18; 'test.': 0.18; 'try:': 0.18; 'team,': 0.18; 'tests': 0.18; 'fix': 0.21; 'keyerror:': 0.22; 'trying': 0.22; '(most': 0.24; 'written': 0.24; 'header:User-Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'skip:" 20': 0.26; 'error': 0.27; 'appreciated': 0.27; 'executing': 0.27; 'module.': 0.27; 'idea': 0.28; 'initial': 0.28; 'raise': 0.29; 'reporting': 0.29; 'starts': 0.29; 'print': 0.30; 'code': 0.30; 'error.': 0.31; 'skip:s 30': 0.31; 'run': 0.33; 'class': 0.33; 'flags': 0.33; 'point,': 0.33; 'traceback': 0.33; 'file': 0.34; 'except': 0.34; 'gets': 0.35; 'false': 0.35; 'comment': 0.35; 'unit': 0.35; 'should': 0.36; 'tool': 0.36; 'mode': 0.36; 'subject:work': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'skip:s 50': 0.37; 'release': 0.37; 'seem': 0.37; 'drop': 0.38; 'skip:o 20': 0.38; 'end': 0.39; 'test': 0.39; 'data': 0.39; 'application': 0.39; 'to:addr:python.org': 0.40; 'still': 0.40; 'received:de': 0.40; 'questions': 0.40; 'skip:u 10': 0.61; 'avoid': 0.61; 'sample': 0.63; 'here': 0.66; 'succeeds': 0.84; 'habit': 0.91 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: p57bd984c.dip0.t-ipconnect.de User-Agent: KNode/4.13.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:100239 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