Python ddt读取CSV文件数据驱动

import csv<br />import unittest<br />from time import sleep<br /><br />from ddt import ddt, data, unpack<br />from selenium import webdriver<br /><br /><br />def getCsvData():<br />    # 读取CSV文件<br />    value_rows = []<br />    with open('./TextData.csv', encoding='UTF-8') as f:<br />        f_csv = csv.reader(f)<br />        next(f_csv)<br />        for r in f_csv:<br />            value_rows.append(r)<br />    return value_rows<br /><br />@ddt<br /># 数据驱动<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(*getCsvData())<br />    # 测试数据<br /><br />    @unpack<br />    # 解包<br />    def test_something(self, searchTerm, searchResult):<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 />中国,中国_百度百科<br />美国,美国_百度百科<br />英国,英国_百度百科

相关推荐