unittest中的testCase執行順序


1.方法順序

def setUp(self): 在測試方法前執行 
def tearDown(self): 在測試方法后執行

 1 class TestMethod(unittest.TestCase):  2 
 3     #每次方法之前執行
 4     def setUp(self):  5         print('每次方法之前執行')  6 
 7     #每次方法之后執行
 8     def tearDown(self):  9         print('每次方法之后執行') 10 
11     def test_01(self): 12         print('測試1') 13 
14     def test_02(self): 15         print('測試2') 16 
17 if __name__ == '__main__': 18     unittest.main()

執行結果: 

2.類順序

@classmethod 
def setUpClass(cls): 
在類之前執行

@classmethod 
def tearDownClass(cls): 
在類之后執行

 1 class TestMethod(unittest.TestCase):  2 
 3  @classmethod  4     def setUpClass(cls):  5         print('類執行之前的方法')  6 
 7  @classmethod  8     def tearDownClass(cls):  9         print('類執行之后的方法') 10 
11     #每次方法之前執行
12     def setUp(self): 13         print('每次方法之前執行') 14 
15     #每次方法之后執行
16     def tearDown(self): 17         print('每次方法之后執行') 18 
19     def test_01(self): 20         print('測試1') 21 
22     def test_02(self): 23         print('測試2') 24 
25 if __name__ == '__main__': 26     unittest.main()

執行結果: 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM