How Python implements the organization algorithm pairwise (efficient test case)

Source: Internet
Author: User
Tags pprint
The following small series for everyone to bring an efficient test case organization algorithm pairwise Python implementation method. Small series feel very good, now share to everyone, also for everyone to make a reference. Let's take a look at it with a little knitting.

Opening:

In the test process, for multi-parameter parameter multi-value cases of the test case organization, before the use of "orthogonal analysis" for use case organization, it is plainly that each parameter of all values and other parameters of the value of a full-amount combination, with the Python script implementation, is the product method in the Itertools module (also known as the Cartesian product method).

The advantage of the orthogonal analysis method is that the test case coverage 100%, the disadvantage of the large number of test cases, the implementation of the use cases of large consumption.

pairwise (pair) algorithm is derived from the optimization of the traditional orthogonal analysis method, and its theory is derived from mathematical statistics. It is not taboo to say that I do not understand the mathematical statistics in the academic paper, only from the Internet to find some simple words to understand the basic meaning.

Many people on the internet are examples of "operating system, browser, language environment" for example, I also do the same example:

Operating system: W (Windows), L (Linux), Mac (Mac), Browser: M (Firefox), O (Opera), IE; locale: C (Chinese), E (English)

According to the orthogonal analysis method: It will produce 3x3x2=18 combination method, test case coverage of 100%.

pairwise Knot Pair test Case organization method, can compress to 9 kinds of combination way. Therefore, a little bit of test cases, the disadvantage is that there will be a leak detection.

Introduction:

The core concept of pairwise algorithm

1, a set of test cases (each use case has 3 parameters of the value composition, such as [W,m,c]) in each of the 2 elements combined, 22 combinations, there are 3 combinations (position [w,m][w,c][m,c]);

2, if the first set of tests with 22 combination of 3 combinations, the principle of comparison: [W,m] will only compare with the first element of the other group, [W,c] will only compare with the second element in the other group .... ;

[W,m] [W,c] [M,c] These three elements appear in the remaining valid group location of the same element, you can consider this set of case is redundant case, and delete.

Noun Explanation: "Effective group" means groups that have not been deleted and groups that have not been compared. For example: 1th, 3 groups are deleted, then the 4th group to compare the effective group is the 2,5,6,7...18 group. Effective group stepped over the pit here%>_<%

3. The final test case is the optimal set of test cases calculated by the pair algorithm.

A brilliant academic proof.

Pairwise is L. L. Thurstone (may1887–30 September 1955) was first proposed in 1927. He is a psychologist in the United States. Pairwise is also the result of the optimization of the traditional orthogonal analysis method based on mathematical statistics.

Pairwise is based on the following 2 assumptions:

(1) Each dimension is orthogonal, that is, each dimension has no intersection with each other.

(2) According to mathematical statistical analysis, 73% of the defects (single-factor is 35%, two-factor is 38%) is a single-factor or 2-factor interaction produced. 19% of the defects are caused by the interaction of 3 factors.

As a result, pairwise is based on the highest cost performance of use case collections that cover all of the 2-factor interactions generated.

Body

First, the idea

For how a test scenario can be measured from the input condition to the output pairwise test case, use Python programming ideas as follows:

1, the allparams=[[' M ', ' O ', ' P '],[' W ', ' L ', ' I '],[' C ', ' E '] are all combined with Cartesian product to generate a one-dimensional array (len=n) of the full set of test cases produced by the regular analysis method;

2, each test case in the full-scale test case, the decomposition of the 22 combination of processing, to generate a full-scale test case set of the same length of the two-dimensional array (one-dimensional len=n);

3, using the Python version of the pairwise algorithm to eliminate invalid test cases, and finally get a valid set of pair test cases;

Code 1th, the 2 function is written using Python's own mathematical library itertools, the code of the 3rd function for my death to knock out the code.

Second, directly on the code


#-*-Coding:utf-8-*-from datetime import *import random,os,copy,timeimport loggingimport itertools ' #Author: Kuzaman# Time:2017-07-18 "class Utils2: #1, Cartesian product, grouping of parameters in full array def product (SELF,TUPLE1): newlist=[] for X in eval (' Itertools.product ' +str (Tuple (Tuple1))): Newlist.append (x) return newlist #2, N-pairing processing of two-dimensional raw data after Cartesian processing, to obtain data def pairwise before get_pairslist calculation (self,lista): Pwlist = [] for i in lista:subtemplist = [] for Sublista in Itertools.combinations (I, 2): Subtempl  Ist.append (Sublista) pwlist.append (subtemplist) return pwlist #3, perform pirwise algorithm calculation def pairwise (SELF,LISTB): Sublistlen = Len (listb[1]) flag = [0]*sublistlen Templistb = Copy.deepcopy (listb) Delmenu = [] holdmenu=[] Self.pprint (LISTB) p Rint ('---' *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], ' equal ... ') Flag[xa] = 1 break Else: # print (sublb, ' = = => ', K[xa], ' No no no no ... ') Flag[xa] = 0 # print (' Subscript%d, sub-element%s double match comparison result flag:%s '% (listb.index (lb), lb,flag)) if 0 not in flag:num = Listb . Index (LB) delmenu.append (num) templistb.remove (lb) # print (' Subscript%d line should be deleted, content =%s, '% (num,lb) ') # print (' Delmenu: ', Delmenu) else:num2 = listb.index (lb) holdmenu.append (num2) # print (' Subscript%d line should be retained, content =%s, '% (num2,lb) ') # print (' Holdmenu= ', Holdmenu) # print (' * * * ' *20) print (' Reserved elements list:%s \ n matches duplicate elements list:%s '% (Holdmenu,delmenu)) return Templistb def pwre    Sult (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 (' Number of final reserved test cases:%d '% (len (finallist))) U2.pprint (finallist)

Code interpretation:

The third for Loop code 39~48 line, mainly vertically to determine whether the element to be detected and the same position elements have the same

The second for Loop code 38~48 line, comparing 22 pairs of a set of test cases, from left to right, respectively, to the same position elements

The first for loop code 37~48 lines, traversing each set of test cases.

The 50th to 58th line of code, judging the 22 pairs of a set of use cases can find the same element from top to bottom in the same position of the other group, then the invalid case is removed from TEMPLISTB and the validity of TEMPLISTB is maintained.

Execution Result:


Line 1: [(' m ', ' W '), (' m ', ' C '), (' W ', ' C ')] <---Second function get_pairslist (Self,lista) processed 22 pairing combination Line 2: [(' m ', ' W '), (' m ', ' E  '), (' W ', ' E ')] <---with the first line to explain lines 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: [(' O ', ' l '), (' O '),  ' E '), (' L ', ' e ')]line: [(' O ', ' I '), (' O ', ' C '), (' I ', ' C ')]line: [(' O ', ' I '), (' O ', ' e '), (' I ', ' e ')]line: [(' P ', ' W '), (' P ', ' C '), (' W ', ' C ')]line: [(' P ', ' W '), (' P ', ' E '), (' W ', ' E ')]line: [(' P ', ' L '), (' P ', ' C '), (' L ', ' C ')]li NE: [(' P ', ' L '), (' P ', ' E '), (' L ', ' e ')]line: [(' P ', ' I '), (' P ', ' C '), (' I ', ' C ')]line: [(' P ', ' I '), (' P ', ' e '),  (' I ', ' E ')] <----with the first line to explain the list of--------------------------------------------------reserved elements: [1, 3, 4, 7, 9, ten,, +] <---- The valid use case matches the list of repeating elements in the array: [0, 2, 5, 6, 8, one, five, six] <----The invalid test case that was rejected in the array the final retention test Case number: 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]

Third, the code core content vernacular interpretation

The pairwise (SELF,LISTB) function contains a 3-layer for loop, first drawing a two-dimensional array:


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 ') ]

A two-dimensional list of Listb, where the lines (pronunciation: hang, two sound. The horizontal row) from top to bottom is the first for loop, and i[0],i[1],i[2 in each line] is the second layer for loop from left to right, and the third for loop element I[x] from the top to the effective group TEMPLISTB the pass position element.

1. i[0] of the nth row is compared to the i[0] element of the other rows of the valid TEMPLISTB (third for), if there is an equal, record an identity such as flag1=true if there is no equivalent record falg1=false;

2, until the second for i[0],i[1],i[2] is compared, will get [FLAG1,FLAG2,FLAG3], all flag=true The behavior is invalid use case

3, the first for traversing all combinations, and finally get the effective templistb preserved

See figure:

End of article

The above is the entire content of the pairwise that you have written, this algorithm takes 3 days altogether:

The first day in determining what exactly this algorithm, read a lot of academic literature, can not understand;

The next day began to write the program, for the nested loop design delay for a long time;

The third day of the process of molding, there are implementation results, found with reference to the article conclusion, and then carefully study the reference article, found out in the pit. Re-overthrow the code follow the correct idea, with 1 hours to complete the final result.

I do the test, is not a professional test development, write code more laborious, really should design accounted for 70%, the code accounted for 30% rationale. If, like the foundation in the near, logic in the chaos, you can only use the time heap.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.