实验二+116+陈洁

测试帖连接:http://www.cnblogs.com/ZQ4162/p/6623033.html

一、实验目的

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

二、实验要求

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

三、实验内容

1) 被测原代码

 1 public class Caculator {
 2     public static float commission(int Headphone,int Shell,int Protector){
 3         int All = Headphone*80+Shell*10+Protector*8; float commission = 0; 
 4         if(All<1000&&All>=0) 
 5             commission = (float) (All*0.1); System.out.println("本次销售所得佣金为:"+commission);
 6         else if(All>=1000 && All<=1800) 
 7             commission = (float) (100+(All-1000)*0.15);System.out.println("本次销售所得佣金为:"+commission);
 8         else if(All>1800)
 9             commission = (float) (100+800*0.15+(All-1800)*0.2);  System.out.println("本次销售所得佣金为:"+commission);
10         return commission; 
11     }
12     public static void main(String[] args){
13         while(true)
14         { 
15             int Headphone=0;
16             int Shell=0;
17             int Protector=0;
18             Scanner scanner=new Scanner(System.in) ;
19             try{
20                     System.out.println("请输入耳机的销售情况:");
21                     Headphone=scanner.nextInt();
22                     System.out.println("请输入手机壳的销售情况:");
23                     Shell=scanner.nextInt();
24                     System.out.println("请输入手机膜的销售情况:");
25                     Protector=scanner.nextInt();
26                     scanner.close();
27             }catch(Exception e){
28                 System.out.println("出现异常:"+e);
29                 System.exit(-1);
30             }
31             if(Headphone>=0&&Shell>=0&&Protector>=0)
32                 commission(Headphone,Shell,Protector);
33             else
34                 System.out.println("输入有误,请重新输入!");             
35         }
36     }
37 }

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

程序图节点

DD-路径

2123

A

25

B

31,3,4

C

5

D

6

E

7

F

8,9

G

10

H

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

1)语句覆盖

用例号

测试用例

覆盖路径

预期结果

实际结果

测试结论

1

-1,-1,-1

A,B

输入有误,请重新输入!

输入有误,请重新输入!

通过

 2

20,20,20

A,B,C,E,G,H

252

252

通过

2)判定覆盖

用例号

测试用例

覆盖路径

预期结果

实际结果

测试结论

1

-1,-1,-1

A,B

输入有误,请重新输入!

输入有误,请重新输入!

通过

2

20,20,20

A,B,C,E,F

252

252

通过

3)条件覆盖

用例号

测试用例

H>=0

S>=0

P>=0

All<=1000

All<=1800

All>1800

覆盖路径

预期结果

实际结果

测试结论

1

1-1-1

T

F

F

F

F

F

A,B

输入有误,请重新输入!

输入有误,请重新输入!

通过

2

-111

F

T

T

F

F

F

A,B

输入有误,请重新输入!

输入有误,请重新输入!

通过

3

10,10,10

 

T

T

T

T

F

F

A,B,C,D,H

98

98

通过

4

20,10,10

T

T

T

T

T

F

A,B,C,E,F,H

217

217

通过

5

20,20,20

T

T

T

T

T

T

A,B,C,E,G,H

252

252

通过

(4)条件判定覆盖

用例号

测试用例

H>=0

S>=0

P>=0

All<=1000

All<=1800

All>1800

条件1

H>=0

S>=0

P>=0

覆盖路径

预期结果

实际结果

测试结论

1

1-1-1

T

F

F

F

F

F

F

A,B

输入有误,请重新输入!

输入有误,请重新输入!

通过

2

-111

F

T

T

F

F

F

F

A,B

输入有误,请重新输入!

输入有误,请重新输入!

通过

3

10,10,10

 

T

T

T

T

F

F

T

A,B,C,D,H

98

98

通过

4

20,10,10

T

T

T

T

T

F

T

A,B,C,E,F,H

217

217

通过

5

20,20,20

T

T

T

T

T

T

T

A,B,C,E,G,H

252

252

通过

(5)路径覆盖

用例号

测试用例

覆盖路径

预期结果

实际结果

测试结论

1

-1,-1,-1

A,B

输入有误,请重新输入!

输入有误,请重新输入!

通过

2

10,10,10

A,B,C,D,H

98

98

通过

3

20,10,10

A,B,C,E,F,H

217

217

通过

4

20,20,20

A,B,C,E,G,H

252

252

通过

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

 1 import org.junit.Test;    
 2 import static org.junit.Assert.assertEquals;
 3 import java.util.Arrays;  
 4 import java.util.Collection;    
 5 import org.junit.runner.RunWith;  
 6 import org.junit.runners.Parameterized;  
 7 import org.junit.runners.Parameterized.Parameters; 
 8 
 9 @RunWith(Parameterized.class)
10 
11 public class test1Test {
12 
13      private int a;
14      private int b;
15      private int x ;
16      private float expected;
17      @Parameters
18      @SuppressWarnings("unchecked")  
19      public static Collection date(){ 
20          Object[][] object={{1,6,9,21.2f},{16, 5, 9,160.3f},{15, 6, 5,145.0f},{30, 11, 22,397.2f}};  
21          return Arrays.asList(object);
22      }
23      public test1Test(int a,int b,int x,float expected)
24      {
25          this.a=a;
26          this.b=b;
27          this.x=x;
28          this.expected=expected;
29      }
30 
31     @Test
32     public void testAdd() {
33         Caculator c=new Caculator();
34          float result=0;
35          result=c.ca(a,b,x);
36          System.out.println(result);
37          assertEquals(expected,result,0.01);
38 }
39 }

4、测试小结:

1通过此次实验对白和测试有了进一步的了解,掌握了如何画DD路径图,以及对测试覆盖的各种方法有了更深入的了解。

2.深入了解了junit,使用junit单元测试框架极大加快了测试速度与效率能够同时测试多个用例节约测试时间。

 

posted @ 2017-04-14 15:46  Trotteur  阅读(169)  评论(0编辑  收藏  举报