Python数据驱动工具——DDT
参考链接1:https://www.cnblogs.com/miniren/p/7099187.html
参考链接2:https://www.imooc.com/article/14861
1、安装ddt模块:cmd命令下:pip install ddt
2、用途:结合单元测试unittest去执行用例
3、本质:类的装饰器
4、python 的unittest 没有自带数据驱动功能。所以如果使用unittest,同时又想使用数据驱动,那么就可以使用DDT来完成
import unittest import ddt test_case=[{'case_id':1,'title':'正常登录','url':'http://ip:9999/futureloan/mvc/api/member/login','data':{'mobilephone':'13417467890','pwd':'123456'},'method':'get','expected':'10001'}, {'case_id':2,'title':'密码错误','url':'http://ip:9999/futureloan/mvc/api/member/login','data':{'mobilephone':'13417467890','pwd':'12345678'},'method':'get','expected':'20111'}, {'case_id':3,'title':'手机号错误','url':'http://ip:9999/futureloan/mvc/api/member/login','data':{'mobilephone':'13417467890666','pwd':'123456'},'method':'get','expected':'20111'}, {'case_id':4,'title':'密码为空','url':'http://ip:9999/futureloan/mvc/api/member/login','data':{'mobilephone':'13417467890','pwd':''},'method':'get','expected':'20103'}, {'case_id':5,'title':'手机号为空','url':'http://ip:9999/futureloan/mvc/api/member/login','data':{'mobilephone':'','pwd':'123456'},'method':'post','expected':'2013'}] @ddt.ddt class TestCase(unittest.TestCase): @ddt.data(test_case) def test01(self,data): print('-----------用例1-----------') print('data:',data) # -----------用例1----------- # data: [{'method': 'get', 'case_id': 1, 'data': {'pwd': '123456', 'mobilephone': '13417467890'}, 'url': 'http://ip:9999/futureloan/mvc/api/member/login', 'title': '正常登录', 'expected': '10001'}, {'method': 'get', 'case_id': 2, 'data': {'pwd': '12345678', 'mobilephone': '13417467890'}, 'url': 'http://192.168.135.128:9999/futureloan/mvc/api/member/login', 'title': '密码错误', 'expected': '20111'}, {'method': 'get', 'case_id': 3, 'data': {'pwd': '123456', 'mobilephone': '13417467890666'}, 'url': 'http://192.168.135.128:9999/futureloan/mvc/api/member/login', 'title': '手机号错误', 'expected': '20111'}, {'method': 'get', 'case_id': 4, 'data': {'pwd': '', 'mobilephone': '13417467890'}, 'url': 'http://192.168.135.128:9999/futureloan/mvc/api/member/login', 'title': '密码为空', 'expected': '20103'}, {'method': 'post', 'case_id': 5, 'data': {'pwd': '123456', 'mobilephone': ''}, 'url': 'http://192.168.135.128:9999/futureloan/mvc/api/member/login', 'title': '手机号为空', 'expected': '2013'}] @ddt.data(*test_case) #@data装饰方法, 加上* 拆分一层(按逗号拆分) def test_02(self,data): print('-----------用例2-----------') print('data:',data) # -----------用例2----------- # data: {'data': {'mobilephone': '13417467890', 'pwd': '123456'}, 'url': 'http://ip:9999/futureloan/mvc/api/member/login', 'method': 'get', 'expected': '10001', 'case_id': 1, 'title': '正常登录'} # -----------用例2----------- # data: {'data': {'mobilephone': '13417467890', 'pwd': '12345678'}, 'url': 'http://ip:9999/futureloan/mvc/api/member/login', 'method': 'get', 'expected': '20111', 'case_id': 2, 'title': '密码错误'} # -----------用例2----------- # data: {'data': {'mobilephone': '13417467890666', 'pwd': '123456'}, 'url': 'http://ip:9999/futureloan/mvc/api/member/login', 'method': 'get', 'expected': '20111', 'case_id': 3, 'title': '手机号错误'} # -----------用例2----------- # data: {'data': {'mobilephone': '13417467890', 'pwd': ''}, 'url': 'http://ip:9999/futureloan/mvc/api/member/login', 'method': 'get', 'expected': '20103', 'case_id': 4, 'title': '密码为空'} # -----------用例2----------- # data: {'data': {'mobilephone': '', 'pwd': '123456'}, 'url': 'http://ip:9999/futureloan/mvc/api/member/login', 'method': 'post', 'expected': '2013', 'case_id': 5, 'title': '手机号为空'} @ddt.data(*test_case) #拆分后的数据是字典格式 @ddt.unpack #在@data拆分的基础上,再拆分一次,并用等量的变量接收这些数据(如果是字典,需要用key) def test_03(self,case_id,title,url,data,method,expected): #字典格式的数据再次拆分,参数要和测试用例的key一样 print('-----------用例3-----------') print('case_id:',case_id) print('title:',title) print('url:',url) print('data:',data) print('method:',method) print('excepted:',expected) # -----------用例3----------- # case_id: 1 # title: 正常登录 # url: http://ip:9999/futureloan/mvc/api/member/login # data: {'mobilephone': '13417467890', 'pwd': '123456'} # method: get # excepted: 10001 # -----------用例3----------- # case_id: 2 # title: 密码错误 # url: http://ip:9999/futureloan/mvc/api/member/login # data: {'mobilephone': '13417467890', 'pwd': '12345678'} # method: get # excepted: 20111 # -----------用例3----------- # case_id: 3 # title: 手机号错误 # url: http://ip:9999/futureloan/mvc/api/member/login # data: {'mobilephone': '13417467890666', 'pwd': '123456'} # method: get # excepted: 20111 # -----------用例3----------- # case_id: 4 # title: 密码为空 # url: http://ip:9999/futureloan/mvc/api/member/login # data: {'mobilephone': '13417467890', 'pwd': ''} # method: get # excepted: 20103 # -----------用例3----------- # case_id: 5 # title: 手机号为空 # url: http://ip:9999/futureloan/mvc/api/member/login # data: {'mobilephone': '', 'pwd': '123456'} # method: post # excepted: 2013 if __name__ == '__main__': unittest.main()