Python ddt读取Excel文件数据驱动
引入xlrd模块:
ExcelDDT.py:
import unittest<br />from time import sleep<br /><br />from ddt import ddt, data<br />from selenium import webdriver<br />from xlrd import open_workbook<br /><br /><br />def getExcelTestData():<br /> openExcelFile = open_workbook("./ExcelTestData.xlsx")<br /> # 打开Excel文件<br /> getSheet = openExcelFile.sheet_by_name("Sheet1")<br /> # 获取工作表<br /> rowNumber = getSheet.row<br /> # 获取行数<br /> dataList = []<br /> # 数据List<br /> for i in range(1, rowNumber):<br /> # 从第二行开始遍历每一行<br /> dataList.append(getSheet.row_values(i))<br /> # 把每个单元格的数值存放到dataList中<br /> return dataList<br /><br /><br />@ddt<br /># ddt驱动<br />class MyTestCase(unittest.TestCase):<br /><br /> def setUp(self):<br /> self.driver = webdriver.Firefox()<br /> self.base_url = "https://www.baidu.com/"<br /> self.driver.get(self.base_url)<br /> self.driver.maximize_window()<br /> sleep(2)<br /><br /> @data(*getExcelTestData())<br /> # 传递数据<br /> def test_something(self, data):<br /> searchTerm, searchResult = tuple(data)<br /> driver = self.driver<br /> driver.find_element_by_xpath(".//*[@id='kw']").send_keys(searchTerm)<br /> driver.find_element_by_xpath(".//*[@id='su']").click()<br /> sleep(2)<br /> responseText = driver.find_element_by_xpath(".//*[@id='1']/h3/a").text<br /> self.assertEqual(responseText, searchResult)<br /><br /> def tearDown(self):<br /> self.driver.close()<br /> self.driver.quit()<br /><br />if __name__ == '__main__':<br /> unittest.main()<br /><br /><br />ExcelTestData.xlsx文件:
相关推荐
diyanpython 2020-11-05
YENCSDN 2020-11-17
lsjweiyi 2020-11-17
houmenghu 2020-11-17
Erick 2020-11-17
HeyShHeyou 2020-11-17
以梦为马不负韶华 2020-10-20
lhtzbj 2020-11-17
夜斗不是神 2020-11-17
pythonjw 2020-11-17
dingwun 2020-11-16
lhxxhl 2020-11-16
坚持是一种品质 2020-11-16
染血白衣 2020-11-16
huavhuahua 2020-11-20
meylovezn 2020-11-20
逍遥友 2020-11-20