实验二+158+余超勇
被测代码连接:http://www.cnblogs.com/k511959373/p/6641611.html
一、实验目的
掌握基于覆盖理论与基本路径的基本白盒测试方法和实践
二、实验要求
运用逻辑覆盖测试的覆盖准则设计被测程序的测试用例,并运行测试用例检查程序的正确与否,给出程序缺陷小结。
三、实验内容
根据各位同学自己的被测程序,分别作出各类白盒测试技术的用例设计和相应的Junit脚本。
所有的覆盖的技术:语句覆盖、判定覆盖、条件覆盖、判定/条件覆盖、组合覆盖、路径覆盖,基本路径测试方法。
包括的内容有:
1) 被测原代码
package java_example2; import java.util.Scanner; public class Yongjin01 { public double Input(double number){ double num = 0; Scanner scanner = new Scanner(System.in); try{ if(num<0){ System.out.println("输入的数量不满足要求!"); }else{ num = scanner.nextDouble(); return num; } }catch(Exception e){System.out.println("输入不合法!");} return num; } public double Commission(double headphone,double shell,double protector) { double commission = 0; double headphonePrice = 80; double shellPrice = 10; double protectorPrice = 8; double headphonesales = headphonePrice*headphone; double shellsales = shellPrice*shell; double protectorsales = protectorPrice*protector; double sales = headphonesales+shellsales+protectorsales; if(sales>1800){ commission = 0.10*1000; commission = commission+0.15*800; commission = commission+0.20*(sales-1800); }else if(sales>1000){ commission = 0.10*1000; commission = commission+0.15*(sales-1000); } else commission = 0.10*sales; return commission; } } package java_example2; //第三次作业 public class ruance2 { public static void main(String[] args) { while(true){ Yongjin01 yj = new Yongjin01(); System.out.println("请分别输入三种手机配件的销售情况:"); double headphone = yj.Input(0); double shell = yj.Input(0); double protector = yj.Input(0); System.out.println("耳机数量:" + headphone + "\n手机壳数量:" + shell + " \n手机贴膜数量:" + protector); double commission = yj.Commission(headphone, shell, protector); System.out.println("销售佣金:" + commission); } } }
2)依据覆盖技术,测试用例列表:
DD-路径(只压缩链路经)
程序图节点 |
DD-路径 |
1,2 |
A |
3 |
B |
4 |
C |
5 |
D |
6 |
E |
7 |
F |
8,9 |
G |
10,12 |
H |
11,13 |
I |
14 |
J |
15 |
P |
(I)语句覆盖(条件覆盖):
A-B-D-E,A-B-D-F,A-B-C-G-H-P,A-B-C-G-I-P,A-B-C-G-I-J-P
测试用例:
用例ID |
输入值 |
执行路径 |
预期输出 |
||
a |
b |
c |
X |
||
1 |
-1 |
-1 |
-1 |
A-B-D-E |
输入的数量不满足要求 |
2 |
a |
1 |
1 |
A-B-D-F |
输入不合法 |
3 |
10 |
10 |
10 |
A-B-C-G-H-P |
98.0 |
4 |
10 |
10 |
20 |
A-B-C-G-I-P |
109.0 |
5 |
20 |
20 |
20 |
A-B-C-G-I-J-P |
252.0 |
6 |
1.1 |
1.1 |
1.1 |
A-B-C-G-H-P |
输入的数量不满足要求 |
(II)分支覆盖(判断覆盖):
A-B-D-E(1.右,2.左),
A-B-D-F(1.右,2.右),
A-B-C-G-H-P(1.左,2.左),
A-B-C-G-I-P(1.左,2.右,3.左),
A-B-C-G-I-J-P(1.左,2.右,3.右)
测试用例:
用例ID |
输入值 |
执行路径 |
预期输出 |
||
a |
b |
c |
X |
||
1 |
-1 |
-1 |
-1 |
A-B-D-E |
输入的数量不满足要求 |
2 |
a |
1 |
1 |
A-B-D-F |
输入不合法 |
3 |
10 |
10 |
10 |
A-B-C-G-H-P |
98.0 |
4 |
10 |
10 |
20 |
A-B-C-G-I-P |
109.0 |
5 |
20 |
20 |
20 |
A-B-C-G-I-J-P |
252.0 |
(III)路径覆盖:
A-B-D-E(1.右,2.左),
A-B-D-F(1.右,2.右),
A-B-C-G-H-P(1.左,2.左),
A-B-C-G-I-P(1.左,2.右,3.左),
A-B-C-G-I-J-P(1.左,2.右,3.右)
测试用例:
用例ID |
输入值 |
执行路径 |
预期输出 |
||
a |
b |
c |
X |
||
1 |
-1 |
-1 |
-1 |
A-B-D-E |
输入的数量不满足要求 |
2 |
a |
1 |
1 |
A-B-D-F |
输入不合法 |
3 |
10 |
10 |
10 |
A-B-C-G-H-P |
98.0 |
4 |
10 |
10 |
20 |
A-B-C-G-I-P |
109.0 |
5 |
20 |
20 |
20 |
A-B-C-G-I-J-P |
252.0 |
3)相应Junit测试脚本、执行结果
package java_example2; import static org.junit.Assert.*; import org.junit.After; import org.junit.Before; import org.junit.Test; public class Yongjin01Test { @Before public void setUp() throws Exception { } @After public void tearDown() throws Exception { } @Test public void testInput0() { String d1 = new Yongjin01().Input(1.1)+""; assertEquals("输入的数量不满足要求!", d1); } @Test public void testInput1() { String d1 = new Yongjin01().Input(-1)+""; assertEquals("输入的数量不满足要求!", d1); } @Test public void testInput2() { String d2 = new Yongjin01().Input('a')+""; assertEquals("输入不合法!", d2); } @Test public void testCommission3() { String d3 = new Yongjin01().Commission(10, 10, 10)+""; assertEquals("98.0", d3); } @Test public void testCommission4() { String d4 = new Yongjin01().Commission(10, 10, 20)+""; assertEquals("109.0", d4); } @Test public void testCommission5() { String d5 = new Yongjin01().Commission(20, 20, 20)+""; assertEquals("252.0", d5); } }
4、测试小结:
1.改代码存在缺陷
(1)没有将判断输入的函数的返回值定义为字符串,无法准确的测试出是否出问题。
(2)并且该代码在Commission方法中用float返回,与要求不符。
(3)方法Commission的输入没有限制,导致不符合实际。
(4)即时输入错误依旧输出错误答案,错误错入只是用空字符代替。
2.建议:
(1)不适用System.out输出信息,将信息存入字符串再输出,可以更好地测试。
(2)限制错误输入,输入错误则返回错误提示信息并返回重新输入。
2.本次测试使用了Junit工具,先画出DD路径图再进行测试,使得测试用例的归纳更加方便,防止测试用例的缺少。本次测试使我认识到系统地测试方法才能使测试更加完整。