#参数化库: ddt
前提:安装ddt库,>>> python -m pip install ddt
import unittest
from ddt import ddt, data,unpack
from selenium import webdriver
from time import sleep
@ddt
class TestSearch(unittest.TestCase):
@classmethod #基于类级别只开启一次浏览器,执行当前目录下所有用例
def setUpClass(cls):
cls.dr = webdriver.Chrome()
cls.base_url = ‘https://www.baidu.com‘
@classmethod
def tearDownClass(cls):
cls.dr.quit()
@data(
["selenium",‘selenium_百度搜索‘],
["python",‘python_百度搜索‘],
["unittest",‘unittest_百度搜索‘],
)
@unpack
def test_search(self,searchkeys,assertResult):
dr = self.dr
dr.get(self.base_url)
dr.find_element_by_id("kw").send_keys(searchkeys)
dr.find_element_by_id("su").click()
sleep(3)
self.assertEqual(self.dr.title,assertResult)
if __name__ == ‘__main__‘:
unittest.main(verbosity=2)
执行结果: