首先,
添加ddt模块:
import unittest
from time import sleep
from ddt import ddt, data, unpack
# 导入ddt模块
from selenium import webdriver
@ddt
# 声明使用ddt
class MyTestCase(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
self.base_url = "https://www.baidu.com/"
self.driver.get(self.base_url)
self.driver.maximize_window()
sleep(2)
@data(["中国", "中国_百度百科"],
["美国", "美国_百度百科"],
["英国", "英国_百度百科"])
# 测试数据
@unpack
# 对测试数据进行解包并传递
def test_something(self, searchTerm, searchResult):
driver = self.driver
driver.find_element_by_xpath(".//*[@id='kw']").send_keys(searchTerm)
driver.find_element_by_xpath(".//*[@id='su']").click()
sleep(2)
responseText = driver.find_element_by_xpath(".//*[@id='1']/h3/a").text
self.assertEqual(responseText, searchResult)
def tearDown(self):
self.driver.close()
self.driver.quit()
if __name__ == '__main__':
unittest.main()