Unit test framework unitest in python

1. Overview of unit test:

From the perspective of software architecture, the most important step of testing is to enter the boundary during software development. Therefore, in the boundary of early testing, from the perspective of software economics, the cost of solving the problems found is low and the resources invested are relatively small. Therefore, for a tested system, the best test to start is the source code level test, that is, the unit test stage. This process is also called white box test. Unit test is the most basic and low-level test type. Unit test is applied to the most basic software code, such as classes and functions. Methods, etc. The unit test checks whether the output of the tested unit meets the expected results through executable assertions. In the theory of the test pyramid, the lower the test investment resources, the greater the rate of return

 

 

Test case: write each specific test scenario

Test firmware (hook method): initialization and cleaning

Test execution testRunner: specifically execute the written test suite or test case

Test report: the test report feeds back the actual test results


Automated test case elements include:

1. Prerequisites (initialization) setUp()

2. Test steps

3. Result verification

4. Clean tearDown ()

Specification for writing test cases:

1. Each test case must start with test. Generally, test is recommended_

2. Each test case should have a title

3. It is recommended to write only one test scenario for each test case

4. Each test case must have result information, that is, assertions

5. Each test case is independent, and the relationship between test cases will not be changed due to business dependencies


 

 

2. Use class method to test

 1 from selenium import webdriver
 2 import unittest
 3 
 4 class BaiduTest(unittest.TestCase):
 5     @classmethod
 6     def setUpClass(cls) -> None:
 7         cls.driver=webdriver.Chrome()
 8         cls.driver.get('http://www.baidu.com/')
 9         cls.driver.maximize_window()
10         cls.driver.implicitly_wait(30)
11     @classmethod
12     def tearDownClass(cls) -> None:
13         cls.driver.quit()
14 
15     def test_baidu_title(self):
16         '''Baidu test: verify the of Baidu home page title'''
17         # assert self.driver.title=='Baidu once, you know'
18         self.assertEqual(self.driver.title,'Baidu once, you know')
19 
20     def test_baidu_url(self):
21         '''Baidu test: verify the of Baidu home page URL'''
22         assert self.driver.current_url=='https://www.baidu.com/'
23 
24     def test_baidu_video(self):
25         '''Baidu test: verify the jump to the video page after clicking the video'''
26         nowwindow=self.driver.current_window_handle   #Get current window
27         self.driver.find_element_by_link_text('video').click()
28         allwindows=self.driver.window_handles
29         for handler in allwindows:   #stay handle Inside loop all windows
30             if handler!=nowwindow:   #If handle Not equal to the current window (Baidu website)
31                 self.driver.switch_to.window(handler)  #Enter the video window
32                 self.assertEqual(self.driver.current_url,'https://haokan.baidu.com/?sfrom=baidu-top')   #Asserts the URL of the current window
33                 self.driver.close()
34         self.driver.switch_to.window(nowwindow)
35     def test_baidu_map(self):
36         '''Baidu test: verify the jump to the map page after clicking the video'''
37         nowwindow=self.driver.current_window_handle
38         self.driver.find_element_by_link_text('Map').click()
39         allwindows=self.driver.window_handles
40         for handler in allwindows:
41             if handler!=nowwindow:
42                 self.driver.switch_to.window(handler)
43                 self.assertTrue(self.driver.current_url.startswith('https://map.baidu'))
44                 self.driver.close()
45         self.driver.switch_to.window(nowwindow)
46 
47 if __name__ == '__main__':
48     unittest.main(verbosity=2)

3. Use the module to test

 1 from selenium import webdriver
 2 import unittest
 3 class BaiduTest(unittest.TestCase):
 4     @classmethod
 5     def setUpClass(cls) -> None:
 6         cls.driver=webdriver.Chrome()
 7         cls.driver.get('http://www.baidu.com/')
 8         cls.driver.maximize_window()
 9         cls.driver.implicitly_wait(30)
10     @classmethod
11     def tearDownClass(cls) -> None:
12         cls.driver.quit()
13 
14     def test_baidu_title(self):
15         '''Baidu test: verify the of Baidu home page title'''
16         # assert self.driver.title=='Baidu once, you know'
17         self.assertEqual(self.driver.title,'Baidu once, you know')
18 
19     def test_baidu_url(self):
20         '''Baidu test: verify the of Baidu home page URL'''
21         assert self.driver.current_url=='https://www.baidu.com/'
22 
23     def test_baidu_video(self):
24         '''Baidu test: verify the jump to the video page after clicking the video'''
25         nowwindow=self.driver.current_window_handle   #Get current window
26         self.driver.find_element_by_link_text('video').click()
27         allwindows=self.driver.window_handles
28         for handler in allwindows:   #stay handle Inside loop all windows
29             if handler!=nowwindow:   #If handle Not equal to the current window (Baidu website)
30                 self.driver.switch_to.window(handler)  #Enter the video window
31                 self.assertEqual(self.driver.current_url,'https://haokan.baidu.com/?sfrom=baidu-top')   #Asserts the URL of the current window
32                 self.driver.close()     #close
33         self.driver.switch_to.window(nowwindow)    #Switch to current window
34     def test_baidu_map(self):
35         '''Baidu test: verify the jump to the map page after clicking the video'''
36         nowwindow=self.driver.current_window_handle
37         self.driver.find_element_by_link_text('Map').click()
38         allwindows=self.driver.window_handles
39         for handler in allwindows:
40             if handler!=nowwindow:
41                 self.driver.switch_to.window(handler)
42                 self.assertTrue(self.driver.current_url.startswith('https://map.baidu'))
43                 self.driver.close()
44         self.driver.switch_to.window(nowwindow)
45 
46 if __name__ == '__main__':
47     # suite=unittest.TestLoader().loadTestsFromTestCase(BaiduTest)   #Instantiate and use the method loadTestsFromTestCase in the class
48     # unittest.TextTestRunner().run(suite)                      #Call the run method in loadTestsFromTestCase
49 
50 #Execute according to the module
51     suite = unittest.TestLoader().loadTestsFromModule('Execute according to the test class')
52     unittest.TextTestRunner().run(suite)

 

Posted by vahidf on Mon, 06 Dec 2021 14:45:40 -0800