python-ddt 数据驱动测试
1 # @File : learn_ddt.py 2 3 #-*- coding:utf-8 -*- 4 5 #本次学习:ddt ---data drive test--数据驱动测试 6 #1.安装 pip install ddt 7 #2.用途:结合单元测试去执行用例 8 #3.本质:类的装饰器 9 10 # def print_msg(*args):#动态参数 11 # print(args)#*args 到了函数内部之后就变成一个元祖 12 # print('参数的长度:',len(args)) 13 # 14 # a=(1,2,3) 15 # print_msg(*a)#拆分一层 16 # 17 # b=[(1,2),3,[4,5]] 18 # print_msg(*b) 19 20 import unittest 21 from ddt import ddt,data,unpack 22 23 test_data=[{'param':{'mobilephone':18688773467,'pwd':'123456'},'http_method':'get','excepted':'登录成功', 24 'url':'http://47.107.168.87:8080/futureloan/mvc/api/member/login'}, 25 26 {'param':{'mobilephone':18688773467,'pwd':'1234567'},'http_method':'post','excepted':'用户名或密码错误', 27 'url':'http://47.107.168.87:8080/futureloan/mvc/api/member/login'}, 28 29 {'param':{'mobilephone':18688773467,'amount':'1000'},'http_method':'post','excepted':'充值成功', 30 'url':'http://47.107.168.87:8080/futureloan/mvc/api/member/recharge'}] 31 32 @ddt #装饰类 33 class TestMath(unittest.TestCase): 34 35 @data(test_data) #@data装饰方法 36 def test_001(self,item): 37 print('-----------用例1-----------') 38 print('item:',item) 39 40 @data(*test_data) #@data装饰方法, 加上* 拆分一层(按逗号拆分) 41 def test_002(self,item): 42 print('-----------用例2-----------') 43 print('item:',item) 44 45 @data(*test_data) 46 @unpack#在@data拆分的基础上,再拆分一次,并用等量的变量接收这些数据(如果是字典,需要用key) 47 def test_002(self,param,http_method,excepted,url): 48 print('-----------用例3-----------') 49 print('param:',param) 50 print('http_method:',http_method) 51 print('excepted:',excepted) 52 print('url:',url) 53 54 if __name__ == '__main__': 55 unittest.main()