黑盒测试(实践)
三角形问题
有一个程序,接收三个整数输入a、b和c,分别表示三角形的三条边。a、b和c的取值范围都为[1,100]。程序根据输入的三条边判断三角形的类型:等边三角形、等腰三角形、普通三角形和不构成三角形。如果输入的a、b和c不在有效范围,程序会输出一条消息来说明问题,如:边a不在有效范围内。
解题:不同的变量进行边界值分析(对一个变量进行分析时另外两个变量为稳定且安全变量)
输入 |
输入 |
输入 |
输出 |
a |
b |
c |
|
0 |
50 |
50 |
边a不在有效范围内 |
1 |
50 |
50 |
等腰三角形 |
2 |
50 |
50 |
等腰三角形 |
99 |
50 |
50 |
等腰三角形 |
100 |
50 |
50 |
不构成三角形 |
101 |
50 |
50 |
边a不在有效范围内 |
50 |
0 |
50 |
边b不在有效范围内 |
50 |
1 |
50 |
等腰三角形 |
50 |
2 |
50 |
等腰三角形 |
50 |
99 |
50 |
等腰三角形 |
50 |
100 |
50 |
不构成三角形 |
50 |
101 |
50 |
边b不在有效范围内 |
50 |
50 |
0 |
边c不在有效范围内 |
50 |
50 |
1 |
等腰三角形 |
50 |
50 |
2 |
等腰三角形 |
50 |
50 |
99 |
等腰三角形 |
50 |
50 |
100 |
不构成三角形 |
50 |
50 |
101 |
边c不在有效范围内 |
50 |
50 |
50 |
等边三角形 |
【计算题】
佣金问题
:某公司生产机器人及部件,机器人包含3大部件:主控模块、通信模块及执行模块。该公司的代理商负责销售机器人整机和部件;公司要求每个代理商每月最少销售一整套机器人(即三类部件至少各销售一个);受限于公司产能,公司每个月最多给每个代理商提供80个主控模块、90个通信模块以及100个执行模块。每个主控模块售价90元、每个通信模块售价60元、每个执行模块售价50元。到6月末的时候,公司会根据代理商的销售情况计算佣金。
佣金计算方法如下:
没有销售额在1000元以下(含)的部分,佣金为10%;
超过1000元但不超过2400元(含)的部分,佣金为15%;
超过2400的部分,佣金为20%。
佣金计算函数:
import java.util.Scanner;
public class test {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while(scanner.hasNext()){
int x = scanner.nextInt();
double m =0;
if(x<=1000){
m = x*0.1;
}else if(x<=2400){
m = (x-1000)*0.15+100;
}else{
m = (x-2400)*0.2+310;
}
System.out.println(m);
}
}
}
|
主控模块 |
通信模块 |
执行模块 |
最多 |
80 |
90 |
100 |
价格 |
90 |
60 |
50 |
- 边界分析
按照边界取值方法
模块 |
正常取值 |
边界值选取 |
主控模块 |
[1,80] |
{0,1,2,40,79,80,81} |
通信模块 |
[1,90] |
{-1,0,1,45,89,90,91} |
执行模块 |
[1,100] |
{-1,0,1,50,99,100,101} |
输入进行考虑
测试用例 |
主控模块 |
通信模块 |
执行模块 |
总销售额 |
预期输出 |
test01 |
0 |
45 |
50 |
5200 |
主控模块输入非法 |
test02 |
1 |
45 |
50 |
5290 |
佣金:888 |
test03 |
2 |
45 |
50 |
5380 |
佣金:906 |
test04 |
79 |
45 |
50 |
12310 |
佣金:2292 |
test05 |
80 |
45 |
50 |
12400 |
佣金:2310 |
test06 |
81 |
45 |
50 |
12490 |
主控模块输入非法 |
test07 |
40 |
0 |
50 |
6100 |
通信模块输入非法 |
test08 |
40 |
1 |
50 |
6160 |
佣金:1062 |
test09 |
40 |
2 |
50 |
6220 |
佣金:1074 |
test10 |
40 |
89 |
50 |
11440 |
佣金:2118 |
test11 |
40 |
90 |
50 |
11500 |
佣金:2130 |
test12 |
40 |
91 |
50 |
11560 |
通信模块输入非法 |
test13 |
40 |
45 |
0 |
6300 |
执行模块输入非法 |
test14 |
40 |
45 |
1 |
6350 |
佣金:1100 |
test15 |
40 |
45 |
2 |
6400 |
佣金:1110 |
test16 |
40 |
45 |
99 |
11250 |
佣金:2080 |
test17 |
40 |
45 |
100 |
11300 |
佣金:2090 |
test18 |
40 |
45 |
101 |
11350 |
执行模块输入非法 |
test19 |
40 |
45 |
50 |
8800 |
佣金:1590 |
- 次边界分析
从输出角度对改程序进行测试,因为代理商每月最低售出主控模块1块、通信模块1块、执行模块1块.其销售额为200,佣金为20元.
最多售出主控模块80块、通信模块90块、执行模块100块,其销售额为17600元,佣金为3350元.
销售额等价类划分为[200,1000],(1000,2400],(2400,17600]
按照此等价类分别取边界值为
{
略小于200,200,略大于200,
略小于1000,1000,略大于1000,
略小于2400,2400,略大于2400,
略小于17600,17600,略大约17600
}
测试用例 |
主控模块 |
通信模块 |
执行模块 |
总销售额 |
预期输出 |
test01 |
1 |
1 |
0 |
150 |
执行模块输入非法 |
test02 |
1 |
1 |
1 |
200 |
佣金:20 |
test03 |
1 |
1 |
2 |
250 |
佣金:25 |
test04 |
5 |
5 |
4 |
950 |
佣金:95 |
test05 |
5 |
5 |
5 |
1000 |
佣金:100 |
test06 |
5 |
5 |
6 |
1050 |
佣金:107.5 |
test07 |
12 |
12 |
11 |
2350 |
佣金:302.5 |
test08 |
12 |
12 |
12 |
2400 |
佣金:310 |
test09 |
12 |
12 |
13 |
2450 |
佣金:320 |
test10 |
80 |
90 |
99 |
17550 |
佣金:3340 |
test11 |
80 |
90 |
100 |
17600 |
佣金:3350 |
test12 |
80 |
90 |
101 |
17650 |
执行模块输入非法 |
酒水佣金问题
某酒水销售公司指派销售员销售各种酒水,其中白酒卖168元/瓶,红酒卖120元/瓶,啤酒卖5元/瓶。对于每个销售员,白酒每月的最高供应量为5000瓶,红酒为3000瓶,啤酒为30000瓶,各销售员每月至少需售出白酒50瓶,红酒30瓶,啤酒300瓶。奖金计算方法如下:
销售额2万元以下(含)的为4%;
销售额2万元(不含)到4.5万元(含)的为1%;
销售额4.5万元以上(不含)的为0.5%。
(请运用边界值法设计测试用例)
边界值分析
- 确定边界
|
白酒 |
红酒 |
啤酒 |
最少 |
50 |
30 |
300 |
最多 |
5000 |
3000 |
30000 |
售价 |
168 |
120 |
5 |
2)确定边界值
白酒{49,50,51,2525,4999,5000,5001}
红酒{29,30,31,1515,2999,3000,3001}
啤酒{299,300,301,15150,29999,30000,30001}
测试用例 |
白酒 |
红酒 |
啤酒 |
销售额 |
预期输出 |
test01 |
49 |
1515 |
15150 |
265782 |
白酒值输入非法 |
test02 |
50 |
1515 |
15150 |
265950 |
佣金4404.75 |
test03 |
51 |
1515 |
15150 |
266118 |
佣金4405.59 |
test04 |
4999 |
1515 |
15150 |
1097382 |
佣金8561.91 |
test05 |
5000 |
1515 |
15150 |
1097550 |
佣金8562.75 |
test06 |
5001 |
1515 |
15150 |
1097718 |
白酒值输入非法 |
test07 |
2525 |
29 |
15150 |
503430 |
红酒值输入非法 |
test08 |
2525 |
30 |
15150 |
503550 |
佣金5592.75 |
test09 |
2525 |
31 |
15150 |
503670 |
佣金5593.35 |
test10 |
2525 |
2999 |
15150 |
859830 |
佣金7374.15 |
test11 |
2525 |
3000 |
15150 |
859950 |
佣金7374.75 |
test12 |
2525 |
3001 |
15150 |
860070 |
红酒值输入非法 |
test13 |
2525 |
1515 |
299 |
607495 |
啤酒值输入非法 |
test14 |
2525 |
1515 |
300 |
607500 |
佣金6112.5 |
test15 |
2525 |
1515 |
301 |
607505 |
佣金6112.525 |
test16 |
2525 |
1515 |
29999 |
755995 |
佣金6854.975 |
test17 |
2525 |
1515 |
30000 |
756000 |
佣金6855 |
test18 |
2525 |
1515 |
30001 |
756005 |
啤酒值输入非法 |
test19 |
2525 |
1515 |
15150 |
681750 |
佣金6483.75 |
考虑次边界
通过输出考虑
销售额可选取边界{1.35,2,4.5,135}
因此按照等价类可选取边界
{
略小于1.35,1.35,略大于1.35,1.675
略小于2,2,略大于2,3.25
略小于4.5,4.5,略大于4.5,69.75
略小于135,135,略大于135,
}
测试用例 |
白酒 |
红酒 |
啤酒 |
销售额 |
预期输出 |
test01 |
50 |
30 |
299 |
13495 |
啤酒输入非法 |
test02 |
50 |
30 |
300 |
13500 |
佣金:540 |
test03 |
50 |
30 |
301 |
13505 |
佣金:540.2 |
test04 |
50 |
30 |
3334 |
16750 |
佣金:3553.75 |
test05 |
50 |
30 |
1599 |
19995 |
佣金:799.8 |
test06 |
50 |
30 |
1600 |
20000 |
佣金:800 |
test07 |
50 |
30 |
1601 |
20005 |
佣金:800.05 |
test08 |
50 |
30 |
6484 |
32500 |
佣金:1044.2 |
test09 |
50 |
30 |
6599 |
44995 |
佣金:1049.95 |
test10 |
50 |
30 |
6600 |
45000 |
佣金:1050 |
test11 |
50 |
30 |
6601 |
45005 |
佣金:3300.025 |
test12 |
1115 |
3000 |
29813 |
697500 |
佣金:6557.765 |
test13 |
5000 |
3000 |
29999 |
1349995 |
佣金:9824.975 |
test14 |
5000 |
3000 |
30000 |
1350000 |
佣金:9825 |
test15 |
5000 |
3000 |
30001 |
1350005 |
啤酒输入非法 |
佣金计算代码(快速得到实验数据):
public class test {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
int x = scanner.nextInt();
double m = 0;
if (x <= 20000) {
m = x * 0.04;
} else if (x <= 45000) {
m = (x - 20000) * 0.01 + 20000 * 0.04;
} else { m = (x-45000)*0.005+25000*0.01+20000*0.04; }
System.out.println(m);
}
}
}
日期检查模块
设有一个档案管理系统,要求用户输入以年月表示的日期。假设日期限定在1990年1月~2049年12月,并规定日期由6位数字字符组成,前4位表示年,后2位表示月。现用等价类划分法设计测试用例,来测试程序的"日期检查功能"。(请运用等价类法设计测试用例)
通过等价类划分标准,进行划分等价类
为每个等价类设计测试用例
测试用例 |
实验数据 |
覆盖情况 |
1 |
200012 |
覆盖有效等价类①⑤⑧ |
2 |
sasd12 |
覆盖无效等价类② |
3 |
12312433 |
覆盖无效等价类③ |
4 |
21343 |
覆盖无效等价类④ |
5 |
122210 |
覆盖无效等价类⑥ |
6 |
299910 |
覆盖无效等价类⑦ |
7 |
200000 |
覆盖无效等价类⑨ |
8 |
200018 |
覆盖无效等价类⑩ |
下一天日期问题
NextDate是一个接受年(year)、月(month)和日(day)三个输入变量的函数,程序输出所输入日期后面一天的日期。其中,年、月和日的取值满足如下条件:
(1)1896<=year<=2096
(2)1<=month<=12
(3)1<=day<=31
将输入不合法情况统一给出消息提示“输入不在有效范围”
1.列出所有的条件桩和动作桩
-
条件桩
Y1={年份:平年}
Y2={年份:闰年}
M1={月份:30天/月}
M2={月份:31天/月}
M3={月份:12月}
M4={月份:2月}
D1={日期:1~27日}
D2={日期:28日}
D3={日期:29日}
D4={日期:30日}
D5={日期:31日}
条件桩C1取{Y1,Y2}中其一,C2取{M1,M2,M3,M4}中其一,C3取{ D1, D2, D3, D4, D5}中其一
-
动作桩:
A1:不可能
A2:日期加一
A3:日期置一
A4:月份加一
A5:月份置一
A6:年份加一
2.确定规则的个数:
除了28,29两种,条件为该项的个数:
所以总的规则项数为4X5+2
3.填入条件项和动作项,形成初始决策表并简化,得到决策表
4.由决策表设计测试用例:
实验代码
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.Arrays;
import java.util.List;
public class test {
public static void main(String[] args) {
Day day = new Day();
// System.out.println(testx.getNextDay(2001, 4, 12));
File file = new File("test.txt");
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
// 获取日期信息
String[] times = line.split(" ");
int yearx = Integer.parseInt(times[0]);
int monthx = Integer.parseInt(times[1]);
int dayx = Integer.parseInt(times[2]);
String nextDay = day.getNextDay(yearx, monthx, dayx);
System.out.println(nextDay);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
} finally {
try {
if (br != null) {
br.close();
}
} catch (Exception e) {
// TODO: handle exception
}
}
}
}
class Day{
public int today_Y;
public int today_M;
public int today_D;
public int MaxMonth;
public List<Integer> Day31Monther;
public List<Integer> Day30Monther;
public void init() {
Integer[] arr31 = { 1, 3, 5, 7, 8, 10, 12 };
Integer[] arr30 = { 4, 6, 9, 11 };
Day31Monther = Arrays.asList(arr31);
Day30Monther = Arrays.asList(arr30);
}
public void setTest(int day, int month, int year) {
init();
this.today_D = day;
this.today_M = month;
this.today_Y = year;
this.MaxMonth = setMaxMonthDay();
}
// 对于输入的数据进行判断,本年,本月,的天数
public int setMaxMonthDay() {
Integer month = new Integer(today_M);
if (Day31Monther.contains(month)) {
// 如果是三十一天的
return 31;
} else if (Day30Monther.contains(month)) {
return 30;
} else if (today_M == 2) {
if (today_Y % 400 == 0 || (today_Y % 100 != 0 && today_Y % 4 == 0)) {
return 29;
} else
return 28;
}
return -1;
}
public boolean dataTrue() {
boolean b = true;
if (today_D > MaxMonth) {
b = false;
}
return b;
}
public String getNextDay(int in_year, int in_month, int in_day) {
setTest(in_day, in_month, in_year);
// System.out.println(today_D+":"+today_M+":"+today_Y+":"+MaxMonth);
String info;
if (!dataTrue()) {
info = "输入不在有效范围";
return info;
}
int day = today_D;
int month = today_M;
int year = today_Y;
if (day == MaxMonth) {
if (month == 12) {
day = 1;
month = 1;
year += 1;
} else {
day = 1;
month += 1;
}
} else {
day += 1;
}
// day = (day + 1) % (MaxMonth+1);
// month = (month + (day + 1) / MaxMonth) % 12;
// year = year + ((month + (day + 1) / MaxMonth) / 12);
info = year + "年"+ month+ "月"+ day +"日";
return info;
}
}
售票问题:
某游乐场售票系统提供三种游戏:“过山车”、“摩天轮”和“海盗船”,门票均为10元。现设计一个自动售票系统,只接受10元、20元的纸币。
(1) 若投入的是10元纸币,并按下“过山车”、“摩天轮”或“海盗船”按钮,就会送出相应游戏的门票。
(2) 若投入的是20元纸币,并在送出相应门票的同时会找还10元纸币(假设不存在没有零钱找的情况)。
-
条件桩:
A1={投入金额10},
A2={投入金额20},
C1={选择1(过山车)}
C2={选择2(摩天轮)}
C3={选择3(海盗船)}
-
动作桩:
D1={是否找钱},
P1={出过山车票},
p2={出摩天轮票},
p3={出海盗船票}
-
确定规格个数: 2*3
-
填入条件项和动作项,形成初始决策表并简化,得到决策表
|
test1 |
test2 |
test3 |
test4 |
test5 |
test6 |
投币 |
A1 |
A1 |
A1 |
A2 |
A2 |
A2 |
按键 |
C1 |
C2 |
C3 |
C1 |
C2 |
C3 |
D1:找10元 |
|
|
|
√ |
√ |
√ |
P1:出过山车票 |
√ |
|
|
√ |
|
|
P2:出摩天轮票 |
|
√ |
|
|
√ |
|
P3:出海盗船票 |
|
|
√ |
|
|
√ |
- 由决策表设计测试用例:
用例编号 |
投币 |
按键 |
预期输出 |
1 |
10 |
1 |
出过山车票 |
2 |
10 |
2 |
出摩天轮票 |
3 |
10 |
3 |
出海盗船票 |
4 |
20 |
1 |
找10元 出过山车票 |
5 |
20 |
2 |
找10元 出摩天轮票 |
6 |
20 |
3 |
找10元 出海盗船票 |
实验代码
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.Arrays;
import java.util.List;
public class test {
public static void main(String[] args) {
Day day = new Day();
// System.out.println(testx.getNextDay(2001, 4, 12));
File file = new File("test.txt");
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
// 获取日期信息
String[] times = line.split(" ");
int yearx = Integer.parseInt(times[0]);
int monthx = Integer.parseInt(times[1]);
int dayx = Integer.parseInt(times[2]);
String nextDay = day.getNextDay(yearx, monthx, dayx);
System.out.println(nextDay);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
} finally {
try {
if (br != null) {
br.close();
}
} catch (Exception e) {
// TODO: handle exception
}
}
}
}
class Day{
public int today_Y;
public int today_M;
public int today_D;
public int MaxMonth;
public List<Integer> Day31Monther;
public List<Integer> Day30Monther;
public void init() {
Integer[] arr31 = { 1, 3, 5, 7, 8, 10, 12 };
Integer[] arr30 = { 4, 6, 9, 11 };
Day31Monther = Arrays.asList(arr31);
Day30Monther = Arrays.asList(arr30);
}
public void setTest(int day, int month, int year) {
init();
this.today_D = day;
this.today_M = month;
this.today_Y = year;
this.MaxMonth = setMaxMonthDay();
}
// 对于输入的数据进行判断,本年,本月,的天数
public int setMaxMonthDay() {
Integer month = new Integer(today_M);
if (Day31Monther.contains(month)) {
// 如果是三十一天的
return 31;
} else if (Day30Monther.contains(month)) {
return 30;
} else if (today_M == 2) {
if (today_Y % 400 == 0 || (today_Y % 100 != 0 && today_Y % 4 == 0)) {
return 29;
} else
return 28;
}
return -1;
}
public boolean dataTrue() {
boolean b = true;
if (today_D > MaxMonth) {
b = false;
}
return b;
}
public String getNextDay(int in_year, int in_month, int in_day) {
setTest(in_day, in_month, in_year);
// System.out.println(today_D+":"+today_M+":"+today_Y+":"+MaxMonth);
String info;
if (!dataTrue()) {
info = "输入不在有效范围";
return info;
}
int day = today_D;
int month = today_M;
int year = today_Y;
if (day == MaxMonth) {
if (month == 12) {
day = 1;
month = 1;
year += 1;
} else {
day = 1;
month += 1;
}
} else {
day += 1;
}
// day = (day + 1) % (MaxMonth+1);
// month = (month + (day + 1) / MaxMonth) % 12;
// year = year + ((month + (day + 1) / MaxMonth) / 12);
info = year + "年"+ month+ "月"+ day +"日";
return info;
}
}
隔一日问题:
程序有三个输入变量month、day、year分别作为输入日期的月份、日、年份,通过程序可以输出该输入日期在日历上隔一天的日期。例如:输入为2020年3月21日,则该程序的输出为2020年3月23日。
- 列出所有的条件桩和动作桩
-
条件桩
Y1={年份:平年}
Y2={年份:闰年}
M1={月份:30天/月}
M2={月份:31天/月}
M3={月份:12月}
M4={月份:2月}
D1={日期:1~26日}
D2={日期:27日}
D3={日期:28日}
D4={日期:29日}
D5={日期:30日}
D6={日期:31日}
条件桩C1取{Y1,Y2}中其一,C2取{M1,M2,M3,M4}中其一,C3取{ D1, D2, D3, D4, D5,D6}中其一
-
动作桩:
A1:不可能
A2:日期加二
A3:日期置一
A4:日期置二
A5:月份加一
A6:月份置一
A7:年份加一
- 确定规则个数
3*6+1*6*2
- 填入条件项和动作项,形成初始决策表并简化,得到决策表
- 由决策表设计测试用例:
实验代码:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.Arrays;
import java.util.List;
public class test03 {
public static void main(String[] args) {
// System.out.println(testx.getNextDay(2001, 4, 12));
// 测试数据在下面,自行建立文档,只是为了更好的得到实验数据
File file = new File("test.txt");
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(file));
String line;
GetNextDay kit = new GetNextDay();
while ((line = br.readLine()) != null) {
System.out.println(line);
// 获得时间
String[] times = line.split(" ");
int yearx = Integer.parseInt(times[0]);
int monthx = Integer.parseInt(times[1]);
int dayx = Integer.parseInt(times[2]);
String nextDay = kit.getNextDayS(yearx, monthx, dayx);
System.out.println(nextDay);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
} finally {
try {
if (br != null) {
br.close();
}
} catch (Exception e) {
// TODO: handle exception
}
}
}
}
class GetNextDay {
public int today_Y;
public int today_M;
public int today_D;
public int MaxMonth;
public List<Integer> Day31Monther;
public List<Integer> Day30Monther;
public void init() {
Integer[] arr31 = { 1, 3, 5, 7, 8, 10, 12 };
Integer[] arr30 = { 4, 6, 9, 11 };
Day31Monther = Arrays.asList(arr31);
Day30Monther = Arrays.asList(arr30);
}
private void setTest(int day, int month, int year) {
init();
this.today_D = day;
this.today_M = month;
this.today_Y = year;
this.MaxMonth = setMaxMonthDay();
}
// 找到本月最大天数
public int setMaxMonthDay() {
Integer month = new Integer(today_M);
if (Day31Monther.contains(month)) {
return 31;
} else if (Day30Monther.contains(month)) {
return 30;
} else if (today_M == 2) {
if (today_Y % 400 == 0 || (today_Y % 100 != 0 && today_Y % 4 == 0)) {
return 29;
} else
return 28;
}
return -1;
}
public boolean dataTrue() {
boolean b = true;
if (today_D > MaxMonth) {
b = false;
}
return b;
}
public String getNextDayS(int in_year, int in_month, int in_day) {
setTest(in_day, in_month, in_year);
String info;
if (!dataTrue()) {
info = "输入错误";
return info;
}
int day = today_D;
int month = today_M;
int year = today_Y;
day +=2;
if(day>MaxMonth){
day++;
day = day%(MaxMonth+1);
month++;
if(month>12){
month=1;
year++;
}
}
info = year + "年" + month + "月" + day + "日";
return info;
}
}
测试用例:
2001 4 12
2001 4 27
2001 4 28
2001 4 29
2001 4 30
2001 4 31
2001 5 12
2001 5 27
2001 5 28
2001 5 29
2001 5 30
2001 5 31
2001 12 12
2001 12 27
2001 12 28
2001 12 29
2001 12 30
2001 12 31
2001 2 12
2001 2 27
2001 2 28
2001 2 29
2004 2 27
2004 2 28
2004 2 29
2001 2 30
2001 2 31
自动售票机问题:
有一个处理单价为5角钱的饮料的自动售货机软件测试用例的设计。
其规格说明如下:
(1)若投入5角钱的硬币,押下【橙汁】或【啤酒】的按钮,则相应的饮料就送出来。
(2)若投入1元钱的硬币,此时售货机没有零钱找,则一个显示【零钱找完】的红灯亮,这时在押下【橙汁】或【啤酒】按钮后,饮料不送出来而且1元硬币也退出来。
(3)若投入1元钱的硬币,此时售货机有零钱找,则显示【零钱找完】的红灯灭,这时在押下【橙汁】或【啤酒】按钮后,会送出饮料的同时退还5角硬币。
-
确定条件桩与条件项
-
条件桩:
T1={投5角}
T2={投1元}
B1={按【橙汁】按钮}
B2={按【啤酒】按钮}
H1={有零钱}
H2={无零钱}
T取{T1,T2}其中一项
B取{B1,B2}其中一项
H取{H1,H2}其中一项
-
条件项
A1={出橙汁}
A2={出啤酒}
A3={红灯亮}
A4={找零钱}
A5={无响应}
-
确定组合可能
其中H条件只会出现在T2条件下,故总可能为2+2*2 六种可能
-
填入条件项和动作项,形成初始决策表并简化,得到决策表
其中test5和test6中B为无关项,所以可以合并
- 由决策表设计测试用例:
设置键
T1=1
T2=2
B1=3
B2=4
H1=5
H2=6
实验代码:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
public class test04 {
public static void main(String[] args) {
// 只为了得到数据,可以自行修改输入流
File f = new File("text04.txt");
BufferedReader br = null;
DoAction doAction = new DoAction();
try {
br = new BufferedReader(new FileReader(f));
String line;
String result;
while ((line = br.readLine()) != null) {
String[] choose = line.split(" ");
//System.out.println(line);
result = doAction.getResult(Integer.parseInt(
choose[0]), Integer.parseInt(choose[1]), Integer.parseInt(choose[2]));
System.out.println(result);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
} finally {
try {
if (br != null) {
br.close();
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
}
class DoAction {
private final int T1 = 1; // 投5
private final int T2 = 2; // 投1元
private final int B1 = 3; // 按橙汁
private final int B2 = 4; // 按啤酒
private final int H1 = 5; // 有零钱
private final int H2 = 6; // 无零钱
private final String[] responseS = { "出橙汁","出啤酒","找零钱","无响应"} ;
public String getResult(int T, int B, int H) {
StringBuffer info = new StringBuffer("");
if (H == H2 && T == T2) {
return responseS[3];
}
if (B == B1) {
info.append(responseS[0]);
} else {
info.append(responseS[1]);
}
if (T == T2) {
info.append(" " + responseS[2]);
}
return info.toString();
}
}
实验数据:
1 3 0
1 4 0
2 3 5
2 4 5
2 3 6