(1) Foreword
Through the use of data-driven testing, the input value and expected results can be parameterized. (for example: input data and expected results can directly read the data of Excel document)
(2) ddt
The ddt library can parameterize the variables in the test. When using ddt, use @ ddt decorator on the test class and @ data decorator on the test method. @The data decorator treats parameters as test data. Parameters can be single values, lists, tuples, and dictionaries. For lists and tuples, you need to use the @ unpack decorator to parse them into multiple parameters.
Use the following command to install the ddt Library
pip install ddt
(3) Get data through Excel
The xlrd library is needed to read Excel files.
Install libraries for xlrd
pip install xlrd
If you want to write data to an Excel table, you need to use the xlwt Library
pip install xlwt
(4) excel file used in the example
mailbox |
Mobile phone |
Landing name |
Nickname? |
Password |
Confirm password |
Expected results |
1 |
bky_110 |
Pangu |
test>100 |
test>100 |
Wrong mobile number |
|
18898989878 |
b |
Pangu |
test>100 |
test>100 |
Unqualified, at least 2 characters, at most 30 characters |
(5) Example
1 from selenium import webdriver 2 from ddt import ddt,data,unpack 3 import xlrd 4 import unittest 5 #read excel Functions of files 6 def get_data(file_name): 7 rows = [] 8 #read excel Data 9 book = xlrd.open_workbook(file_name) 10 #Access the first by index sheet page 11 sheet = book.sheet_by_index(0) 12 #Iterative read excel First sheet Page data, sheet.nrows finger excel Number of rows 13 for r_idx in range(1, sheet.nrows): 14 #row_values Read the first r_idx Data for row (0 for reading data for column 1 and all subsequent columns) 15 #When reading data, we generally say that the first row, the first column, and the index are all 0 16 #therefore r_idx=1 When it comes to reading excel Data in the second row 17 rows.append(list(sheet.row_values(r_idx,0))) 18 #First delete the mobile number and assign it to pthone,It is then converted to a string and added back to its original location 19 pthone = rows[r_idx - 1].pop(1) 20 rows[r_idx - 1].insert(1, str(int(pthone))) 21 return rows 22 @ddt 23 class RegisterNewUserDDT(unittest.TestCase): 24 @classmethod 25 def setUpClass(cls): 26 cls.driver = webdriver.Chrome() 27 cls.driver.implicitly_wait(20) 28 cls.driver.maximize_window() 29 cls.driver.get('https://www.cnblogs.com/') 30 login_area = cls.driver.find_element_by_css_selector('#login_area') 31 register = login_area.find_element_by_link_text('register') 32 register.click() 33 #read excel File data as parameters 34 @data(*get_data('data/reTest.xlsx')) 35 @unpack 36 def test_register_new_user(self,email,phone,login_name,nickname,password,confirm_password,expected_result): 37 driver = self.driver 38 self.assertTrue('User registration - Blog Garden' == driver.title) 39 # Locate the fields on the registration page 40 user_email = driver.find_element_by_id('Email') 41 user_phone_country = driver.find_element_by_id('CountryCode') 42 user_phone = driver.find_element_by_id('PhoneNum') 43 user_login_name = driver.find_element_by_id('LoginName') 44 user_nickname = driver.find_element_by_id('DisplayName') 45 user_password = driver.find_element_by_id('Password') 46 user_confirm_password = driver.find_element_by_id('ConfirmPassword') 47 #Clear the value of each field (if any) 48 user_email.clear() 49 user_phone.clear() 50 user_login_name.clear() 51 user_nickname.clear() 52 user_password.clear() 53 user_confirm_password.clear() 54 #Input email, mobile number and other information 55 user_email.send_keys(email) 56 user_phone.send_keys(phone) 57 user_login_name.send_keys(login_name) 58 user_nickname.send_keys(nickname) 59 user_password.send_keys(password) 60 user_confirm_password.send_keys(confirm_password) 61 #Judge whether the prompt is correct (there should be a way to get the current execution time. The following writing method is too rigid) 62 if phone == '1': 63 phone_error = driver.find_element_by_id('PhoneNum-error') 64 self.assertTrue(phone_error.text == expected_result) 65 elif login_name == 'b': 66 loginName_error = driver.find_element_by_id('LoginName-error') 67 self.assertTrue(loginName_error.text == expected_result) 68 69 @classmethod 70 def tearDownClass(cls): 71 cls.driver.quit()