作用:将字典进行解包,输出测试用例中的单个字段的值
def unpack(func):
func.unpack = True
return func
def create_test_method(method, value):
# todo 闭包函数作用是将测试方法和函数放到闭包中,进行数据锁定
def wrapper(self):
# todo unpack装饰器的实现
if hasattr(method, 'unpack'):
if isinstance(value, dict):
method(self, **value)
else:
method(self, *value)
else:
method(self, value)
return wrapper
test_datas=[
{'id':1,'title':'测试用例1'},
{'id':2,'title':'测试用例2'},
{'id':3,'title':'测试用例3'},
{'id':4,'title':'测试用例4'}
]
from test_ddt import ddt,data,json_file,yaml_file,unpack
import unittest
@ddt
class TestDemo(unittest.TestCase):
@unpack
@data(*test_datas)
def test_demo(self,id,title):
print('测试用例执行',id,title)
测试用例执行 1 测试用例1
测试用例执行 2 测试用例2
测试用例执行 3 测试用例3
测试用例执行 4 测试用例4
test_datas=[
{'id':1,'title':'测试用例1'},
{'id':2,'title':'测试用例2'},
{'id':3,'title':'测试用例3'},
{'id':4,'title':'测试用例4'}
]
from test_ddt import ddt,data,json_file,yaml_file,unpack
import unittest
@ddt
class TestDemo(unittest.TestCase):
@data(*test_datas)
def test_demo(self, item):
print('测试用例执行', item)
测试用例执行 {‘id’: 1, ‘title’: ‘测试用例1’}
测试用例执行 {‘id’: 2, ‘title’: ‘测试用例2’}
测试用例执行 {‘id’: 3, ‘title’: ‘测试用例3’}
测试用例执行 {‘id’: 4, ‘title’: ‘测试用例4’}
作用:测试数据编写在json文件中,读取测试数据用于测试
def json_file(file_path):
'''
:param file_path: json文件路径
:return:
'''
def wrapper(func):
try:
with open(file_path, "r", encoding="utf-8") as f:
datas = json.load(f)
except:
with open(file_path, "r", encoding="gbk") as f:
datas = json.load(f)
if not isinstance(datas, list):
raise ValueError('json文件内的用例数据格式不符护规范')
func.datas = datas
return func
return wrapper
from test_ddt import ddt,data,json_file,yaml_file,unpack
import unittest
@ddt
class TestDemo(unittest.TestCase):
# @unpack
# @data(*test_datas)
# def test_demo(self,id,title):
# print('测试用例执行',id,title)
# @data(*test_datas)
# def test_demo(self, item):
# print('测试用例执行', item)
@json_file(r'D:\project_development\测试开发\day5_unittest框架用例参数化库ddt的使用\data.json')
def test_demo1(self,item):
print('测试用例执行',item)
[
{
"title": "用例1",
"data": "python"
},
{
"title": "用例2",
"data": "python1"
},
{
"title": "用例3",
"data": "python2"
}
]
测试用例执行 {‘title’: ‘用例1’, ‘data’: ‘python’}
测试用例执行 {‘title’: ‘用例2’, ‘data’: ‘python1’}
测试用例执行 {‘title’: ‘用例3’, ‘data’: ‘python2’}
作用:测试数据编写在yaml文件中,读取测试数据用于测试
def yaml_file(file_path):
def wrapper(func):
try:
with open(file_path, "r", encoding="utf-8") as f:
datas = yaml.load(f, Loader=yaml.FullLoader)
except:
with open(file_path, "r", encoding="gbk") as f:
datas = yaml.load(f, Loader=yaml.FullLoader)
if not isinstance(datas, list):
raise ValueError('json文件内的用例数据格式不符护规范')
func.datas = datas
return func
return wrapper
from test_ddt import ddt,data,json_file,yaml_file,unpack
import unittest
@ddt
class TestDemo(unittest.TestCase):
@yaml_file(r'D:\project_development\测试开发\day5_unittest框架用例参数化库ddt的使用\data.yaml')
def test_demo2(self,item):
print('测试用例执行', item)
- title: 用例1
data: python_1
- title: 用例2
data: python_2
- title: 用例3
data: python_3
测试用例执行 {‘title’: ‘用例1’, ‘data’: ‘python_1’}
测试用例执行 {‘title’: ‘用例2’, ‘data’: ‘python_2’}
测试用例执行 {‘title’: ‘用例3’, ‘data’: ‘python_3’}