Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jacoco not excluding classes when using ant

Tags:

jacoco

I'm having trouble getting a jacoco/junit ant target to exclude classes from coverage. I can get it to exclude packages though with something like this:

<jacoco:coverage destfile="${coverage.reports.dir.xml}/output.jacoco" excludes="foo.*:bar.fiz.*:my.long.package.name.*">

This doesn't exclude my test classes though because the test classes are in the same package as the classes they test. I've tired this to exclude the test classes with a regex, but it doesn't work.

<jacoco:coverage destfile="${coverage.reports.dir.xml}/output.jacoco" excludes="foo.*:bar.fiz.*:**/Test.*:**/Tests.*">

I also tried just including the classes I want in the report task, but since our test classes are in the same packages that doesn't work. Our build puts all the classes in the same directory, like buildRoot/classes/ProjectName. So buildRoot/classes/ProjectName/foo will contain the compiled classes for tests and non-test classes.

Any suggestions how how to get jacoco to exclude all tests in this setup?

thanks.

like image 214
adhamh Avatar asked Jan 18 '13 17:01

adhamh


People also ask

How do you exclude classes in JaCoCo code coverage?

Starting from JaCoCo 0.8. 2, we can exclude classes and methods by annotating them with a custom annotation with the following properties: The name of the annotation should include Generated. The retention policy of annotation should be runtime or class.

Why does JaCoCo not show coverage?

Why does the coverage report not show highlighted source code? Make sure the following prerequisites are fulfilled to get source code highlighting in JaCoCo coverage reports: Class files must be compiled with debug information to contain line numbers. Source files must be properly supplied at report generation time.

What is JaCoCo ant?

JaCoCo comes with Ant tasks to launch Java programs with execution recording and for creating coverage reports from the recorded data. Execution data can be collected and managed with the tasks coverage , agent , dump and merge . Reports in different formats are created with the report task.

What is branch coverage in JaCoCo?

JaCoCo mainly provides three important metrics: Lines coverage reflects the amount of code that has been exercised based on the number of Java byte code instructions called by the tests. Branches coverage shows the percent of exercised branches in the code, typically related to if/else and switch statements.


1 Answers

Specifying classes with jacoco:coverage excludes them from coverage, so they show up as having 0% coverage in the report.

In order to also exclude these classes from the JaCoCo report, you need to use classfiles fileset task and exclude them in the jacoco:report ant task.

<jacoco:report>
  <executiondata>
    <file file="${coverage.reports.dir.xml}/merged-jacoco.exec"/>
  </executiondata>
  <structure name="Unit Tests ${unit.test.run.ts}">
    <classfiles>
      <fileset dir="${build.root}/classes/ProjectName/" >
        <exclude name="**/*Test*.class" />
      </fileset>
    </classfiles>
    <sourcefiles encoding="UTF-8">
      <fileset dir="${src.root}/ProjectName/src/main"/>
    </sourcefiles>
  </structure>

  <html destdir="${coverage.reports.dir.html}"/>
</jacoco:report>
like image 61
adhamh Avatar answered Oct 12 '22 12:10

adhamh