Batch Execution Case of Unittest on Day 4 of python+unittest Framework

Posted by n000bie on Sun, 25 Aug 2019 16:46:36 +0200

Starting today with batch execution of use cases, the scenario is as follows:

We may have multiple module files (. py) in our work. These files are distributed under different module files according to different business types or functions. In the previous small examples, our test cases are all in one file, running directly in the test suite ~, and it's okay to do so during the development or debugging phase, but if it's the test run phase, it's impossible for us to switch the use cases in another module to continue after the use cases in this module file have been executed. Implementation. So we need to execute test cases of multiple module files in batches.

 

The first case of the first use case module is this~

 1 import unittest               #Import unittest library
 2 from selenium import webdriver    #Import Automated Testing selenium Medium webdriver
 3 
 4 class baidu_test_1(unittest.TestCase):   #Write one baidu_test_1 Class, inheritance unittest Medium TestCase class
 5 
 6     @classmethod        #Convenient for us to call directly, without the need to instantiate the class's object to call directly
 7     def setUp(cls):     #Before all cases are executed, the method is executed and initialized.
 8         cls.driver = webdriver.Chrome()    #item base webdriver object
 9         cls.driver.maximize_window()      #Maximizing Browser Windows
10         cls.driver.implicitly_wait(15)    #When the page loads slowly, we set the waiting time to 15 seconds.
11         cls.driver.get(r'http://www.baidu.com')     #Open Baidu's Home Page
12 
13     @classmethod
14     def tearDown(cls):     #After all cases are executed, the method is executed
15         cls.driver.quit()     #Close the browser
16 
17     '''test case'''
18     def test_baidu_lianjie(self):    #Use Case of Baidu Link Test
19         '''Baidu Home Page: Test the news link, the link address after the jump is correct'''
20         self.driver.find_element_by_link_text('Journalism').click()  #After clicking on the news link, get the news link: self.driver.current_url
21         self.assertEqual(self.driver.current_url,'http://news.baidu.com/')   #Compare the obtained links with the actual links to see if they are equal.
22 
23 '''if __name__ == '__main__'It means:.py The file is run directly. if __name__ == '__main__'The code block below will be run.
24 When.py When files are imported as modules, if __name__ == '__main__'The code block below is not run.'''
25 if __name__ == '__main__':
26     unittest.main(verbosity=2)

Next comes the code for the second use case module:

 

 1 import unittest
 2 from selenium import webdriver
 3 
 4 class baidu_test_2(unittest.TestCase):
 5     @classmethod
 6     def setUp(cls):
 7         cls.driver=webdriver.Chrome()
 8         cls.driver.maximize_window()
 9         cls.driver.implicitly_wait(15)
10         cls.driver.get(r'http://www.baidu.com')
11 
12     @classmethod
13     def tearDown(cls):
14         cls.driver.quit()
15 
16     def test_baidu_enabled(self):
17         so=self.driver.find_element_by_id('kw')    #Check whether elements are editable is_enabled(),What can be edited and returned is True
18         self.assertTrue(so.is_enabled())   #If the return is True,So it's true.~adopt
19 
20     def test_baidu_sousuo(self):
21         so = self.driver.find_element_by_id('kw')
22         so.send_keys('Hello China')  #The above code selects the input box, which is the input: send_keys()
23         self.driver.find_element_by_id('su').click()   #This method simulates click operation. click()
24         print(so.get_attribute('value'))     #Get the values in the form based on attributes get_attribute('value')
25         self.assertEqual(so.get_attribute('value'),'Hello China')       #We compare the values we get with the values we expect to see if they are equal.
26 
27 if __name__ == '__main__':
28     unittest.main(verbosity=2)

 

Note: The name of the test module needs to be unified. Look at the figure: The names of the two test case module files are preceded by the beginning of test_.

Next is the code of the last module file, which executes the code in the use case module in batches:

 1 import unittest    #Import unittest
 2 import os       #Introduce the previous learning os Library for easy access to file paths
 3 
 4 def allTests():
 5     suite=unittest.TestLoader().discover(            #Instance test suite
 6         start_dir=os.path.dirname(__file__),         #start_dir=This parameter is discover()In the method, the latter parameter is the use case module path that requires batch execution
 7         pattern='test_*.py',                         #pattern=This parameter is discover()In the method, the latter parameter is the preceding of all the use cases that need to be executed. test_,For the latter part*Number substituted.py file
 8         top_level_dir=None)                          #top_level_dir=This parameter is discover()In the method, fixed format: top_level_dir=None
 9     return suite                                     #Remember to return to the test suite
10 
11 def run():                                           #Function
12     unittest.TextTestRunner(verbosity=2).run(allTests())           #Test Report
13 
14 if __name__ == '__main__':
15     run()

This is how batch execution works.~~~~

Summary:

1. # Check whether the element can be edited with is_enabled(), which returns True or Flase.
2. Enter content in the edit box with: send_keys()
3. Click on the button can be used: click()
4. Get the value get_attribute('value') in the form based on the attribute, such as the content in the search
5. Get the URL link address of the current page: driver.current_url to determine if the page we jumped is correct.
6. The TestLoader() class in the unitest library used for batch execution use cases. The discover(start_dir, pattern='test*.py', top_level_dir=None) method in this class

There are three parameters in discover: the following is Baidu's wheel for everyone to learn by oneself

start_dir: The name of the module to be tested or the test case directory.
pattern='test*.py': Represents the matching principle for use case file names. The asterisk "*" denotes any number of characters. (test *. py begins with test)
top_level_dir=None: The top-level directory of the test module. If there is no top-level directory (that is, test cases are not placed in a multi-level directory), the default is None.
Links to the original text: https://blog.csdn.net/weixin_40569991/article/details/81155145
From the self-study summary, I hope it will be helpful to everyone. No friends can leave a message and make progress together. Autumn is coming. We should pay attention to the seasonal change. We are prone to fall ill and stick to autumn fat.

Topics: Python Selenium Windows Attribute Asterisk