Mchr3k - JaCoCo

Credits

Download

JaCoCo

EclEmma

Update site: http://mchr3k-eclipse.appspot.com/

JaCoCo User Guide

Source Directives

The new default filters listed under Features below are always enabled.

Source directives allow coverage to be turned on/off in arbitrary sections of code. The following directives are supported:

///COVERAGE:OFF
///COVERAGE:ON

To allow for easy migration from Clover the following directives are also recognised:

///CLOVER:OFF
///CLOVER:ON

Source directives are disabled by default. To enable them you must add a new section to your jacoco coverage ant task:

<jacoco:report>
  <options>
    <sourcedirectives/>
  </options>
  <executiondata>
    <file file="${result.exec.file}"/>
  </executiondata>
  <structure name="JaCoCo Ant Example">
    <classfiles>
      <fileset dir="${result.classes.dir}"/>
    </classfiles>
    <sourcefiles encoding="UTF-8">
      <fileset dir="${src.dir}"/>
    </sourcefiles>
  </structure>
  <html destdir="${result.report.dir}"/>
</jacoco:report>

By default source directives will only be recognised when directives that turn off coverage are acomapnied by a comment e.g.

///COVERAGE:OFF because the following code is too simple to bother unit testing.

This requirement can be disabled by adding an extra parameter to the ant task:

  <options>
    <sourcedirectives requirecomment="false"/>
  </options>

EclEmma User Guide

Source Directives support can be configured through a new option in the Code Coverage preferences. This feature is disabled by default.

Features

This fork adds the following:

  • New default filters which ignore elements of bytecode which are not useful in a coverage report:
    • Empty no args constructors including implicit no args constructors.
    • Default enum methods - values() and valueOf().
    • Synchronization exit.
  • Arbitrary sections of source code can have coverage turned off using source directives.
    • Access to this feature can currently only be enabled from ant.

Blog Posts

Further details about this project can be found on my blog:

Clover to JaCoCo Migration

Source directives are currently supported by Clover so this section discussed how to migrate your build from Clover to JaCoCo. This example also assumes you will use offline instrumentation which allows tools like Powermock to work.

These instructions assume you store the required jacoco jars in the following locations:

  • lib/build/jacocoant.jar
  • lib/test/jacocoagent.jar

Task Definitions

Clover

<taskdef resource="clovertasks" />
<typedef resource="clovertypes" />

JaCoCo

<taskdef resource="org/jacoco/ant/antlib.xml">
  <classpath path="lib/build/jacocoant.jar"/>
</taskdef>

Instrumentation

Clover

<clover-setup initString="./reports/coverage/metadata/coverage.db">
  <fileset dir="./build/src" />
</clover-setup>
<clover-clean />
        
<javac destdir="./build/classes_instr" debug="true">
  <src path="./build/src" />
  <classpath>
    <fileset dir="./lib" includes="*.jar" />
    <fileset dir="./lib/test" includes="*.jar" />
  </classpath>
</javac>

JaCoCo

<instrument destdir="./build/classes_instr">
  <fileset dir="./build/classes" includes="**/*.class"/>
</instrument>

Running Tests

Clover - include clover.jar in junit classpath.

JaCoCo - include jacocoagent.jar in junit classpath. You also need to set a sysproperty within the junit task:

<sysproperty key="jacoco-agent.destfile" file="./reports/coverage/jacoco.exec"/>

Generating Reports

Clover

<clover-report>
  <current outfile="./reports/coverage">
    <format type="html" orderBy="ElementsCoveredDesc" />
  </current>
  <current outfile="./reports/coverage/current.xml" />
</clover-report>

JaCoCo

<report>
  <options>
    <sourcedirectives/>
  </options>
  <executiondata>
    <file file="./reports/coverage/jacoco.exec"/>
  </executiondata>
  <structure name="ProjectName">
    <classfiles>
      <fileset dir="./build/classes"/>
    </classfiles>
    <sourcefiles encoding="UTF-8">
      <fileset dir="./src"/>
    </sourcefiles>
  </structure>
  <html destdir="./reports/coverage"/>
</report>

Progress

22/02/2013 (unreleased)

  • Complete rewrite of finally block dedup to remove line number information requirement and improve accuracy.

08/01/2013

  • Merge latest JaCoCo changes, remove local offline instrumentation implementation.

06/01/2013

  • Fix finally block coverage.
  • Complete offline instrumentation.
  • Add tests for finally block coverage, improve comments on other tests.

16/12/2012

  • Get offline instrumentation working.
  • Fix bug in directives handling.
  • Ignore coverage of finally exception path.

09/12/2012

Fixed handling of source directives in light of:

  • Init methods can overlap line numbers of other methods.
  • Line numbers within methods are not always visited in order when you have try/catch/finally blocks.
  • Begin work on offline instrumentation.

28/11/2012

Initial release with new filters.