实验二+099+吴丹丹

被测代码地址:http://www.cnblogs.com/yuyaoc/p/6624589.html

一、实验目的

掌握基于覆盖理论与基本路径的基本白盒测试方法和实践

二、实验要求

运用逻辑覆盖测试的覆盖准则设计被测程序的测试用例,并运行测试用例检查程序的正确与否,给出程序缺陷小结。

三、实验内容

根据各位同学自己的被测程序,分别作出各类白盒测试技术的用例设计和相应的Junit脚本。

所有的覆盖的技术:语句覆盖、判定覆盖、条件覆盖、判定/条件覆盖、组合覆盖、路径覆盖,基本路径测试方法。

包括的内容有:

1) 被测原代码

 1 package demo;
 2  
 3 import java.io.BufferedReader;
 4 import java.io.InputStreamReader;
 5  
 6 public class Commission {
 7      public static final int Pheadphone = 80;
 8      public static final int Pshell = 10;
 9      public static final int Pprotector = 8;
10  
11    private static BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
12      
13     private static int headphone = -1;
14     private static int shell = -1;
15     private static int protector = -1;
16  
17     public static double commission(int headphone,int shell,int protector){
18         double sum = 0;
19         double salary = 0.0;
20         //判断销售额
21         sum = Pheadphone*headphone + Pshell*shell +Pprotector*protector;
22         if(sum<1000)
23             salary = 0.1*Pheadphone*headphone + 0.1*Pshell*shell +0.1*Pprotector*protector;//销售额小于1000时所抽取的佣金
24        else if(sum<1800)
25             salary = 0.15*Pheadphone*headphone + 0.15*Pshell*shell +0.15*Pprotector*protector;//计算销售额1000到1800的佣金
26        else
27             salary = 0.2*Pheadphone*headphone + 0.2*Pshell*shell +0.2*Pprotector*protector;//销售额1800以上的佣金
28        return salary;
29     }
30     private static int[] input(){
31         int a[] = null, i = 0;
32         try {
33             String str = bufferedReader.readLine();
34             String s[] = str.split("\\s+");
35             a = new int[s.length];
36             for(String tmp : s){
37                 a[i++] = Integer.parseInt(tmp);
38             }
39         } catch (Exception e) {
40           a = null;
41         }
42         return a;
43     }
44  
45  
46     public static void main(String[] args) {
47         // TODO 自动生成的方法存根
48         double salary = 0.0;
49         int exit = 1, a[] = null;
50         while(exit == 1){
51            System.out.println("请分别输入三种手机配件的销售情况:");//输入数量并检查输入是否符合要求
52          
53                a = input();
54                if(a == null || a.length != 3){
55                    System.out.println("输入数量不满足要求”,返回重新输入: ");
56                 }else{
57                    headphone = a[0];
58                    shell = a[1];
59                    protector = a[2];
60                 }
61                exit = 2;
62            }
63             if(headphone<0||shell<0||protector<0){
64                 System.out.println("输入数量不满足要求");
65               
66             }
67             salary = commission(headphone,shell,protector);
68             System.out.println("佣金额为:"+salary);
69             headphone = shell = protector = -1;
70             a = null;
71        }
72     }
View Code

2)依据覆盖技术,测试用例列表:

 

DD-路径(只压缩链路经)

程序图节点

DD-路径

17

A

18,19,21,22

B

23

C

24

D

25

E

26,27

F

28

G

(I)语句覆盖:

      A-B-C-G

      A-B-D-E-G

      A-B-D-F-G

测试用例:

用例ID     

输入值

执行路径

预期输出  

headphone

shell  

protector  

salary  

1    

5  

10  

8  

A-B-C-G

56.4  

2    

10 

 20   

14  

A-B-D-E-G

166.8  

3    

20 

28  

23  

A-B-D-F-G

412.8  

(II)分支覆盖(判断覆盖):

       A-B-C-G

      A-B-D-E-G

      A-B-D-F-G

     测试用例:

用例ID     

输入值

执行路径

预期输出  

headphone

shell  

protector  

salary  

1    

8  

 7    

11  

A-B-C-G

79.8  

2    

12 

23   

10  

A-B-D-E-G

190.5  

3    

26 

21   

23  

A-B-D-F-G

494.8  

(III)路径覆盖:

      A-B-C-G

      A-B-D-E-G

      A-B-D-F-G

     测试用例:

用例ID     

输入值

执行路径

预期输出  

headphone

shell  

protector  

salary  

1    

6  

11  

7  

A-B-C-G

 64.6   

2    

15  

 20   

16  

A-B-D-E-G

229.2  

3    

33  

21  

23  

A-B-D-F-G

606.8  


(IV)条件覆盖:

 

编号  

0<=sum<=1000

覆盖路径

1  

T

B-C

2  

F

B-D

 

编号  

1000<sum<=1800

覆盖路径

3  

T

D-E

4  

F

D-F


测试用例:

用例ID     

输入值

执行路径

预期输出  

headphone

shell  

protector  

salary  

1    

7  

18  

7  

A-B-C-G

 79.6   

2    

16  

 16   

16  

 A-B-D-E-G

   235.2   

3    

14  

11  

29  

A-B-D-E-G

         219.3   

4    

37  

20  

23  

 A-B-D-F-G

668.8  


3)相应Junit测试脚本、执行结果
 

 1 import static org.junit.Assert.*;
 2 
 3 import org.junit.Before;
 4 import org.junit.Test;
 5 
 6 public class CommissionTest {
 7     Commission commission = new Commission();
 8     
 9     @Before
10     public void setUp() throws Exception {
11     }
12 
13     @SuppressWarnings("deprecation")
14     
15     //语句覆盖
16     @Test
17     public void testStatement1() {
18         assertEquals(56.4,commission.commission(5, 10, 8),0.0001); //0.0001为结果允许误差
19     }
20     
21     @Test
22     public void testStatement2() {
23         assertEquals(166.8,commission.commission(10, 20, 14),0.0001);
24     }
25     
26     @Test
27     public void testStatement3() {
28         assertEquals(412.8,commission.commission(20, 28, 23),0.0001);
29     }
30     
31     //判断覆盖
32     @Test
33     public void testJudge1() {
34         assertEquals(79.8,commission.commission(8, 7, 11),0.0001);
35     }
36     
37     @Test
38     public void testJudge2() {
39         assertEquals(190.5,commission.commission(12, 23, 10),0.0001);
40     }
41     
42     @Test
43     public void testJudge3() {
44         assertEquals(494.8,commission.commission(26, 21, 23),0.0001);
45     }
46     
47     //路径覆盖
48     @Test
49     public void testPath1() {
50         assertEquals(64.6,commission.commission(6, 11, 7),0.0001);
51     }
52     
53     @Test
54     public void testPath2() {
55         assertEquals(229.2,commission.commission(15, 20, 16),0.0001);
56     }
57     
58     @Test
59     public void testPath3() {
60         assertEquals(606.8,commission.commission(33, 21, 23),0.0001);
61     }
62     
63     //条件覆盖
64     @Test
65     public void testCondition1() {
66         assertEquals(79.6,commission.commission(7, 18, 7),0.0001);
67     }
68     
69     @Test
70     public void testCondition2() {
71         assertEquals(235.2,commission.commission(16, 16, 16),0.0001);
72     }
73     
74     @Test
75     public void testCondition3() {
76         assertEquals(219.3,commission.commission(14, 11, 29),0.0001);
77     }
78     
79     @Test
80     public void testCondition4() {
81         assertEquals(668.8,commission.commission(37, 20, 23),0.0001);
82     }
83 }
View Code

 

4)给出测试参数化和打包测试的脚本,并生成执行结果

 

 1 import static org.junit.Assert.*;
 2 
 3 import java.util.Arrays;
 4 import java.util.Collection;
 5 
 6 import org.junit.Test;
 7 import org.junit.runner.RunWith;
 8 import org.junit.runners.Parameterized;
 9 import org.junit.runners.Parameterized.Parameters;
10 
11 @RunWith(Parameterized.class)
12 public class CommissionParamTest {
13     private int param1;
14     private int param2;
15     private int param3;
16     private double result;
17     
18     Commission commission = new Commission();
19     
20     @Parameters
21     public static Collection data(){
22         return Arrays.asList(new Object[][]{
23             {5, 10, 8,56.4},
24             {12, 23, 10,190.5},
25             {37, 20, 23,668.8},
26             {15, 20, 16,229.2},
27             {14, 11,29,219.3}
28             
29         });
30     }
31     
32     public CommissionParamTest(int param1,int param2,int param3,double result){
33         this.param1 = param1;
34         this.param2 = param2;
35         this.param3 = param3;
36         this.result = result;
37     }
38     
39     @Test
40     public void testParam(){
41         double rs = commission.commission(param1, param2, param3);
42         assertEquals(rs,result,0.001);
43     }
44 }
View Code

打包测试

 1  import org.junit.runner.RunWith;
 2  import org.junit.runners.Suite;
 3  @RunWith(Suite.class)
 4  @Suite.SuiteClasses(
 5           {
 6               CommissionTest.class,
 7               CommissionParamTest.class,
 8           
 9           })
10 public class AllCommitionTest {
11 
12 }
View Code

 

四、测试小结:

1)测试找到的缺陷清单:

  代码通过所有的测试用例,虽然单元测试没有错误,但是整个函数还是出现比较大的错误。循环和输出都没有控制好。

2)对源代码的修改建议:

     建议将main函数里面的判断提取到方法里去,使程序能够更全面的进行测试,不然程序的方法显得过于简单,达不到测试的目的。

3)测试总结与心得体会:

  通过这次实验,对白盒测试有了进一步的了解,对Junit工具的使用更加熟练了一点,希望继续学习!

posted @ 2017-04-13 23:20  Esperer`  阅读(220)  评论(0编辑  收藏  举报