2017年7月28日,今天是一个里程碑的日子,根据参数及数值通过结对有效算法自动生成字典形式的测试用例功能模块完成,鼓掌,鼓掌!
替换postdata模板内指定元素值的代码如下:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 import copy 2 import createPWtestcase 3 dicta ={ 4 "H": { 5 "ACID": "aaaaa", 6 "Ver": ["aaaa","bbbb"], 7 "CInf": [100,2,1000], 8 "TID": "df22839322dfceb0622b5c6be5879ed852d594d5" 9 }, 10 "B": { 11 "Login": { 12 "Type": "LP", 13 "Name": "18601231676", 14 "Pwd": "123456", 15 "ForToken": 1 16 }, 17 "TInfo": { 18 "IMSI": "460029636249253", 19 "IMEI": "868349029037662", 20 "AndroidID": "ed93e839f67279df", 21 "Mac": "68: 3e: 34: 14: 61: e7", 22 "NetType": "Wifi", 23 "Model": "m2note", 24 "OsVer": "5.1", 25 "AppVer": "4.0.0" 26 }, 27 "TT": "kAEBLgABABQzMjAxMTcwNjA1MTcyMjMxMDEzNwEACjMwMDA2NjkxNDIAAAFcjH7IjgERrT0YjQhgPAjXvmY9Z1lM8A" 28 } 29 } 30 31 dictd={'ACID': ['999','appchina'], 'Ver': [['IOS_5.0.0_1.0', 'IOS_5.0.0'],['Android2.0.0']], 'CInf': [100, 2, 1000], 32 'TID': ['df22839322dfceb0622b5c6be5879ed852d594d5'],'Type': [['LP'],['PH']], 'Name': [['18232441676'],['15806666690']], 33 'Pwd': ['123456'], 'ForToken':[ 1], 'IMSI': ['460029636249253'], 'IMEI': [['868349029037662'],['666666']], 'AndroidID': ['ed93e839f67279df'], 34 'Mac': ['68: 3e: 34: 14: 61: e7'], 'NetType': ['Wifi','4G','3G'], 'Model': ['m2note'], 'OsVer': [['5.1'],['ios9'],['IOS10']] 35 , 'AppVer': [['4.0.0'],['4.0.1'],['4.0.2']], 'TT': ['kAEBLgABABQzMjAxMTcwNjA1MTcyM']} 36 def quantizeJsonTemplet(targetDict,sourceList_dict): 37 copytargetDict = copy.deepcopy(targetDict) 38 def recursionReplace(copytargetDict,serchKey,newValue): 39 for k,v in copytargetDict.items(): 40 if isinstance(v,dict) : #值是字典元素,则递归处理 41 recursionReplace(v,serchKey,newValue) 42 else: 43 if k == serchKey: 44 # print ('-----Find it:',k) 45 copytargetDict[k] = newValue #对目标字典元素的值重写 46 return copytargetDict 47 48 for k,v in sourceList_dict.items():#遍历替换列表,使多测试用例同时生成 49 recursionReplace(copytargetDict,k,v) 50 return copytargetDict 51 52 53 #测试程序 54 u2 = createPWtestcase.utils2() 55 allvalues=dictd.values() 56 allkeys = dictd.keys() 57 58 finallist = u2.dictzip(allkeys,allvalues,2) 59 u2.pprint(finallist)
CTF2.py需要依赖的py文件:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 # -*- coding: utf-8 -*- 2 from datetime import * 3 import random,os,copy,time 4 import logging 5 import itertools 6 ''' 7 #Author:Kuzaman 8 #Time:2017-07-18 9 ''' 10 class utils2 : 11 #1、笛卡尔积 对参数分组全排列 12 def product(self,allparams): 13 newlist=[] 14 for x in eval('itertools.product'+str(tuple(allparams))): 15 newlist.append(x) 16 print (len(newlist)) 17 return newlist 18 19 #2、对笛卡尔积处理后的二维原始数据进行N配对处理,得到Pairwise计算之前的数据 20 def get_pairslist(self,productedlist,pairlen): 21 pwlist = [] 22 for i in productedlist: 23 subtemplist = [] 24 for sublista in itertools.combinations(i,pairlen): 25 subtemplist.append(sublista) 26 pwlist.append(subtemplist) 27 return pwlist 28 29 #3、进行Pirwise算法计算 30 def pairwise(self,allparams,pairlen): 31 productedlist=self.product(allparams) #productedlist笛卡尔积全排列组合的测试用例 32 # self.pprint(productedlist) 33 # print ('笛卡尔积全排列组合数量:',len(productedlist),'--'*11) 34 listb = self.get_pairslist(productedlist,pairlen) #listb:对测试用例结对拆分后的二维列表 35 sublistlen = len(listb[1]) #sublistlen:每个测试用例拆分后一维列表长度 36 flag = [0]*sublistlen #一条测试用例拆分后,每个结对在二维列表同位置上是否有相 37 #同值,其标识列表为flag,flag长度根据sublistlen改变 38 templistb = copy.deepcopy(listb)#【有效组】的原始值与listb相同 39 delmenu = [] #无效测试用例编号列表 40 holdmenu=[] #有效测试用例编号列表 41 # self.pprint (listb) 42 # print ('--'*7,'有效测试用例计算结果','--'*7) 43 for cow in listb: #一维遍历,等同于二维数组按照行遍历 44 for column in cow: #二维遍历,等同二维数组中任意一行按照‘列’横向遍历 45 for templistbcow in templistb: #【有效组=templistb】中按照行,从上至下遍历 46 Xa = cow.index(column) #待校验元素的横坐标 47 Ya = listb.index(cow) #待校验元素的纵坐标 48 #有效组中行不能是当前要对比元素所在的行, 49 #且带对比元素与【有效组】的行templistbcow中的坐标Xa元素相同, 50 #则认为对比成功,跳出第三层for循环。 51 if templistbcow != cow and column == templistbcow[Xa]: 52 # print (column,'===>' ,templistbcow[Xa],'相等了。。。') 53 flag[Xa] = 1 #1表示对比成功 54 break 55 else: #对比不成功,需要继续第三层for循环对比 56 # print (column,'===>' ,templistbcow[Xa],'不不不等了。。。') 57 flag[Xa] = 0 #0表示对比不成功 58 # print ('下标%d,子元素 %s 双匹配对比结果flag:%s'%(listb.index(cow),cow,flag)) 59 if 0 not in flag: #如果对比列表中都是1,表明该行的所有结对都在同列的对应位置找到了 60 num = listb.index(cow) 61 delmenu.append(num) #记录该无用用例所在总列表listb中的位置 62 templistb.remove(cow) #有效组中删除该无用用例,保持有效性 63 # print ('下标为%d行应删除,内容=%s,'%(num,cow)) 64 # print ('delmenu:',delmenu) 65 else: #如果有0出现在对比列表flag中,则表示该行所有结对组并未全在同列对应位置找到,此用例为有效用例应保留 66 num2 = listb.index(cow) 67 holdmenu.append(num2) #记录有效用例在总列表listb中的坐标 68 # print ('---------------下标为%d行应保留,内容=%s'%(num2,cow)) 69 # print('holdmenu=',holdmenu) 70 # print ('***'*20) 71 print ('保留元素列表:%s 匹配重复元素列表:%s'%(holdmenu,delmenu)) 72 return self.pwresult(productedlist,holdmenu) 73 74 def pwresult(self,slist,holdmenu): 75 holdparamslist = [] 76 for item in holdmenu: 77 holdparamslist.append(slist[item]) 78 return holdparamslist 79 80 def pprint(self,list): 81 for item in list: 82 print ('line %d:'%(list.index(item)+1),item) 83 # print (item) 84 def dictzip(self,listkeys,allparams,pairlen): 85 twoLevellistvalues = self.pairwise(allparams,pairlen) 86 list_dict = [] 87 for listvalues in twoLevellistvalues: 88 list_dict.append(dict(zip(listkeys,listvalues))) 89 return list_dict 90 91 if __name__ == '__main__': 92 u2 = utils2() 93 # allparams=[['M','O','P'],['W','L','I'],['C','E' 94 # allparamsdict={'os':['M','O','T'],'Brow':['L','I','T'],'top':['s','T','E','K'],'Level':[1,3],'flag':['Yes','No'],'is666':['666','']} 95 96 dictd={'ACID': ['999','appchina'], 'Ver': [['IOS_5.0.0_1.0', 'IOS_5.0.0'],['Android2.0.0']], 'CInf': [100],'TID': ['df22839322dfceb0622b5c6be5879ed852d594d5'] 97 ,'Type': ['LP','PH'], 'Name': ['15806666690'],'Pwd': ['123456'], 'ForToken': ['1'], 'IMSI': ["460029636249253"], 'IMEI': ['666666'], 98 'AndroidID': ['ed93e839f67279df'], 'Mac': ['68: 3e: 34: 14: 61: e7'], 'NetType': ['Wifi','4G'], 'Model': ['m2note'], 'OsVer': [['4.4'],['6.0'],['5.1']], 99 'AppVer': ['4.0.0','4.0.1'], 'TT': ['kAEBLgABABQzMjAxMTcwNjA1MTcyM']} 100 101 102 103 104 allvalues=dictd.values()#[['M','O','T'],['L','I','T'],['s','T','E','K'],[1,3],['Yes','No'],['666','']] 105 allkeys = dictd.keys() 106 u2.product(allvalues) 107 # finallist = u2.dictzip(allkeys,allvalues,2) 108 # print('最终保留测试用例个数:%d 个'%(len(finallist))) 109 # u2.pprint(finallist)