首页 > 代码库 > #ST# Lab1 for ST

#ST# Lab1 for ST

Software Testing, Lab 1

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

  1.在github上下载两个jarjunit-4.12.jarhamcrest-all-1.3.jar地址为https://github.com/junit-team/junit4/wiki/Download-and-Install

  2.打开eclipse,新建项目,点击Project,选择Properties

   技术分享

  弹出如下界面,点击Add External JARs...,添加如上两个jar包。

  技术分享

 

2. Install Eclemma with Eclipse

  将eclemma-2.3.3.zip解压缩到eclipse下的dropins文件夹中,我的路径为:D:\Download\eclipse\eclipse\dropins\eclemma-2.3.3

  技术分享

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

  3.1 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.

 

  3.2 创建triangles类,编写代码,首先检测三条边是否可以构成三角形,如果构成,那是等边三角形还是等腰三角形或者是不等边三角形。代码如下:

   

package triangle;

public class triangles {
    
    public static String triangles(int a,int b,int c){
        
        if(a <= 0 || b <= 0 || c <= 0 || a + b <= c || a + c <= b || b + c <= a)
            
            return "Not a triangle";
        
        else {
            
            if(a == b && a == c)
                return "equilateral";
            
            else if(a == b || b == c || a == c)
                return "isosceles";
            
            else
                return "scalene";
            
        }
    }
}

 

 

  3.3 创建测试类,及测试用例,利用JUnit进行测试,代码如下:

   

package triangle;

import static org.junit.Assert.*;

import java.util.Arrays;

import java.util.Collection;

import org.junit.Test;

import org.junit.runner.RunWith;

import org.junit.runners.Parameterized;

import org.junit.runners.Parameterized.Parameters;

@RunWith(Parameterized.class)

public class testTriangle {

    private int a;

    private int b;

    private int c;

    private String expected;

    public testTriangle(int a,int b, int c, String expected){

        this.a = a;

        this.b = b;

        this.c = c;

        this.expected= expected;

        }

    @Parameters

    public static Collection<Object[]> getData(){

     return Arrays.asList(new Object[][]{

     {-1, 1, 2, "Not a triangle"},

     {3, 2, 5, "Not a triangle"},

     {6, 6, 6, "equilateral"},

     {2, 5, 5, "isosceles"},

     {7, 8, 9, "scalene"}

     });

    }

    @Test

    public void test() {

    assertEquals(this.expected,triangles.triangles(a,b,c));

    }

}

 

  3.4 利用JUnit进行测试,如下图,在testTriangle类上点击右键,选择run as, 然后选择JUnit Test

   技术分享

 

  测试结果如下:

  技术分享

 

  3.5 点击如下图标,利用Eclemma进行覆盖统计。

  技术分享

 

  结果如下图:

  技术分享

 

  可见,对于测试类的覆盖率为100%

 

 

 

 

#ST# Lab1 for ST