Home Backend Development Python Tutorial How Python implements organization algorithm pairwise (efficient test cases)

How Python implements organization algorithm pairwise (efficient test cases)

Jul 20, 2017 pm 03:37 PM
python algorithm organize

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
&#39;&#39;&#39;
#Author:Kuzaman
#Time:2017-07-18
&#39;&#39;&#39;
class utils2 :
 #1、笛卡尔积 对参数分组全排列
 def product(self,tuple1):
  newlist=[]
  for x in eval(&#39;itertools.product&#39;+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 (&#39;--&#39;*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,&#39;===>&#39; ,k[Xa],&#39;相等了。。。&#39;)
      flag[Xa] = 1
      break
     else:
      # print (sublb,&#39;===>&#39; ,k[Xa],&#39;不不不等了。。。&#39;)
      flag[Xa] = 0
   # print (&#39;下标%d,子元素 %s 双匹配对比结果flag:%s&#39;%(listb.index(lb),lb,flag))
   if 0 not in flag:
    num = listb.index(lb)
    delmenu.append(num)
    templistb.remove(lb)
    # print (&#39;下标为%d行应删除,内容=%s,&#39;%(num,lb))
    # print (&#39;delmenu:&#39;,delmenu)
   else:
    num2 = listb.index(lb)
    holdmenu.append(num2)
    # print (&#39;下标为%d行应保留,内容=%s,&#39;%(num2,lb))
    # print(&#39;holdmenu=&#39;,holdmenu)
   # print (&#39;***&#39;*20)
  print (&#39;保留元素列表:%s \n匹配重复元素列表:%s&#39;%(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 (&#39;line %d:&#39;%(list.index(i)+1),i)  

if __name__ == &#39;__main__&#39;:
 u2 = utils2()
 allparams=[[&#39;M&#39;,&#39;O&#39;,&#39;P&#39;],[&#39;W&#39;,&#39;L&#39;,&#39;I&#39;],[&#39;C&#39;,&#39;E&#39;]]#,&#39;K&#39;],[1,2,3],[&#39;Yes&#39;,&#39;No&#39;]]
 str = u2.product(allparams)
 strpc = u2.get_pairslist(str)
 finallist = u2.pairwise(strpc)
 print(&#39;最终保留测试用例个数:%d 个&#39;%(len(finallist)))
 u2.pprint(finallist)
Copy after login

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]
Copy after login

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

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;)]
Copy after login

二维列表 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 of this Website
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

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 Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Can visual studio code be used in python Can visual studio code be used in python Apr 15, 2025 pm 08:18 PM

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

How to run programs in terminal vscode How to run programs in terminal vscode Apr 15, 2025 pm 06:42 PM

In VS Code, you can run the program in the terminal through the following steps: Prepare the code and open the integrated terminal to ensure that the code directory is consistent with the terminal working directory. Select the run command according to the programming language (such as Python's python your_file_name.py) to check whether it runs successfully and resolve errors. Use the debugger to improve debugging efficiency.

Can vs code run in Windows 8 Can vs code run in Windows 8 Apr 15, 2025 pm 07:24 PM

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

Is the vscode extension malicious? Is the vscode extension malicious? Apr 15, 2025 pm 07:57 PM

VS Code extensions pose malicious risks, such as hiding malicious code, exploiting vulnerabilities, and masturbating as legitimate extensions. Methods to identify malicious extensions include: checking publishers, reading comments, checking code, and installing with caution. Security measures also include: security awareness, good habits, regular updates and antivirus software.

What is vscode What is vscode for? What is vscode What is vscode for? Apr 15, 2025 pm 06:45 PM

VS Code is the full name Visual Studio Code, which is a free and open source cross-platform code editor and development environment developed by Microsoft. It supports a wide range of programming languages ​​and provides syntax highlighting, code automatic completion, code snippets and smart prompts to improve development efficiency. Through a rich extension ecosystem, users can add extensions to specific needs and languages, such as debuggers, code formatting tools, and Git integrations. VS Code also includes an intuitive debugger that helps quickly find and resolve bugs in your code.

Python: Automation, Scripting, and Task Management Python: Automation, Scripting, and Task Management Apr 16, 2025 am 12:14 AM

Python excels in automation, scripting, and task management. 1) Automation: File backup is realized through standard libraries such as os and shutil. 2) Script writing: Use the psutil library to monitor system resources. 3) Task management: Use the schedule library to schedule tasks. Python's ease of use and rich library support makes it the preferred tool in these areas.

Can visual studio code run python Can visual studio code run python Apr 15, 2025 pm 08:00 PM

VS Code not only can run Python, but also provides powerful functions, including: automatically identifying Python files after installing Python extensions, providing functions such as code completion, syntax highlighting, and debugging. Relying on the installed Python environment, extensions act as bridge connection editing and Python environment. The debugging functions include setting breakpoints, step-by-step debugging, viewing variable values, and improving debugging efficiency. The integrated terminal supports running complex commands such as unit testing and package management. Supports extended configuration and enhances features such as code formatting, analysis and version control.

Can vs code run python Can vs code run python Apr 15, 2025 pm 08:21 PM

Yes, VS Code can run Python code. To run Python efficiently in VS Code, complete the following steps: Install the Python interpreter and configure environment variables. Install the Python extension in VS Code. Run Python code in VS Code's terminal via the command line. Use VS Code's debugging capabilities and code formatting to improve development efficiency. Adopt good programming habits and use performance analysis tools to optimize code performance.

See all articles