PairWise算法,基本思想是每一个测试用例至少会出现一个新的二元组(我瞎编的名词,懂得起我的意思即可),举个栗子:

image-20210404205926491

上图中A,B,C列表示他们的取值,每一行表示一个测试用例,深色标记的测试用例即为不符合PairWise的用例。以第24个测试用例举例,它有“23”,“24”,“34”共三个二元组,但第21、第16、第12个个测试用例中都已经出现过,所以没有出现新的二元组,不符合PairWise方法。

JAVA实现代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import java.util.HashMap;

/**
* PairWise(成对)测试方法
* author: likeqc
* date: 2021-4-4 11:06:59
*/
public class PairWise {
/**
* PairWise方法
* @param str String[][],二维数组,一维数组 str[i] 中存放第 i 个因素的因子
*/
private static void solution(String[][] str) {
if (str == null) {
return;
}
// 传统方式测试用例数量
int sum = 1;
// 符合要求的测试用例数量
int count = 0;
HashMap hashMap = new HashMap();
for (int i = 0; i < str.length; i++) {
if (str[i].length < 1) {
System.out.println("输出的数据错误!");
return;
}
sum *= str[i].length;
}
int[] one = new int[str.length];
for (int i = 0; i < sum; i++) {
// 创造一个新的测试用例(传统方法,暴力所有可能的测试用例)
int carry = 1;
for (int j = str.length - 1; j >= 0; j--) {
if (i == 0) {
continue;
}
one[j] = (one[j] + carry) % str[j].length;
if (carry == 1 && one[j] == 0) {
carry = 1;
} else {
carry = 0;
}
}
// 测试该测试用例是否能够产生新的配对组
boolean flag = false;
for (int j = 0; j < str.length; j++) {
for (int k = j + 1; k < str.length; k++) {
String key = j + "_" + str[j][one[j]] + "," + k + "_" + str[k][one[k]];
// System.out.println(key);
if (hashMap.get(key) == null) {
flag = true;
// System.out.println("新的key:" + key);
hashMap.put(key, 1);
}
}
}

// 产生了新的配对组,说明该用例符合 PairWise 规则,输出
if (flag) {
count++;
System.out.print("测试用例" + count + ":" + str[0][one[0]]);
for (int j = 1; j < str.length; j++) {
System.out.print("," + str[j][one[j]]);
}
System.out.println();
}
}
System.out.println("PairWise方法测试用用例:" + count);
System.out.println("传统方法测试用用例:" + sum);
}

public static void main(String[] args) {
solution(new String[][]{{"T", "F"}, {"1", "2", "3"}, {"a", "b", "c", "d"}});
}
}