Liam的软件测试学习历程(三):JUint使用

  今天的上机课上,我尝试了使用JUint和EclEmma对项目进行测试。java

  首先是这两个工具的安装,JUint比较容易,只需将须要的jar包引入到项目中便可,而EclEmma则须要在Eclipse中选择安装新软件来进行安装。编程

  安装好后,是完成对待测项目的编程,此次的测试代码是一个检测三角形形状的方法。个人代码以下:ide

package com.triangleProblem;

import junit.framework.Test;

public class Triangle {

    
    public Triangle(){}
    
    public boolean testThreeSideAmount( int sideOne,int sideTwo,int sideThree ){
        boolean isRight = false;
        if ( (sideOne + sideTwo > sideThree)){
            isRight = true;
        }
        return isRight;
    }
    
    public boolean testIsTriangle(int sideOne,int sideTwo,int sideThree){
        boolean isTriangle = false;
        if (sideOne > 0 && sideTwo > 0 && sideThree > 0){
            if (testThreeSideAmount(sideOne, sideTwo, sideThree)){
                if(testThreeSideAmount(sideOne, sideThree, sideTwo)){
                    if(testThreeSideAmount(sideTwo, sideThree, sideOne)){
                        isTriangle = true;
                    }
                }
            }    
        }
        return isTriangle;
    }
    
    public boolean testSideEqual(int sideOne,int sideTwo){
        boolean equals = false;
        if (sideOne == sideTwo){
            equals = true;
        }
        return equals;
    }
    
    public String test(int sideOne,int sideTwo,int sideThree){
        String result;
        boolean isTriangle = this.testIsTriangle(sideOne,sideTwo,sideThree);
        if( isTriangle ){
            boolean ab = this.testSideEqual(sideOne, sideTwo);
            boolean ac = this.testSideEqual(sideOne, sideThree);
            boolean bc = this.testSideEqual(sideTwo, sideThree);
            if(ab && ac && bc){
                result = "Equilateral";
            }else if (!(ab || ac || bc)){
                result = "Scalene";
            }else {
                result = "Isosceles";
            }
        }else{
            result = "Not a Triangle";
        }
        return result;
    }
    
}

  这段代码中,主要有testThreeSideAmount()、testIsTriangle()、testSideEqual()和test()四个方法,他们的功能分别是测试前两个参数之和是否大于第三个参数、测试输入的三个数是否能够构成三角形、测试两个参数是否相等、判断三角形的形状。工具

  为了测试这些方法,我编写四个测试用例的代码,分别测试每一个方法,并在每段代码中我有对全部的可能状况,给出了相应的测试用例,这样一来在以后使用EclEmma进行测试时就能够达到100%的覆盖率了。测试

  最终的测试结果以下图:ui

  

从结果能够看出28个测试用例没有错误,且对目标代码Triangle.java的覆盖率达到了100%,这样就完成了测试。this

相关文章
相关标签/搜索