1. 程式人生 > >Unittest測試用例加入到測試集中的操作例項

Unittest測試用例加入到測試集中的操作例項

#coding=utf8 
'''
測試sub
'''
import unittest
from source.calcutor import calculatorClass
class UpAndDown(unittest.TestCase):
    def setUp(self):
        '''建立物件例項'''
        self.cal1=calculatorClass()
        self.cal2=calculatorClass(15,2)
    
    def test_sub_1(self):
        '''first is litter than second'''
        result=self.cal1.sub()
        self.assertTrue(result >0,"The result should be greater than 0")
        self.assertGreater(result, 0, "The result should be greater than 0")
        self.assertEqual(result,10,"The result should be equal 0")
        
    def test_sub_2(self):
        '''first is greater than second'''
        result=self.cal2.sub()
        self.assertTrue(result >0,"The result should be greater than 0")
        self.assertGreater(result, 0, "The result should be greater than 0")
        self.assertEqual(result,13,"The result should be equal 0")
        
    def tearDown(self):
        ''''刪除物件例項引用'''
        del self.cal1,self.cal2
        
def suite_1():
    suite=unittest.TestSuite()
    '''方法1:把測試用例中的方法一個個新增到測試集中'''
    suite.addTest(UpAndDown('test_sub_1'))
    suite.addTest(UpAndDown('test_sub_2'))
    
    
def suite_2():
    suite=unittest.TestSuite()
    '''方法2:把測試用例中的方法一次新增到測試集中'''
    tests=["test_sub_1","test_sub_2"]
    suite.addTests(tests)
    return suite

def suite_3():
    tests=["test_sub_1","test_sub_2"]
    '''方法3:把測試用例中的方法一次新增到測試集中'''
    suite=unittest.TestSuite(map(UpAndDown,tests))    
    return suite
            
if __name__=="__main__":
    unittest.TextTestRunner().run(suite_1())