selenium3+python3——数据驱动(DDT)
一、环境准备
1、安装
1.安装ddt模块,打开cmd输入pip install ddt在线安装
>>pip install ddt
2、参考代码:
DDT(Data-Driven Tests)是针对unittest单元测试框架设计的扩展库。
from selenium import webdriver import time from ddt import ddt, data, file_data, unpack import unittest @ddt class TestBaidu(unittest.TestCase): @classmethod def setUpClass(cls): cls.driver = webdriver.Chrome() cls.base_url = "http://www.baidu.com" def baidu_search(self, search_key): self.driver.get(self.base_url) self.driver.find_element_by_id("kw").send_keys(search_key) self.driver.find_element_by_id("su").click() time.sleep(2) # 参数化使用方式一:列表 @data(["case1", "selenium"], ["case2", "python"], ["case3", "参数"]) @unpack def test_search01(self,case, search_key): print("第一组测试用例:", case) self.baidu_search(search_key) self.assertEqual(self.driver.title, search_key + "_百度搜索") # 参数化使用方式二:元组 @data(("case1", "selenium"), ("case2", "python"), ("case3", "参数")) @unpack def test_search02(self, case, search_key): print("第二组测试用例:", case) self.baidu_search(search_key) self.assertEqual(self.driver.title, search_key + "_百度搜索") # 参数化使用方式三:字典 @data({"search_key": "selenium"}, {"search_key": "python"}, {"search_key": "参数"}) @unpack def test_search03(self, search_key): print("第三组测试用例:", search_key) self.baidu_search(search_key) self.assertEqual(self.driver.title, search_key + "_百度搜索") @classmethod def tearDownClass(cls): cls.driver.quit() if __name__ == ‘__main__‘: unittest.main(verbosity=2)
结果:
第一组测试用例: case1
test_search01_1___case1____selenium__ (__main__.TestBaidu) ... ok
第一组测试用例: case2
test_search01_2___case2____python__ (__main__.TestBaidu) ... ok
第一组测试用例: case3
test_search01_3___case3____参数__ (__main__.TestBaidu) ... ok
第二组测试用例: case1
test_search02_1___case1____selenium__ (__main__.TestBaidu) ... ok
第二组测试用例: case2
test_search02_2___case2____python__ (__main__.TestBaidu) ... ok
第二组测试用例: case3
test_search02_3___case3____参数__ (__main__.TestBaidu) ... ok
第三组测试用例: selenium
test_search03_1 (__main__.TestBaidu) ... 第三组测试用例: python
ok
test_search03_2 (__main__.TestBaidu) ... ok
第三组测试用例: 参数
test_search03_3 (__main__.TestBaidu) ... ok
----------------------------------------------------------------------
Ran 9 tests in 29.285s
OK
3、参数化读取json
在同一目录下,创建ddt_data_file
{ "case1":{"search_key": "python"}, "case2": {"search_key": "selenium"}, "case3": {"search_key": "fdhdhd"} }
from selenium import webdriver import time from ddt import ddt, data, file_data, unpack import unittest @ddt class TestBaidu(unittest.TestCase): @classmethod def setUpClass(cls): cls.driver = webdriver.Chrome() cls.base_url = "http://www.baidu.com" def baidu_search(self, search_key): self.driver.get(self.base_url) self.driver.find_element_by_id("kw").send_keys(search_key) self.driver.find_element_by_id("su").click() time.sleep(2) @file_data(‘ddt_data_file‘) def test_search03(self, search_key): print("第四组测试用例:", search_key) self.baidu_search(search_key) self.assertEqual(self.driver.title, search_key + "_百度搜索") @classmethod def tearDownClass(cls): cls.driver.quit() if __name__ == ‘__main__‘: unittest.main(verbosity=2)
运行结果:
第四组测试用例: python
test_search03_00001_case1 (__main__.TestBaidu)
test_search03_00001_case1 ... ok
第四组测试用例: selenium
test_search03_00002_case2 (__main__.TestBaidu)
test_search03_00002_case2 ... 第四组测试用例: fdhdhd
ok
test_search03_00003_case3 (__main__.TestBaidu)
test_search03_00003_case3 ... ok
----------------------------------------------------------------------
Ran 3 tests in 14.049s
OK
Process finished with exit code 0