The following editor will bring you a Python implementation method of efficient test case organization algorithm pairwise. The editor thinks it’s pretty good, so I’ll share it with you now and give it as a reference. Let’s follow the editor and take a look.
Opening:
During the test process, test cases are organized for multi-parameter parameters with multiple values. , I have been using [Orthogonal Analysis Method] to organize use cases. To put it bluntly, it means to make a complete combination of all the values of each parameter with the values of other parameters. It is implemented with a Python script, which is the product method (also known as flute) in the itertools module. Karl product method).
The advantage of the orthogonal analysis method is that the test case coverage rate is 100%. The disadvantage is that the number of test cases is huge and the labor consumption of executing the use cases is huge.
Pairwise algorithm is derived from the optimization of traditional orthogonal analysis methods, and its theory comes from mathematical statistics. To be honest, I cannot understand academic papers on mathematical statistics, so I can only find some popular and simple explanations on the Internet to understand their basic meaning.
Many people on the Internet use [operating system, browser, language environment] as examples. I also use the same example:
Operating system: W (Windows), L ( Linux), Mac (Mac); Browser: M (Firefox), O (Opera), IE; Language environment: C (Chinese), E (English)
According to the orthogonal analysis method: 3x3x2 will be generated =18 combination methods, test case coverage 100%.
Pairwise paired test case organization method can be compressed into 9 combinations. Therefore, the disadvantage is that the number of test cases is small, and the disadvantage is that there will definitely be missed tests.
Introduction:
The core concept of Pairwise algorithm
1. A set of test cases (each use case consists of 3 parameter values, such as [W, M, C]). Each 2 elements are combined, and combined in pairs, there are 3 combinations (positioned) [W,M][W,C][M,C]);
2. If this first set of tests uses three combinations of two by two, the comparison principle is: [W,M] It will only be compared with the first element in other groups, and [W,C] will only be compared with the second element in other groups. . . . ; The three elements
[W,M][W,C][M,C] respectively appear in the elements with the same positions in other valid groups, so this group of Cases can be considered as redundant Cases, and to delete.
Explanation of terms: [Effective group] means a group that has not been deleted and a group that has not been compared. For example: If groups 1 and 3 are deleted, the effective groups to be compared with group 4 are groups 2, 5, 6, 7...18. The effective group has gone through the pit here%>_<%
3. Finally, the test case is obtained, which is the optimal set of test cases calculated by the pairing algorithm.
Brilliant academic proof
Pairwise was first proposed by L. L. Thurstone (29 May1887 – 30 September 1955) in 1927. He is an American psychostatistician. Pairwise is also a product based on mathematical statistics and optimization of traditional orthogonal analysis methods.
Pairwise is based on the following two assumptions:
(1) Each dimension is orthogonal, that is, each dimension is independent of each other. intersection.
(2) According to mathematical statistical analysis, 73% of defects (35% for single factor and 38% for two factors) are caused by a single factor or the interaction of 2 factors. 19% of defects are caused by the interaction of 3 factors.
Therefore, pairwise is generated based on the use case set that covers the interaction of all 2 factors and is the most cost-effective.
Text
1. Idea
For a test scenario How to start from inputting the tested conditions to producing Pairwise test cases, using Python programming ideas are as follows:
1. Set allparams=[['M','O','P'],[' W','L','I'],['C','E']] perform Cartesian product full combination processing to generate a one-dimensional array (len=N) of the full set of test cases generated by the regular analysis method;
2. Decompose each test case in the full set of test cases into two-by-two combinations to generate a two-dimensional array with the same length as the full set of test cases (one-dimensional len=N);
3. Use the Python version of the Pairwise algorithm to eliminate invalid test cases, and finally obtain a valid pair test case set;
The 1st and 2nd functions of the code are written using Python’s own mathematical calculation library itertools, and the 3rd code The function is the code that I came up with.
2. Upload the code directly
##
# -*- coding: utf-8 -*- from datetime import * import random,os,copy,time import logging import itertools ''' #Author:Kuzaman #Time:2017-07-18 ''' class utils2 : #1、笛卡尔积 对参数分组全排列 def product(self,tuple1): newlist=[] for x in eval('itertools.product'+str(tuple(tuple1))): newlist.append(x) return newlist #2、对笛卡尔积处理后的二维原始数据进行N配对处理,得到Pairwise计算之前的数据 def get_pairslist(self,lista): pwlist = [] for i in lista: subtemplist = [] for sublista in itertools.combinations(i, 2): subtemplist.append(sublista) pwlist.append(subtemplist) return pwlist #3、进行Pirwise算法计算 def pairwise(self,listb): sublistlen = len(listb[1]) flag = [0]*sublistlen templistb = copy.deepcopy(listb) delmenu = [] holdmenu=[] self.pprint (listb) print ('--'*25) for lb in listb: for sublb in lb: for k in templistb: Xa = lb.index(sublb) Ya = listb.index(lb) if k != lb and sublb == k[Xa]: # print (sublb,'===>' ,k[Xa],'相等了。。。') flag[Xa] = 1 break else: # print (sublb,'===>' ,k[Xa],'不不不等了。。。') flag[Xa] = 0 # print ('下标%d,子元素 %s 双匹配对比结果flag:%s'%(listb.index(lb),lb,flag)) if 0 not in flag: num = listb.index(lb) delmenu.append(num) templistb.remove(lb) # print ('下标为%d行应删除,内容=%s,'%(num,lb)) # print ('delmenu:',delmenu) else: num2 = listb.index(lb) holdmenu.append(num2) # print ('下标为%d行应保留,内容=%s,'%(num2,lb)) # print('holdmenu=',holdmenu) # print ('***'*20) print ('保留元素列表:%s \n匹配重复元素列表:%s'%(holdmenu,delmenu)) return templistb def pwresult(self,slist,delmenu): for x in delmenu: slist.remove(slist[x]) return slist def pprint(self,list): for i in list: print ('line %d:'%(list.index(i)+1),i) if __name__ == '__main__': u2 = utils2() allparams=[['M','O','P'],['W','L','I'],['C','E']]#,'K'],[1,2,3],['Yes','No']] str = u2.product(allparams) strpc = u2.get_pairslist(str) finallist = u2.pairwise(strpc) print('最终保留测试用例个数:%d 个'%(len(finallist))) u2.pprint(finallist)
Code interpretation:
The third for loop code lines 39~48 are mainly used to vertically determine whether the element to be detected and the element at the same position have the same numberThe second for loop code 38~48 Line, pair the two test cases in a group, and compare them with the elements at the same position from left to right.The first for loop code line 37~48 traverses each group of test cases.第50~58行代码,判断一组用例的两两配对在其他组同位置上从上到下都能找到相同元素,则将改无效Case从templistb中删除,保持templistb的有效性。
执行结果:
line 1: [('M', 'W'), ('M', 'C'), ('W', 'C')] <---第二个函数get_pairslist(self,lista)处理后的两两配对组合 line 2: [('M', 'W'), ('M', 'E'), ('W', 'E')] <---同第一行解释 line 3: [('M', 'L'), ('M', 'C'), ('L', 'C')] line 4: [('M', 'L'), ('M', 'E'), ('L', 'E')] line 5: [('M', 'I'), ('M', 'C'), ('I', 'C')] line 6: [('M', 'I'), ('M', 'E'), ('I', 'E')] line 7: [('O', 'W'), ('O', 'C'), ('W', 'C')] line 8: [('O', 'W'), ('O', 'E'), ('W', 'E')] line 9: [('O', 'L'), ('O', 'C'), ('L', 'C')] line 10: [('O', 'L'), ('O', 'E'), ('L', 'E')] line 11: [('O', 'I'), ('O', 'C'), ('I', 'C')] line 12: [('O', 'I'), ('O', 'E'), ('I', 'E')] line 13: [('P', 'W'), ('P', 'C'), ('W', 'C')] line 14: [('P', 'W'), ('P', 'E'), ('W', 'E')] line 15: [('P', 'L'), ('P', 'C'), ('L', 'C')] line 16: [('P', 'L'), ('P', 'E'), ('L', 'E')] line 17: [('P', 'I'), ('P', 'C'), ('I', 'C')] line 18: [('P', 'I'), ('P', 'E'), ('I', 'E')] <----同第一行解释 -------------------------------------------------- 保留元素列表:[1, 3, 4, 7, 9, 10, 12, 14, 17] <----有效用例在数组中下标 匹配重复元素列表:[0, 2, 5, 6, 8, 11, 13, 15, 16] <----被剔除的无效测试用例在数组中下标 最终保留测试用例个数:9 个 line 1: [('M', 'W'), ('M', 'E'), ('W', 'E')] line 2: [('M', 'L'), ('M', 'E'), ('L', 'E')] line 3: [('M', 'I'), ('M', 'C'), ('I', 'C')] line 4: [('O', 'W'), ('O', 'E'), ('W', 'E')] line 5: [('O', 'L'), ('O', 'E'), ('L', 'E')] line 6: [('O', 'I'), ('O', 'C'), ('I', 'C')] line 7: [('P', 'W'), ('P', 'C'), ('W', 'C')] line 8: [('P', 'L'), ('P', 'C'), ('L', 'C')] line 9: [('P', 'I'), ('P', 'E'), ('I', 'E')] [Finished in 0.2s]
三、代码核心内容白话解释
pairwise(self,listb)函数包含3层for循环,先画一个二维数组:
i[0] i[1] i[2] listb.index(i)=0 : [('M', 'W'), ('M', 'C'), ('W', 'C')] listb.index(i)=1 : [('M', 'W'), ('M', 'E'), ('W', 'E')] listb.index(i) : [('M', 'L'), ('M', 'C'), ('L', 'C')] listb.index(i) : [('M', 'L'), ('M', 'E'), ('L', 'E')] listb.index(i) : [('M', 'I'), ('M', 'C'), ('I', 'C')] listb.index(i) : [('M', 'I'), ('M', 'E'), ('I', 'E')] listb.index(i) : [('O', 'W'), ('O', 'E'), ('W', 'E')] listb.index(i) : [('O', 'L'), ('O', 'C'), ('L', 'C')] listb.index(i) : [('O', 'L'), ('O', 'E'), ('L', 'E')] listb.index(i) : [('O', 'I'), ('O', 'C'), ('I', 'C')] listb.index(i)=n : [('O', 'I'), ('O', 'E'), ('I', 'E')]
二维列表 listb ,其中的行(发音:hang,二声。横着的那排)从上到下就是第一层for循环 ;每一行中的i[0],i[1],i[2]就是第二层for循环从左至右;第三次for循环元素i[x]从上之下与有效组 templistb通位置元素的对比。
1、第n行的i[0]要和有效templistb的其他行的i[0]元素对比(第三for),如果有相等的,记录一个标识 如 flag1=True,如果没有相等的记录falg1=False;
2、直到第二for中的i[0],i[1],i[2]都进行对比后,会得到 [flag1,flag2,flag3 ],所有flag=True则该行为无效用例
3、第一for遍历全部组合,最终得到保留下来的有效templistb
见图:
完结篇
以上是自己编写的pairwise的全部内容,此算法共耗时3天:
第一天在确定这究竟是什么算法,看了很多学术文献,看不懂;
第二天开始写程序,for的嵌套循环设计耽误很久;
第三天程序成型,有执行结果,发现与参考文章结论不同,随后再仔细研读参考文章,发现掉坑里了。重新推翻代码按照正确思路,用1个小时完成最终结果。
本人做测试的,还不是专业的测试开发,写代码比较费劲,真正应了设计占70%,编码占30%的理。如果像基础在差点,逻辑在乱点,就只能用时间堆了。
The above is the detailed content of How Python implements organization algorithm pairwise (efficient test cases). For more information, please follow other related articles on the PHP Chinese website!