码迷,mamicode.com
首页 > 编程语言 > 详细

selenium3+python3——数据驱动(DDT)

时间:2020-05-14 11:34:28      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:exit   ack   sea   打开   结果   chrome   元组   base   get   

一、环境准备

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

selenium3+python3——数据驱动(DDT)

标签:exit   ack   sea   打开   结果   chrome   元组   base   get   

原文地址:https://www.cnblogs.com/yuer02/p/12882269.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!