如何使用JUnit+JaCoCo+EclEmma完成单元测试

Posted William的小黑板

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何使用JUnit+JaCoCo+EclEmma完成单元测试相关的知识,希望对你有一定的参考价值。

 

看完点个赞呗,难道想白嫖不成?更多内容请访问微信公众号 :三国测,扫码关注哟!

 

 

 

 

原文链接:http://www.cnblogs.com/zishi/p/6726664.html

-----如何快速完成单元测试代码

1.      JUnit安装(http://junit.org/junit4/ 目前最新版本是4.12)

注:下面的一些演示图片是按照我本地的4.11版本,差别不大。

在项目上右键- Properties- java build path - Libraries, 点击Add External JARs, 选中刚刚下载的Junit包即可。如图所示:

 

 

 

 

或者使用以下方式添加:

 

在项目上右键- Properties- java build path - Libraries, 点击Add Library…,如下图所示:

 

 

在弹出窗口中,选中Junit,点击Next按钮继续:

 

 

 

 

接下来就是选择当前版本,我们目前使用的大版本是Junit 4,可以看到我本地版本为4.11,点击Finish按钮完成:

 

 

 

 

2.      JaCoCo和EclEmma安装

JaCoCo(Java Code Coverage)是一种分析单元测试覆盖率的工具JaCoCo(Java Code Coverage)就是一种分析单元测试覆盖率的工具,使用它运行单元测试后,可以给出代码中哪些部分被单元测试测到,哪些部分没有没测到,并且给出整个项目的单元测试覆盖情况百分比,看上去一目了然。EclEmma 是基于 JaCoCo 的一个 Eclipse 插件,开发人员可以方便的和其交互。下面简单介绍这两个工具的安装流程。

JaCoco安装:

首先我们访问JaCoco网站:http://www.eclemma.org/jacoco/ 下载最新版本,目前是:http://search.maven.org/remotecontent?filepath=org/jacoco/jacoco/0.7.9/jacoco-0.7.9.zip

 

 

压缩包解压后,将其中的Jar包拷贝到Java工程下面存放第三方Jar包的目录。比如在我本地存放在这里D:\\tools\\luna-withtestng\\luna\\eclipse\\plugins

 

 

 

重新启动Eclipse IDE即可加载JaCoCo插件。接下来我们安装EclEmma:

 

打开 Eclipse 的软件市场,在其中搜索 EclEmma,找到后完成安装,如下图所示:

 

 

 

 

直接安装即可。

3.      自动生成单元测试代码(默认Java项目已Ready):

选择需要生成单元测试代码的项目,并在需要生成测试代码的类上点右键 -  New – Other…,如下图所示:

 

 

 

 

在弹出窗口选择JUnit文件夹,点击展开,选择第一个“JUnit Test Case”,然后点击“Next”:

 

 

 

 

确认一下选中的Junit版本,其他全部按照默认选项即可:

 

 

 

确认无误,点击“Next”按钮,进入下一步:

 

 

 

此处选中我们需要生成单元测试代码的方法即可,此处我选择了之前写好的两个需要测试的函数,然后点击”Finish”按钮,最终对应生成的单元测试代码如下:

 

 

 

4.      运行单元测试

鼠标选中要运行的代码,EclEmma安装后直接可以点击IDE上方的工具栏,有一个运行按钮,如下图:

 

 

注:如果没有安装EclEmma插件,可以通过菜单Run As – Junit test执行单元测试

 

毫无疑问的,这次运行失败了:

 

 

 

详细的失败log如下:

 

HelloWorldTest.testAddMethod

testAddMethod(com.test.jacoco.HelloWorldTest)

java.lang.AssertionError: Not yet implemented

 at org.junit.Assert.fail(Assert.java:88)

 at com.test.jacoco.HelloWorldTest.testAddMethod(HelloWorldTest.java:19)

 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

  ………..

     

从上方的抛出的信息中,可以看到我们生成的断言中指定的消息“Not yet implemented”,另外,失败原因是我们默认生成的是一个fail断言,我们看一下关于fail断言的描述:

 

static void

fail()
Fails a test with no message.

static void

fail(String message)
Fails a test with the given message.

 

所以非常清楚的知道,这个断言的用途就是为了使单元测试立即失败,它其实并没有测试到我们的具体代码。

接下来我们给他们分别添加具体的测试代码和断言,如下图所示:

 

 

 

代码完成之后,继续调用Run As – Junit test,运行,这次毫无疑问运行成功了,看下图:

 

 

 

 

这样我们就完成了两个函数的单元测试代码覆盖。这里我们又用到了两个常用断言:assertNotNull() assertEquals()。Junit提供很多断言方法给我们,正确使用这些断言可以帮助我们确定被测试的函数是否按照预期的效果正常工作了,非常方便。我们在本文的最后部分提供了附录文档,详细介绍了Junit的全部断言目录,有兴趣的同学可以继续查看。

 

5.      查看单元测试覆盖率

JaCoCo和EclEmma安装完成后增加了以下特性:

 

1、未覆盖代码标记为红色,如下图:

 

 

2、已覆盖代码会标记为绿色,如图所示:

 

 

3、当然也有部分覆盖的情况,如图所示(还有可能是因为代码未完全执行):

 

 

 

4、在Eclipse下方的状态栏窗口,增加了一栏“Coverage”,点击可以显示详细的代码覆盖率:

 

5、颜色也可以在Eclipse中自定义设置:

 

 

 

 

 

 

以上是本次插件的全部介绍。我们通过这些工具可以快速生成单元测试代码,并利用分析工具将单元测试覆盖率尽量提高,这样我们对自己开发的代码更有信心了。当然,我们在这里并不是为了单纯的追求这个数字,在增加单元测试覆盖率的诱导下,我们重新理清了测试的步骤,写出了更有意义、更全面的单元测试。而且根据单元测试分析工具给的反馈,我们还发现了先前没有想到的情形。因此,单元测试的覆盖率并不只是一个为了取悦管理者的数据,它实实在在地帮助我们改善了代码的质量,增加了我们对所编写代码的信心。

6.  附录文档:Junit Assert目录

 

Junit Assert目录

static void

assertArrayEquals(boolean[] expecteds, boolean[] actuals)
Asserts that two boolean arrays are equal.

static void

assertArrayEquals(byte[] expecteds, byte[] actuals)
Asserts that two byte arrays are equal.

static void

assertArrayEquals(char[] expecteds, char[] actuals)
Asserts that two char arrays are equal.

static void

assertArrayEquals(double[] expecteds, double[] actuals, double delta)
Asserts that two double arrays are equal.

static void

assertArrayEquals(float[] expecteds, float[] actuals, float delta)
Asserts that two float arrays are equal.

static void

assertArrayEquals(int[] expecteds, int[] actuals)
Asserts that two int arrays are equal.

static void

assertArrayEquals(long[] expecteds, long[] actuals)
Asserts that two long arrays are equal.

static void

assertArrayEquals(Object[] expecteds, Object[] actuals)
Asserts that two object arrays are equal.

static void

assertArrayEquals(short[] expecteds, short[] actuals)
Asserts that two short arrays are equal.

static void

assertArrayEquals(String message, boolean[] expecteds, boolean[] actuals)
Asserts that two boolean arrays are equal.

static void

assertArrayEquals(String message, byte[] expecteds, byte[] actuals)
Asserts that two byte arrays are equal.

static void

assertArrayEquals(String message, char[] expecteds, char[] actuals)
Asserts that two char arrays are equal.

static void

assertArrayEquals(String message, double[] expecteds, double[] actuals, double delta)
Asserts that two double arrays are equal.

static void

assertArrayEquals(String message, float[] expecteds, float[] actuals, float delta)
Asserts that two float arrays are equal.

static void

assertArrayEquals(String message, int[] expecteds, int[] actuals)
Asserts that two int arrays are equal.

static void

assertArrayEquals(String message, long[] expecteds, long[] actuals)
Asserts that two long arrays are equal.

static void

assertArrayEquals(String message, Object[] expecteds, Object[] actuals)
Asserts that two object arrays are equal.

static void

assertArrayEquals(String message, short[] expecteds, short[] actuals)
Asserts that two short arrays are equal.

static void

assertEquals(double expected, double actual)
Deprecated. Use assertEquals(double expected, double actual, double delta) instead

static void

assertEquals(double expected, double actual, double delta)
Asserts that two doubles are equal to within a positive delta.

static void

assertEquals(float expected, float actual, float delta)
Asserts that two floats are equal to within a positive delta.

static void

assertEquals(long expected, long actual)
Asserts that two longs are equal.

static void

assertEquals(Object[] expecteds, Object[] actuals)
Deprecated. use assertArrayEquals

static void

assertEquals(Object expected, Object actual)
Asserts that two objects are equal.

static void

assertEquals(String message, double expected, double actual)
Deprecated. Use assertEquals(String message, double expected, double actual, double delta) instead

static void

assertEquals(String message, double expected, double actual, double delta)
Asserts that two doubles are equal to within a positive delta.

static void

assertEquals(String message, float expected, float actual, float delta)
Asserts that two floats are equal to within a positive delta.

static void

assertEquals(String message, long expected, long actual)
Asserts that two longs are equal.

static void

assertEquals(String message, Object[] expecteds, Object[] actuals)
Deprecated. use assertArrayEquals

static void

assertEquals(String message, Object expected, Object actual)
Asserts that two objects are equal.

static void

assertFalse(boolean condition)
Asserts that a condition is false.

static void

assertFalse(String message, boolean condition)
Asserts that a condition is false.

static void

assertNotEquals(double unexpected, double actual, double delta)
Asserts that two doubles are not equal to within a positive delta.

static void

assertNotEquals(float unexpected, float actual, float delta)
Asserts that two floats are not equal to within a positive delta.

static void

assertNotEquals(long unexpected, long actual)
Asserts that two longs are not equals.

static void

assertNotEquals(Object unexpected, Object actual)
Asserts that two objects are not equals.

static void

assertNotEquals(String message, double unexpected, double actual, double delta)
Asserts that two doubles are not equal to within a positive delta.

static void

assertNotEquals(String message, float unexpected, float actual, float delta)
Asserts that two floats are not equal to within a positive delta.

static void

assertNotEquals(String message, long unexpected, long actual)
Asserts that two longs are not equals.

static void

assertNotEquals(String message, Object unexpected, Object actual)
Asserts that two objects are not equals.

static void

assertNotNull(Object object)
Asserts that an object isn\'t null.

static void

assertNotNull(String message, Object object)
Asserts that an object isn\'t null.

static void

assertNotSame(Object unexpected, Object actual)
Asserts that two objects do not refer to the same object.

static void

assertNotSame(String message, Object unexpected, Object actual)
Asserts that two objects do not refer to the same object.

static void

assertNull(Object object)
Asserts that an object is null.

static void

assertNull(String message, Object object)
Asserts that an object is null.

static void

assertSame(Object expected, Object actual)
Asserts that two objects refer to the same object.

static void

assertSame(String message, Object expected, Object actual)
Asserts that two objects refer to the same object.

static

<T> void

assertThat(String reason, T actual, Matcher<? super T> matcher)
Asserts that actual satisfies the condition specified by matcher.

static

<T> void

assertThat(T actual, Matcher<? super T> matcher)
Asserts that actual satisfies the condition specified by matcher.

static void

assertTrue(boolean condition)
Asserts that a condition is true.

static void

assertTrue(String message, boolean condition)
Asserts that a condition is true.

static void

fail()
Fails a test with no message.

static void

fail(String message)
Fails a test with the given message.

 

感谢阅读,作者原创技术文章,转载请注明出处

 其他推荐相关阅读:

 

单元测试系列之一:如何使用JUnit、JaCoCo和EclEmma提高单元测试覆盖率

测试系列之二Mock工具Jmockit实战

单元测试系列之三:JUnit单元测试规范

单元测试系列之四:Sonar平台中项目主要指标以及代码坏味道详解

单元测试系列之五:Mock工具之Mockito实战

单元测试系列之六:JUnit5 技术前瞻

单元测试系列之七:Sonar 数据库表关系整理一(rule相关)

单元测试系列之八:Sonar 数据库表关系整理一(续)

单元测试系列之九:Sonar 常用代码规则整理(一)

单元测试系列之十:Sonar 常用代码规则整理(二)

单元测试系列之十一:Jmockit之mock特性详解

以上是关于如何使用JUnit+JaCoCo+EclEmma完成单元测试的主要内容,如果未能解决你的问题,请参考以下文章

Junit与Eclemma的初步安装与使用

如何在eclipse中安装与使用jacoco

Junit,hamcrest,eclemma单元测试基础(java)

JUNIT,HAMCREST,ECLEMMA单元测试基础(JAVA)

JaCoCo:分析单元测试覆盖率的利器

实验关于Junit和Eclemma