Software Testing Lab1

本文介绍如何使用JUnit 4.12和Hamcrest 1.3进行单元测试,并结合Eclemma实现代码覆盖率分析。主要内容包括安装配置过程、编写并测试一个判断三角形类型的程序。

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

 

转载于:https://www.cnblogs.com/zhuchenlu/p/8635278.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值