DDT (Data-Drivers- Tests) 允许使用不同的测试数据运行同一个测试用例,展示为不同的测试用例。
第一步: pip安装 ddt
第二步: 创建test_baidu_ddt.py
1. 测试类要使用@ddt 修饰
2. 不同形式的参数化: 列表,字典(key 要与测试方法保持一致),元祖
import unittest
from selenium import webdriver
from time import sleep
from ddt import ddt, data, file_data, unpack
@ddt
class TestBaidu(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.driver = webdriver.Chrome()
cls.base_url = "https://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()
sleep(3)
# 参数化使用方式一 ------------------ 列表
@data(["case1", "selenium"], ["case2", "ddt"], ["case3", "python"])
@unpack
def test_search1(self, case, search_key):
print("第一组测试用例:", case)
self.baidu_search(search_key)
print("self.driver.title:", self.driver.title)
self.assertEqual(self.driver.title, search_key + "_百度搜索")
# 参数化使用方式二-------------------- 元组
@data(("case1", "selenium"), ("case2", "ddt"), ("case3", "python"))
@unpack
def test_search2(self, case, search_key):
print("第二组测试用例:", case)
self.baidu_search(search_key)
self.assertEqual(self.driver.title, search_key + "_百度搜索")
print("self.driver.title:", self.driver.title)
# 参数化使用方式三--------------------- 字典
@data({"search_key": "selenium"},
{"search_key": "ddt"},
{"search_key": "python"})
@unpack
def test_search3(self, search_key):
print("第三组测试用例:", search_key)
self.baidu_search(search_key)
self.assertEqual(self.driver.title, search_key + "_百度搜索")
print("self.driver.title:", self.driver.title)
# 参数化读取JSON文件
@file_data('ddt_data_file.json')
def test_search4(self, search_key):
print("第四组测试用例:", search_key)
self.baidu_search(search_key)
self.assertEqual(self.driver.title, search_key + "_百度搜索")
print("self.driver.title:", self.driver.title)
# 参数化读取yaml文件
@file_data('ddt_data_file.yaml')
def test_search5(self, case):
search_key = case[0]["search_key"]
print("第五组测试用例:", search_key)
self.baidu_search(search_key)
self.assertEqual(self.driver.title, search_key + "_百度搜索")
print("self.driver.title:", self.driver.title)
@classmethod
def tearDownClass(cls):
cls.driver.quit()
if __name__ == '__main__':
unittest.main(verbosity=2) #日志等级2
3.支持数据文件读取
3.1json 文件读取
数据文件:test_case/ddt_data_file.json @file_data('ddt_data_file.json') 读取 文件要与test_baidu_ddt.py 相同否则需要加上路径
{
"case1": {"search_key": "python"},
"case2": {"search_key": "ddt"},
"case3": {"search_key": "Selenium"}
}
3.2 yaml 文件读取:ddt_data_file.yaml
case1:
- search_key: "python"
case2:
- search_key: "ddt"
case3:
- search_key: "unittest"
每个数据会被解析为[{'search_key':'python'}] 获取值case[0]["search_key"]
# 参数化读取yaml文件
@file_data('ddt_data_file.yaml')
def test_search5(self, case):
search_key = case[0]["search_key"]
print("第五组测试用例:", search_key)
self.baidu_search(search_key)
self.assertEqual(self.driver.title, search_key + "_百度搜索")
print("self.driver.title:", self.driver.title)
第三步:创建 unittestdemo1\baidu_ddt_run_tests.py
HTMLTestRunner
import time
import unittest
from HTMLTestRunner import HTMLTestRunner
# # 把测试报告作为附件发送到指定邮箱。
# def send_mail(report):
# yag = yagmail.SMTP(user="1164512033@qq.com",
# password="zjx3601296",
# host='mail.qq.com')
# subject = "标题,自动化测试报告"
# contents = "正文,请查看附件。"
# yag.send('1164512033@qq.com', subject, contents, report)
# print('email has send out !')
if __name__ == '__main__':
# 定义测试用例的目录为当前目录
print("----__main__-------")
test_dir = './test_case' # 测试用例的路径
suit = unittest.defaultTestLoader.discover(test_dir, pattern='test_baidu_dtt.py')
print("suit===========",suit)
# 取当前日期时间
now_time = time.strftime("%Y-%m-%d %H_%M_%S")
html_report = './test_report/' + now_time + 'result.html'
fp = open(html_report, 'wb')
# 调用HTMLTestRunner,运行测试用例
runner = HTMLTestRunner(stream=fp,
title="baidu_ddt测试报告",
description="运行环境:Windows 10, Chrome浏览器"
)
runner.run(suit)
print("run -------------------------------------suit")
fp.close()
#send_mail(html_report) # 发送报告
运行结果: baidu_ddt_run_tests.py 不能以test 开头