代码改变世界

TJU_SCS_软件测试_Lab1

2016-03-18 11:59  blueContra  阅读(223)  评论(0编辑  收藏  举报

一.实验要求

  1. Install Junit(4.12), Hamcrest(1.3) with Eclipse

  2. Install Eclemma with Eclipse

  3. Write a java program for the triangle problem and test the program with Junit.

  a) Description of triangle problem:

    Function triangle takes three integers a,b,c which are length of triangle sides; calculates whether the triangle is equilateral, isosceles, or scalene.

 

二.实验准备

  操作系统: Windows 8.1 64x

  IDEIntelliJ IDEA 15.0.1

 

三.实验步骤

  1. 安装JUnit Hamcrest

    需要导入JUnitHamcrestjar包。

       首先,先打开IntelliJ IDEA,用maven创建一个java工程。

    

其次,在网络上寻找到JUnitmaven上的依赖,并把依赖添加到java工程中的xml配置文件中

  

           这样相关的jar包也就会出现在工程中。

      

  2. 实现判定三角形类型的函数

由于问题并不复杂,事先也没什么难度,函数如下

 1     public int triangleJudgement(int a,int b, int c){
 2         //-1 rep not a triangle
 3 //        if(a <= 0 || b <= 0 || c <= 0)
 4 //        {
 5 //            return -1;
 6 //        }
 7 
 8         if(a + b > c && b + c > a && a + c > b)
 9         {
10             //2 rep equilateral, 1 rep isosceles, 0 rep scalene
11             if(a == b && b == c){
12                 return 2;
13             }
14             else if(a == b || b == c || a == c){
15                 return 1;
16             }
17             else
18             {
19                 return 0;
20             }
21         }
22         else
23             return -1;
24     }

    这里需要说明一下,需要先检测以下输入参数是否合法,比如输入负数,然而其实负数是不能够满足构成三角形的条件:任意两边之和大于第三边,所以我注释掉了部分代  码。

 

  3. 实现测试函数

    3.1 单测试用例测试

      这里实现了最简单的一种JUnit测试,需要在测试的函数前面加上@Test,代码如下:

1 public class Test1 {
2 
3     @Test
4     public void test1(){
5       App app = new App();
6         assertEquals(-1,app.triangleJudgement(1,2,3));
7     }
8 }

      测试正常时:

      测试出错时:

      以上是简单的单测试用例的示例。

 

    3.2 参数化多测试用例测试

      多测试用例测试需要参数化,需要用到@RunWith(Parameterized.class),测试类代码如下:

 1 @RunWith(Parameterized.class)
 2 public class Test2 {
 3     private int input1 = 0;
 4     private int input2 = 0;
 5     private int input3 = 0;
 6     private int expected = 0;
 7     private App AppTest = null;
 8 
 9     public Test2(int input1,int input2, int input3, int expected){
10         this.input1 = input1;
11         this.input2 = input2;
12         this.input3 = input3;
13         this.expected = expected;
14     }
15 
16     @Before
17     public void setUp(){
18         AppTest = new App();
19     }
20 
21     @Parameterized.Parameters
22     public static Collection<Object[]> getData(){
23         return Arrays.asList(new Object[][]{
24                 {3,1,2,-1},
25                 {3,3,3,2},
26                 {1,4,4,1},
27                 {5,3,4,0},
28                 {4,3,3,0},
29                 {2,2,2,1}
30 
31         });
32     }
33 
34     @Test
35     public void testApp(){
36         assertEquals(this.expected,AppTest.triangleJudgement(input1,input2,input3));
37     }
38 }

      首先我们需要申明以下数据域,expected,inputx等用于对应测试的期望值和输入,然后需要有一个返回Object数组的静态函数,然后测试的函数与之前的大致就相    同了,只不过期待值和参数都使用了我们之前声明的数据域。

      测试结果如下:

      可见参数化的方法可以一次对多个测试用例进行统一测试,相比较单测试用例的测试函数,效率高了很多。

  

  4. 安装Eclemma

    intellij IDEA并不能安装这个插件,所以需要使用Eclipse进行实验。

    EclEmma是一个开源的软件测试工具(for eclipse),可以在编码过程中查看代码调用情况、也可以检测单覆盖率。

          详见: http://eclemma.org/

    4.1 下载Eclemma

      1. 选择Help->Eclipse Marketplace->搜索EclEmma,Install;

      2. 重启eclipse发现工具栏上出现Coverage图标,说明安装成功;

        

    4.2 在Eclpise中使用Eclemma对代码的覆盖情况如下

  

      绿色表示代码被执行到了,运行结果有多种颜色,其中黄色表示代码部分执行到,红色表示代码没有被执行到。