JUnit简单测试样例

JUnit简单测试样例

源码

Main.java

package github.hyzeta;

import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;

public class Main {

    public static void main(String[] args) {
        // write your code here
        Result result = JUnitCore.runClasses(TriangleFunTest.class);
        if (result.wasSuccessful()) {
            System.out.println("All tests for triangle function has passed.");
        } else {
            System.out.println("Some tests for triangle function has failed.");
            for (Failure failure : result.getFailures()) {
                System.out.println(failure.toString());
            }
        }
    }

}

TriangleFun.java

package github.hyzeta;

enum TriangleFuncResult {
    EQUILATERAL, ISOSCELES, SCALENE, ERROR
}

public class TriangleFun {
    public TriangleFuncResult triangle(int a, int b, int c) {
        if (a <= 0 || b <= 0 || c <= 0) {
            return TriangleFuncResult.ERROR;
        }
        if (!(a + b > c && Math.abs(a - b) < c)) {
            return TriangleFuncResult.ERROR;
        }
        if (a == b && b == c) {
            return TriangleFuncResult.EQUILATERAL;
        }
        if (a == b || a == c || b == c) {
            return TriangleFuncResult.ISOSCELES;
        }
        return TriangleFuncResult.SCALENE;
    }
}

TriangleFunTest.java

package github.hyzeta;

import org.junit.Test;

import static org.junit.Assert.*;

public class TriangleFunTest {
    @Test
    public void testErrorTriangle() {
        assertEquals(new TriangleFun().triangle(0, 0, 0), TriangleFuncResult.ERROR);
        assertEquals(new TriangleFun().triangle(-1, -1, -1), TriangleFuncResult.ERROR);
        assertEquals(new TriangleFun().triangle(-1, 2, 3), TriangleFuncResult.ERROR);
        assertEquals(new TriangleFun().triangle(1, 2, 3), TriangleFuncResult.ERROR);
    }

    @Test
    public void testEquilateralTriangle() {
        assertEquals(new TriangleFun().triangle(3, 3, 3), TriangleFuncResult.EQUILATERAL);
        assertEquals(new TriangleFun().triangle(1, 1, 1), TriangleFuncResult.EQUILATERAL);
        assertEquals(new TriangleFun().triangle(2, 2, 2), TriangleFuncResult.EQUILATERAL);
    }


    @Test
    public void testIsosceles() {
        assertEquals(new TriangleFun().triangle(2, 2, 3), TriangleFuncResult.ISOSCELES);
        assertEquals(new TriangleFun().triangle(4, 4, 5), TriangleFuncResult.ISOSCELES);
        assertEquals(new TriangleFun().triangle(5, 7, 7), TriangleFuncResult.ISOSCELES);
    }

    @Test
    public void testScalene() {
        assertEquals(new TriangleFun().triangle(2, 3, 4), TriangleFuncResult.SCALENE);
        assertEquals(new TriangleFun().triangle(3, 4, 5), TriangleFuncResult.SCALENE);
        assertEquals(new TriangleFun().triangle(12, 22, 26), TriangleFuncResult.SCALENE);
    }

}

JUnit和eclemma安装过程

直接在Maven仓库里搜索JUnit和hamcrest-core

http://search.maven.org/#search|gav|1|g:%22junit%22%20AND%20a:%22junit%22
http://search.maven.org/#search|gav|1|g:%22org.hamcrest%22%20AND%20a:%22hamcrest-core%22

下载junit-4.12.jar包和hamcrest-core-1.3.jar包

然后在IntelliJ IDEA里的Project Structure里的Libraries里添加这两个jar包

creen Shot 2018-03-22 at 11.36.21 A

JUnit配置完成

eclemma的安装过程相同,直接将eclemma-2.3.3.zip加入到Libraries里就可以了,配置完成后如下

creen Shot 2018-03-22 at 11.38.16 A

测试样例编写

详情请参考开头的源码,主要是编写了TriangleFun类(含有triangle函数)和TriangleFuncResult枚举类型,TriangleFuncResult枚举类型包含四个结果:

  • 不是三角形:TriangleFuncResult.ERROR
  • 等边:TriangleFuncResult.EQUILATERAL
  • 等腰:TriangleFuncResult.ISOSCELES
  • 非等腰三角形:TriangleFuncResult.SCALENE

然后编写测试类型TriangleFunTest,包含四个测试函数testErrorTriangle,testEquilateralTriangle,testIsosceles,testScalene,分别测试四种结果。

最后在Main函数中主动唤起JUnit测试

Result result = JUnitCore.runClasses(TriangleFunTest.class);

然后判断是否正确,如果测试通过则汇报成功,不成功则打印所有错误信息。

if (result.wasSuccessful()) {
    System.out.println("All tests for triangle function has passed.");
} else {
    System.out.println("Some tests for triangle function has failed.");
    for (Failure failure : result.getFailures()) {
        System.out.println(failure.toString());
    }
}

JUnit测试结果

creen Shot 2018-03-22 at 11.46.37 A

代码覆盖率测试

creen Shot 2018-03-22 at 11.47.18 A

creen Shot 2018-03-22 at 11.47.41 A

之所以Main函数有部分没有覆盖到,是因为在Main中有部分代码用于判断和输出测试中的错误信息,而这个测试样例并没有发生错误,所以这段代码是跑不到的。

转载于:https://www.cnblogs.com/hyzeta/p/8622738.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值