Software Testing Lab1

Lab1 Junit and Eclemma

Tasks:

  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.

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

(1)Download the Junit and Hamcrest jars

(2)right click the project, click “Properties”, click “Java Build Path” on the left, click “Libraries”, click “ Add Library…”. Then select “Junit4”

 

Click "Add external JARs", select the jars that you have downloaded.

 

2Install Eclemma with Eclipse

Select "Help"->"Install New Software"->"Add"->"local"

 

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

Caculator.java

package cn.tju.scs;

 

public class Caculator {

    public static int isTriangle(int a, int b, int c){

       int max = Math.max(a,b);

       max = Math.max(max, c);

       double sum;

       double  quadraticSum;

       double  quadraticMax = Math.pow(max, 2);

       if(max == a){

           sum = b + c;

           quadraticSum = Math.pow(b, 2) + Math.pow(c,2);

       }

       else if(max == b){

           sum = a + c;

           quadraticSum = Math.pow(a, 2) + Math.pow(c, 2);

       }

       else{

           sum = a + b;

           quadraticSum = Math.pow(b, 2) + Math.pow(a, 2);

       }

      

       if(sum < max || sum == max){

           return -1;

           //不是三角形

       }

       else{

           if( quadraticMax ==  quadraticSum)

           {

              return 0;

              //直角三角形

              //非直角三角形就是斜角三角形

           }

           else if(a == b && b == c){

              return 1;

              //等边三角形

           }

           else if(a == b || b == c){

              return 2;

              //等腰三角形

           }

           else{

              return 3;

              //普通三角形

           }

       }

    }

}

新建测试类”New”->”other”->”java”->”Junit”->”Junit Test Case”

CaculatorTest.java

package cn.tju.scs;

 

import static org.junit.Assert.*;

 

import org.junit.Before;

import org.junit.Test;

 

public class CaculatorTest {

    private static Caculator caculator = new Caculator();

    @Before

    public void setUp() throws Exception {

    }

 

    @Test

    public void testIsTriangle() {

       assertEquals(-1,caculator.isTriangle(1,5,7));//不是三角形

       assertEquals(0,caculator.isTriangle(3,4,5));//直角三角形

       assertEquals(1,caculator.isTriangle(5,5,5));//等边三角形

       assertEquals(2,caculator.isTriangle(5,5,6));//等腰三角形

       assertEquals(3,caculator.isTriangle(4,5,6));//普通三角形

    }

 

}

Right click the project, “Run as”->”Junit Test Case”

Coverage Test:

 

CaculatorTest.java

 

效果如图,其中红色代表未执行,黄色代表条件没有完全执行,绿色代表执行过了。

 

(1). Runs:表示总共有几个测试方法,已经运行了几个;

(2). Errors:表示抛出异常的测试方法的个数;

(3). Failures:表示失败的测试方法的个数;

(4). 打钩:表示通过测试方法。

(5). 另外有个绿色的进度条表示测试成功,红色的进度条则表示测试失败。

GitHub: https://github.com/zhuchenlu/SoftwareTesting

 

posted @ 2018-03-23 21:51  3015218109  阅读(62)  评论(0编辑  收藏  举报