软件测试(三) Junit和Eclemma的安装及使用

环境配置

  • 我使用的IDE是Eclipse Mars,安装后就自带Junit,所以没有Junit的安装过程。
  • Eclemma的安装过程也可以直接通过Eclipse内嵌的插件市场进行安装。
    • 进入Eclipse,菜单栏help>>Eclipse Market Place
    • 这里写图片描述
    • 直接安装即可,重启eclipse之后就可以使用了

方法类的编写

一个判断三角形是否是等边的,是否是等腰的,是否是斜角三角形


public class Hello {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
         check ( 1 , 1 , 1 );
    }

    /***
     * 检查一个三角形是否为等边,等腰或者斜角三角形
     * @param a
     * @param b
     * @param c
     * @return The type of the triangle
     */
    public static int check ( int a , int b , int c ){
        int maxn = Math.max(Math.max(a,b), c);
        int x[]={a,b,c};
        int pow_sum = 0;
        int min_sum = 0;
        boolean flag = false;
        for ( int i = 0 ; i < x.length ; i++ )
        {
            if( x[i] != maxn || flag ){
                pow_sum += Math.pow(x[i], 2);
                min_sum += x[i];
            }
            if( x[i] == maxn ) flag = true;
        }
        //TODO 如果不能满足两边之和大于第三边,是非法三角形
        if( min_sum <= maxn ) return -1;
        //TODO 如果等边,返回1
        if ( a == b && b == c  ) return 1;
        //TODO 如果等腰,返回2
        if( a== b || b == c || a == c ) return 2;
        //TODO 如果斜角三角形,返回3
        if( Math.pow(maxn, 2) == pow_sum ) return 3;
        //TODO 如果是直角三角形,返回0
        return 0;
    }
}

测试类的编写

import static org.junit.Assert.*;

import org.junit.Test;

public class HelloTest {

    Hello hello = new Hello();

    /***
     * 负责用例测试Check函数
     */
    @Test
    public void testCheck() {
        int temp = hello.check(1, 1, 1);
        assertEquals( 1 , temp );
        temp = hello.check(1, 1, 3 );
        assertEquals( -1 , temp );
        temp = hello.check(2, 2, 3 );
        assertEquals( 2 , temp );
        temp = hello.check(2, 3, 4);
        assertEquals ( 3 , temp );
    }
}

测试结果和覆盖率

这里写图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值