JUnit简单测试样例

发表于:2018-4-04 11:22  作者:Absintheme   来源:博客园

字体: | 上一篇 | 下一篇 |我要投稿 | 推荐标签: 软件测试工具 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包
  JUnit配置完成
  eclemma的安装过程相同,直接将eclemma-2.3.3.zip加入到Libraries里就可以了,配置完成后如下
  
  测试样例编写
  详情请参考开头的源码,主要是编写了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测试结果
  
  代码覆盖率测试
 
  之所以Main函数有部分没有覆盖到,是因为在Main中有部分代码用于判断和输出测试中的错误信息,而这个测试样例并没有发生错误,所以这段代码是跑不到的。

上文内容不用于商业目的,如涉及知识产权问题,请权利人联系博为峰小编(021-64471599-8017),我们将立即处理。

【直播预售】接口测试行业大佬带你从青铜上王者>>立即查看

评 论

论坛新帖

顶部 底部


建议使用IE 6.0以上浏览器,800×600以上分辨率,法律顾问:上海瀛东律师事务所 张楠律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2018, 沪ICP备05003035号
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪公网安备 31010102002173号

51Testing官方微信

51Testing官方微博

扫一扫 测试知识全知道