安装ddt pip install ddt
直接上代码:
1 # coding:utf-8 2 import ddt 3 import time 4 import excelunit 5 import unittest 6 from selenium import webdriver 7 8 # 测试数据 9 data = excelunit.ExcelUtil("test.xlsx", "sheet1") 10 testData = data.dict_data() 11 print testData 12 13 14 @ddt.ddt 15 class Bolg(unittest.TestCase): 16 """登录博客""" 17 def setUp(self): 18 self.driver = webdriver.Firefox() 19 url = "https://passport.cnblogs.com/user/signin" 20 self.driver.get(url) 21 self.driver.implicitly_wait(10) 22 23 def login(self, username, psw): 24 """登录方法,账号和密码参数化""" 25 self.driver.find_element_by_id("input1").send_keys(username) 26 self.driver.find_element_by_id("input2").send_keys(psw) 27 self.driver.find_element_by_id("signin").click() 28 time.sleep(3) 29 30 def is_login_sucess(self): 31 """判断是否获取到登录账户名称""" 32 try: 33 text = self.driver.find_element_by_id("lnk_current_user").text 34 print text 35 return True 36 except: 37 return False 38 39 @ddt.data(*testData) 40 def test_login(self, data): 41 """登录案例""" 42 print ("当前测试数据%s" % data) 43 # 调用登录方法 44 self.login(data["username"], data["password"]) 45 # 判断结果 46 result = self.is_login_sucess() 47 self.assertTrue(result) 48 49 def tearDown(self): 50 self.driver.quit() 51 52 53 if __name__ == "__main__": 54 unittest.main()