Unnitest測試框架總結


Unnitest總結

第一點,setUptearDown方法

l  每次執行test開頭的用例都會執行setUptearDown方法

l  :

 1import unittest
 2 
 3 class Mydemo(unittest.TestCase):
 4     def setUp(self):
 5         print('調用setUp方法')
 6         self.a = 1
 7     def test1(self):
 8         print("i am test1 the value of a is {}".format(self.a))
 9     def test2(self):
10         print("i am test2 the value of a is {}".format(self.a))
11     def test3(self):
12         print("i am test3 the value of a is {}".format(self.a))
13     def tearDown(self):
14         print("調用tearDown方法")
15 if __name__ == '__main__':
16     unittest.main()

n  結果:

 

第二點: setUpClasstearDownClass方法

 

l  :

 1import unittest
 2 class Mydemo(unittest.TestCase):
 3     @classmethod
 4     def setUpClass(cls):
 5         print("調用setUpClass\n")
 6     def test1(self):
 7         print("i am test1")
 8     def test2(self):
 9         print("i am test2")
10     @classmethod
11     def tearDownClass(cls):
12         print("調用tearDownClass")
13 if __name__ == '__main__':
14     unittest.main()

l  結果:

第三點:定義全局的屬性,可以放到setUpClasstestCase中均可去調用

 

l  :

 1class NavigationTest(unittest.TestCase):
 2     @classmethod
 3     def setUpClass(cls):
 4         cls.driver = webdriver.Chrome()
 5         cls.driver.implicitly_wait(30)
 6         cls.driver.maximize_window()
 7         cls.driver.get('https://www.baidu.com/')
 8         driver = cls.driver #定義在全局中
 9         cls.search_field = driver.find_element_by_name('wd') #定義在全局中
10 
11     def testBrowserNavigation(self):
12         self.search_field.clear()
13         print('執行test1')
14 
15         self.search_field.send_keys('聖女果')
16         self.search_field.submit()
17         time.sleep(1)
18 
19         self.assertEqual('聖女果_百度搜索',self.driver.title)
20 
21         self.driver.back()
22         self.assertTrue(WebDriverWait(self.driver,30).until(expected_conditions.title_contains('百度一下')))
23     def testBrowserNavigation2(self):
24         driver = self.driver
25         search_field = driver.find_element_by_name('wd')
26         search_field.clear()
27         print('執行test2')
28 
29         search_field.send_keys('瓔珞')
30         search_field.submit()
31         time.sleep(1)
32     @classmethod
33     def tearDownClass(cls):
34         driver = cls.driver
35         time.sleep(10)
36         driver.quit()
37 
38 if __name__ == "__main__":
39     unittest.main()
40

第四點:testCase之間的執行順序按照默認是以首字母排序的

 

l  執行順序test1,test10,....test18,test2,..........test9

 

 

 

 

Unnitest總結

第一點,setUptearDown方法

l  每次執行test開頭的用例都會執行setUptearDown方法

l  :

l   import unittest

class Mydemo(unittest.TestCase):
   
def setUp(self):
       
print('調用setUp方法')
       
self.a = 1
   
def test1(self):
       
print("i am test1 the value of a is {}".format(self.a))
   
def test2(self):
       
print("i am test2 the value of a is {}".format(self.a))
   
def test3(self):
       
print("i am test3 the value of a is {}".format(self.a))
   
def tearDown(self):
       
print("調用tearDown方法")
if __name__ == '__main__':
    unittest.main()

n  結果:

 

第二點: setUpClasstearDownClass方法

l  :

 
 
 
         l   
 
 
 
         import unittest
class Mydemo(unittest.TestCase):
   
@classmethod
   
def setUpClass(cls):
       
print("調用setUpClass\n")
   
def test1(self):
       
print("i am test1")
   
def test2(self):
       
print("i am test2")
   
@classmethod
   
def tearDownClass(cls):
       
print("調用tearDownClass")
if __name__ == '__main__':
    unittest.main()

l  結果:

n 

第三點:定義全局的屬性,可以放到setUpClasstestCase中均可去調用

l  :

 
 
 
         l   
 
 
 
         class NavigationTest(unittest.TestCase):
   
@classmethod
   
def setUpClass(cls):
       
cls.driver = webdriver.Chrome()
       
cls.driver.implicitly_wait(30)
       
cls.driver.maximize_window()
       
cls.driver.get('https://www.baidu.com/')
        driver =
cls.driver #定義在全局中
       
cls.search_field = driver.find_element_by_name('wd') #定義在全局中

   
def testBrowserNavigation(self):
       
self.search_field.clear()
       
print('執行test1')

       
self.search_field.send_keys('聖女果')
       
self.search_field.submit()
        time.sleep(
1)

       
self.assertEqual('聖女果_百度搜索',self.driver.title)

       
self.driver.back()
       
self.assertTrue(WebDriverWait(self.driver,30).until(expected_conditions.title_contains('百度一下')))
   
def testBrowserNavigation2(self):
        driver =
self.driver
        search_field = driver.find_element_by_name(
'wd')
        search_field.clear()
       
print('執行test2')

        search_field.send_keys(
'瓔珞')
        search_field.submit()
        time.sleep(
1)
   
@classmethod
   
def tearDownClass(cls):
        driver =
cls.driver
        time.sleep(
10)
        driver.quit()

if __name__ == "__main__":
    unittest.main()

n   

第四點:testCase之間的執行順序按照默認是以首字母排序的

l  執行順序test1,test10,....test18,test2,..........test9

 

 


免責聲明!

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



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