search
HomeBackend DevelopmentPython TutorialHow Python implements organization algorithm pairwise (efficient test cases)

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 number

The 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: [(&#39;M&#39;, &#39;W&#39;), (&#39;M&#39;, &#39;C&#39;), (&#39;W&#39;, &#39;C&#39;)]  <---第二个函数get_pairslist(self,lista)处理后的两两配对组合
line 2: [(&#39;M&#39;, &#39;W&#39;), (&#39;M&#39;, &#39;E&#39;), (&#39;W&#39;, &#39;E&#39;)]  <---同第一行解释
line 3: [(&#39;M&#39;, &#39;L&#39;), (&#39;M&#39;, &#39;C&#39;), (&#39;L&#39;, &#39;C&#39;)]
line 4: [(&#39;M&#39;, &#39;L&#39;), (&#39;M&#39;, &#39;E&#39;), (&#39;L&#39;, &#39;E&#39;)]
line 5: [(&#39;M&#39;, &#39;I&#39;), (&#39;M&#39;, &#39;C&#39;), (&#39;I&#39;, &#39;C&#39;)]
line 6: [(&#39;M&#39;, &#39;I&#39;), (&#39;M&#39;, &#39;E&#39;), (&#39;I&#39;, &#39;E&#39;)]
line 7: [(&#39;O&#39;, &#39;W&#39;), (&#39;O&#39;, &#39;C&#39;), (&#39;W&#39;, &#39;C&#39;)]
line 8: [(&#39;O&#39;, &#39;W&#39;), (&#39;O&#39;, &#39;E&#39;), (&#39;W&#39;, &#39;E&#39;)]
line 9: [(&#39;O&#39;, &#39;L&#39;), (&#39;O&#39;, &#39;C&#39;), (&#39;L&#39;, &#39;C&#39;)]
line 10: [(&#39;O&#39;, &#39;L&#39;), (&#39;O&#39;, &#39;E&#39;), (&#39;L&#39;, &#39;E&#39;)]
line 11: [(&#39;O&#39;, &#39;I&#39;), (&#39;O&#39;, &#39;C&#39;), (&#39;I&#39;, &#39;C&#39;)]
line 12: [(&#39;O&#39;, &#39;I&#39;), (&#39;O&#39;, &#39;E&#39;), (&#39;I&#39;, &#39;E&#39;)]
line 13: [(&#39;P&#39;, &#39;W&#39;), (&#39;P&#39;, &#39;C&#39;), (&#39;W&#39;, &#39;C&#39;)]
line 14: [(&#39;P&#39;, &#39;W&#39;), (&#39;P&#39;, &#39;E&#39;), (&#39;W&#39;, &#39;E&#39;)]
line 15: [(&#39;P&#39;, &#39;L&#39;), (&#39;P&#39;, &#39;C&#39;), (&#39;L&#39;, &#39;C&#39;)]
line 16: [(&#39;P&#39;, &#39;L&#39;), (&#39;P&#39;, &#39;E&#39;), (&#39;L&#39;, &#39;E&#39;)]
line 17: [(&#39;P&#39;, &#39;I&#39;), (&#39;P&#39;, &#39;C&#39;), (&#39;I&#39;, &#39;C&#39;)]
line 18: [(&#39;P&#39;, &#39;I&#39;), (&#39;P&#39;, &#39;E&#39;), (&#39;I&#39;, &#39;E&#39;)]  <----同第一行解释
--------------------------------------------------
保留元素列表:[1, 3, 4, 7, 9, 10, 12, 14, 17]  <----有效用例在数组中下标
匹配重复元素列表:[0, 2, 5, 6, 8, 11, 13, 15, 16]  <----被剔除的无效测试用例在数组中下标
最终保留测试用例个数:9 个
line 1: [(&#39;M&#39;, &#39;W&#39;), (&#39;M&#39;, &#39;E&#39;), (&#39;W&#39;, &#39;E&#39;)]
line 2: [(&#39;M&#39;, &#39;L&#39;), (&#39;M&#39;, &#39;E&#39;), (&#39;L&#39;, &#39;E&#39;)]
line 3: [(&#39;M&#39;, &#39;I&#39;), (&#39;M&#39;, &#39;C&#39;), (&#39;I&#39;, &#39;C&#39;)]
line 4: [(&#39;O&#39;, &#39;W&#39;), (&#39;O&#39;, &#39;E&#39;), (&#39;W&#39;, &#39;E&#39;)]
line 5: [(&#39;O&#39;, &#39;L&#39;), (&#39;O&#39;, &#39;E&#39;), (&#39;L&#39;, &#39;E&#39;)]
line 6: [(&#39;O&#39;, &#39;I&#39;), (&#39;O&#39;, &#39;C&#39;), (&#39;I&#39;, &#39;C&#39;)]
line 7: [(&#39;P&#39;, &#39;W&#39;), (&#39;P&#39;, &#39;C&#39;), (&#39;W&#39;, &#39;C&#39;)]
line 8: [(&#39;P&#39;, &#39;L&#39;), (&#39;P&#39;, &#39;C&#39;), (&#39;L&#39;, &#39;C&#39;)]
line 9: [(&#39;P&#39;, &#39;I&#39;), (&#39;P&#39;, &#39;E&#39;), (&#39;I&#39;, &#39;E&#39;)]
[Finished in 0.2s]

三、代码核心内容白话解释

pairwise(self,listb)函数包含3层for循环,先画一个二维数组:


i[0]  i[1]  i[2]
listb.index(i)=0 : [(&#39;M&#39;, &#39;W&#39;), (&#39;M&#39;, &#39;C&#39;), (&#39;W&#39;, &#39;C&#39;)]
listb.index(i)=1 : [(&#39;M&#39;, &#39;W&#39;), (&#39;M&#39;, &#39;E&#39;), (&#39;W&#39;, &#39;E&#39;)]
listb.index(i) : [(&#39;M&#39;, &#39;L&#39;), (&#39;M&#39;, &#39;C&#39;), (&#39;L&#39;, &#39;C&#39;)]
listb.index(i) : [(&#39;M&#39;, &#39;L&#39;), (&#39;M&#39;, &#39;E&#39;), (&#39;L&#39;, &#39;E&#39;)]
listb.index(i) : [(&#39;M&#39;, &#39;I&#39;), (&#39;M&#39;, &#39;C&#39;), (&#39;I&#39;, &#39;C&#39;)]
listb.index(i) : [(&#39;M&#39;, &#39;I&#39;), (&#39;M&#39;, &#39;E&#39;), (&#39;I&#39;, &#39;E&#39;)]
listb.index(i) : [(&#39;O&#39;, &#39;W&#39;), (&#39;O&#39;, &#39;E&#39;), (&#39;W&#39;, &#39;E&#39;)]
listb.index(i) : [(&#39;O&#39;, &#39;L&#39;), (&#39;O&#39;, &#39;C&#39;), (&#39;L&#39;, &#39;C&#39;)]
listb.index(i) : [(&#39;O&#39;, &#39;L&#39;), (&#39;O&#39;, &#39;E&#39;), (&#39;L&#39;, &#39;E&#39;)]
listb.index(i) : [(&#39;O&#39;, &#39;I&#39;), (&#39;O&#39;, &#39;C&#39;), (&#39;I&#39;, &#39;C&#39;)]
listb.index(i)=n : [(&#39;O&#39;, &#39;I&#39;), (&#39;O&#39;, &#39;E&#39;), (&#39;I&#39;, &#39;E&#39;)]

二维列表 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!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Merging Lists in Python: Choosing the Right MethodMerging Lists in Python: Choosing the Right MethodMay 14, 2025 am 12:11 AM

TomergelistsinPython,youcanusethe operator,extendmethod,listcomprehension,oritertools.chain,eachwithspecificadvantages:1)The operatorissimplebutlessefficientforlargelists;2)extendismemory-efficientbutmodifiestheoriginallist;3)listcomprehensionoffersf

How to concatenate two lists in python 3?How to concatenate two lists in python 3?May 14, 2025 am 12:09 AM

In Python 3, two lists can be connected through a variety of methods: 1) Use operator, which is suitable for small lists, but is inefficient for large lists; 2) Use extend method, which is suitable for large lists, with high memory efficiency, but will modify the original list; 3) Use * operator, which is suitable for merging multiple lists, without modifying the original list; 4) Use itertools.chain, which is suitable for large data sets, with high memory efficiency.

Python concatenate list stringsPython concatenate list stringsMay 14, 2025 am 12:08 AM

Using the join() method is the most efficient way to connect strings from lists in Python. 1) Use the join() method to be efficient and easy to read. 2) The cycle uses operators inefficiently for large lists. 3) The combination of list comprehension and join() is suitable for scenarios that require conversion. 4) The reduce() method is suitable for other types of reductions, but is inefficient for string concatenation. The complete sentence ends.

Python execution, what is that?Python execution, what is that?May 14, 2025 am 12:06 AM

PythonexecutionistheprocessoftransformingPythoncodeintoexecutableinstructions.1)Theinterpreterreadsthecode,convertingitintobytecode,whichthePythonVirtualMachine(PVM)executes.2)TheGlobalInterpreterLock(GIL)managesthreadexecution,potentiallylimitingmul

Python: what are the key featuresPython: what are the key featuresMay 14, 2025 am 12:02 AM

Key features of Python include: 1. The syntax is concise and easy to understand, suitable for beginners; 2. Dynamic type system, improving development speed; 3. Rich standard library, supporting multiple tasks; 4. Strong community and ecosystem, providing extensive support; 5. Interpretation, suitable for scripting and rapid prototyping; 6. Multi-paradigm support, suitable for various programming styles.

Python: compiler or Interpreter?Python: compiler or Interpreter?May 13, 2025 am 12:10 AM

Python is an interpreted language, but it also includes the compilation process. 1) Python code is first compiled into bytecode. 2) Bytecode is interpreted and executed by Python virtual machine. 3) This hybrid mechanism makes Python both flexible and efficient, but not as fast as a fully compiled language.

Python For Loop vs While Loop: When to Use Which?Python For Loop vs While Loop: When to Use Which?May 13, 2025 am 12:07 AM

Useaforloopwheniteratingoverasequenceorforaspecificnumberoftimes;useawhileloopwhencontinuinguntilaconditionismet.Forloopsareidealforknownsequences,whilewhileloopssuitsituationswithundeterminediterations.

Python loops: The most common errorsPython loops: The most common errorsMay 13, 2025 am 12:07 AM

Pythonloopscanleadtoerrorslikeinfiniteloops,modifyinglistsduringiteration,off-by-oneerrors,zero-indexingissues,andnestedloopinefficiencies.Toavoidthese:1)Use'i

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools