Hi Dear Python Friends,
The unittest’s TestCase <https://docs.python.org/3/library/unittest.html#unittest.TestCase> class provides several assert methods to check for and report failures . I need suggestion what would the best way to assert test cases in the below piece of code. (1) should I add several asserts per test case, or just warn with the error and fail at the end . In the line 33 – 35 / 37-38 ( sorry this is a dirty pusedo-code) . (2) Is there a way we can warn the test using assert method and not fail? I was trying to see if I could use assertWarns but the help says that “The test passes if warning is triggered and fails if it isn’t “. I don’t want to fail on warning but just continue which next checks (3) All more ways to optimize the sample code. 1 import unittest 2 import library 3 4 5 class AutoRepairFilesystem(unittest.TestCase): 6 7 blocks = {} 8 report = "" 9 10 @classmethod 11 def setUpClass(self): 12 """ 13 Set UP 14 """ 15 logging.info("SETUP.....Started") 16 try: 17 self.blocks['test01'] = library.inject_corruption1(file1) 18 self.blocks['test100'] = library.inject_corruption100(file100) 19 20 except Exception as e: 21 logging.error("Failure injection failed \n") 22 raise 23 24 if not library.check_Repair(): 25 logging.error("Failed running FSCK Tool ") 26 assert False, "Pre-test checks in setUpClass failed skipping test" 27 logging.info("SETUP.....Done") 28 29 def test_corruption1(self): 30 """Run test no 1 """ 31 # This was the only earlier condition then! 32 #self.assertTrue(library.log_message_is_reported(self.report,self.blocks['test01']):''' 33 if not library.log_message_is_reported(self.report, 34 self.blocks['test01']): 35 print "Warning: Reporting Failed.... \n" 36 37 if not library.is_corruption_fixed(): 38 print "Warning: Corruption is not fixed .... \n" 39 40 if not library.is_corruption_reparied(): 41 assert False, "Corruption not reported,fixed and auto repaired.\n" 42 43 def test_corruption100(self): 44 """ Run test no 100 """ 45 if not library.log_message_is_reported(self.report, 46 self.blocks['test100']): 47 print "Warning: Reporting Failed.... \n" 48 49 if not library.is_corruption_fixed(): 50 print "Warning: Corruption is not fixed .... \n" 51 52 if not library.is_corruption_reparied(): 53 assert False, "Corruption not reported,fixed and auto repaired.\n" 54 55 @classmethod 56 def tearDownClass(self): 57 """ Delete all files """ 58 os.system("rm -rf /tmp/files/") 59 60 if __name__ == '__main__': 61 unittest.main() I am a Linux user with Python 2.7. Regards, Ganesh -- https://mail.python.org/mailman/listinfo/python-list