Unittest fourth day of python + unittest framework batch execution case

  Today in volume execution example to the scene like this:

We work may have multiple module files (.py) These files are distributed in different modules, depending on the file type of business or functional test cases. Previous small example, we are in a test case file, run the test suite - direct, stage of development or commissioning phase This is not wrong, but if the test run stage, we can not file this module after finished the use case, the switching module with another embodiment continues. So test batch files to perform multiple modules we need to use.

 

First, a case with the first embodiment is such a module -

1  Import unittest                # import unittest library 
2  from selenium Import webdriver     # Import automated testing selenium in webdriver 
3  
4  class baidu_test_1 (unittest.TestCase):    # write a baidu_test_1 classes, inheritance TestCase class in the unittest 
5  
6      @classmethod         # facilitate our direct object calls need not directly call the class is instantiated 
. 7      DEF the setUp (CLS):      # before performing all cases, to perform the method are initialized 
. 8          cls.driver webdriver.Chrome = ()     # instantiate a webdriver objects 
. 9          CLS. driver.maximize_window ()       #Maximize the browser window 
10          cls.driver.implicitly_wait (15)     # page loading slow, we set a long time to wait for 15 seconds 
11          cls.driver.get (r ' http://www.baidu.com ' )      # open Baidu home page 
12 is  
13 is      @classmethod
 14      DEF the tearDown (CLS):      # after performing all cases, execution of the method 
15          cls.driver.quit ()      # close the browser 
16  
. 17      '' ' test ' '' 
18 is      DEF test_baidu_lianjie (Self) :     # Baidu link test use cases 
19          '' ' Baidu Home: test news links, link after the jump address is correct ' '' 
20         self.driver.find_element_by_link_text ( ' News ' ) .click ()   # click on news links, get news links: self.driver.current_url 
21          self.assertEqual (self.driver.current_url, ' http://news.baidu.com / ' )    # with the acquired links, and links to do the actual comparison is not equal see 
22 is  
23 is  '' ' iF the __name__ ==' __main__ 'means: when the file is run directly .py time, if __name__ ==' __main__ 'block of code to be run under;
 24  when .py files when introduced in modular form, if __name__ ==' block __main__ 'is not under operation. '' ' 
25  IF  the __name__ == ' __main__ ' :
 26 is      unittest.main (the verbosity = 2)

Next is a second embodiment of the code 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 if an element can be edited by is_enabled (), can be edited returns True 
18 is          self.assertTrue ( so.is_enabled ())    # If it returns true, then that by Barbara true ~ 
. 19  
20 is      DEF test_baidu_sousuo (Self):
 21 is          sO = self.driver.find_element_by_id ( ' kW ' )
 22 is          so.send_keys ( ' Hello China ' )   # above code input box is selected, which is input: send_keys () 
23 is         self.driver.find_element_by_id ( ' SU ' ) .click ()    # which simulate a click operation is the Click () 
24          Print (so.get_attribute ( ' value ' ))      # get the value get_attribute form according to the attribute ( 'value') 
25          self.assertEqual (so.get_attribute ( ' value ' ), ' hello China ' )        # we get to use the value, and the value we expect to do comparison, to see whether the same 
26  
27  iF  __name__ == ' __main__ ' :
 28      unittest.main (verbosity = 2)

 

Note: The name of the test module, requires a unified front, Figure: module file name in front of two test cases are beginning test_

The next module is the last file of a code, the code module with the batch execution Example:

. 1  Import the unittest     # introducing the unittest 
2  Import os        # import library os learned previously, to facilitate access to the file path 
. 3  
. 4  DEF alltests ():
 . 5      . Unittest.TestLoader Suite = () Discover (             # instantiated test suite 
. 6          start_dir = the os.path .dirname ( __FILE__ ),          # start_dir = this parameter is Discover () method, the latter parameter is required to batch with Example module path 
. 7          pattern = ' Test _ *. Py ' ,                          # pattern = this parameter is Discover () method is followed with all the foregoing parameters are to be performed is with test_ embodiment, instead of the second half of a number .py files * 
8         = None top_level_dir)                           # top_level_dir = This parameter is Discover () method, the fixed format: top_level_dir = None 
. 9      return Suite                                      # remember back test suite 
10  
. 11  DEF RUN ():                                            # Run 
12 is      unittest.TextTestRunner (the verbosity = 2) .run (alltests ())            # test report 
13 is  
14  IF  the __name__ == ' __main__ ' :
 15      RUN ()

Batch execution is such a ~ ~ ~ ~

to sum up:

1, # check whether the element can be edited with is_enabled (), you can edit returns True, otherwise it is Flase 
2, in typing in the box with: send_keys ()
3, click the button operation can be used: the Click ()
4, get_attribute property to get the value in a form ( 'value') such as content search in accordance with
5, url links to obtain the address of the current page: driver.current_url to determine whether we jump right page
6, batch execution cases used unitest library the TestLoader () class that the discover (start_dir, pattern = 'test * .py', top_level_dir = None) method

discover can be seen that there are three parameters: the following is for everyone Baidu wheel self learning ~~~

start_dir: To test name or test modules directory.
pattern = 'test * .py': indicates the file name to match with the principles of embodiments. An asterisk "*" means any number of characters. (Test * .py is the beginning of the test)
top_level_dir = None: the top-level directory of the test module. If there is no top-level directory (that is not a test case on the multi-level directory), the default is None.
Original link: https: //blog.csdn.net/weixin_40569991/article/details/81155145
------------- summary from self-study, we want to help, friends can not leave a message, along with progress ~~~~~ Autumn has come, pay attention to the seasonal transition, easy to get sick ~ ~ ~ ~ ~ ~ stick autumn fat

 

Guess you like

Origin www.cnblogs.com/woshidaliua/p/11397346.html