python基础之ddt的使用
1.为什么使用ddt
在做测试过程中如下测试数据存在大量的重复性,需要把测试数据参数化,提高代码的复用性
cases=[{'params':{'username':'python','password1':'1234567890','password2':'1234567890'},
'title':'注册成功','except':{"code": 1, "msg": "注册成功"}},
{'params':{'username':'','password1':'123456','password2':'1234567890'},
'title':'用户名为空','except':{"code": 0, "msg": "所有参数不能为空"}},
{'params':{'username':'python42','password1':'','password2':'1234567890'},
'title':'密码1为空','except':{"code": 0, "msg": "所有参数不能为空"}}]
2.引入ddt,需要配合unittest使用
from ddt import ddt,data
case=[1,2,3,4,5]
@ddt
class TestCase(unittest.TestCase):
@data(*case)#*代表将case拆包,去掉一层外套
def test_case(self,item):#定义item为变量名,接收使用ddt处理过的数据
print(item)
打印结果:
1
2
3
4
5
3.也可使用unittestreport带的ddt模块进行数据参数化,unittestreport安装方法:pip install unittestreport
from unittestreport import ddt,list_data
case=[1,2,3,4,5]
@ddt
class TestCase(unittest.TestCase):
@list_data(case)#此处不需要对case进行拆包,直接传入即可
def test_case(self,item):
print(item)
打印结果:
1
2
3
4
5
本文来自博客园,作者:大头~~,转载请注明原文链接:https://www.cnblogs.com/xiaoying-guo/p/14994447.html

浙公网安备 33010602011771号