Code Coverage for Cucumber Tests using Jacoco - java

I am trying to integrate Jacoco to get the code coverage for my Cucumber tests using Maven. Following is my project structure:
-src-main-java-Pages
-src-main-java-Helper
-src-test-java-resources-features
-src-test-java-Steps
Following is the Jacoco configuration in my POM.xml
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.2</version>
<configuration>
<destFile>${basedir}/target/coverage-reports/jacoco.exec</destFile>
<dataFile>${basedir}/target/coverage-reports/jacoco.exec</dataFile>
<outputDirectory>${basedir}/target/coverage-reports/jacoco</outputDirectory>
</configuration>
<executions>
<execution>
<id>prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<goals>
<goal>report</goal>
</goals>
</execution>
<execution>
<id>check</id>
<goals>
<goal>check</goal>
</goals>
<configuration>
<rules>
<rule>
<element>PACKAGE</element>
<includes>
<include>**/*Steps.*</include>
</includes>
</rule>
<rule>
<element>PACKAGE</element>
<excludes>
<exclude>**/*Pages.*</exclude>
<exclude>**/*Page.*</exclude>
</excludes>
</rule>
</rules>
</configuration>
</execution>
</executions>
</plugin>
I am able to generate the code coverage report. However, in the report, it covers all the classes in the -src-main packages. As per different google researches and SO posts, I tried to modify my POM to exclude Pages and include only Steps in my code coverage report. But somehow, it keeps on displaying all the files in the main package.
Am I taking the wrong approach? I just want to create a report which does code coverage for only the tests that I execute rather than all the files in the main package.

See https://maven.apache.org/guides/mini/guide-configuring-plugins.html - you specify exclusions in configuration of
<execution>
<id>check</id>
<goals>
<goal>check</goal>
but not in configuration of
<execution>
<id>report</id>
<goals>
<goal>report</goal>
so it has effect on check, but not on report.

Related

SonarQube coverage showing 0% even after exclusion

I been working on a SpringBoot project and I am trying to exclude some of the packages from the SonarQube code coverage.
The package that I want to exclude is com.org.projectname.model. I tried, <sonar.exclusions>**/model/*.java</sonar.exclusions> and <sonar.coverage.exclusions>**/model/*.java</sonar.coverage.exclusions>, sill SonarQube is showing 0% coverage, why is that?. Also I am curious that, do we need to have any specific dependency to use <sonar.exclusions>, as I am not using any additional dependency related to SonarQube?
or is there any other way to achieve this?
<properties>
<sonar.exclusions>
**/model/*.java
</sonar.exclusions>
</properties>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.6</version>
<configuration>
<excludes>
</excludes>
</configuration>
<executions>
<execution>
<id>pre-unit-test</id>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<destFile>target/coverage-data/jacoco-ut.exec</destFile>
<propertyName>surefireArgLine</propertyName>
</configuration>
</execution>
<execution>
<id>post-unit-test</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<dataFile>target/coverage-data/jacoco-ut.exec</dataFile>
<outputDirectory>target/coverage-reports/jacoco-ut</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>

Code coverage for test framework written in Node and application is in Java

I have written my test framework which is a node service and the application is written in Java. I want to understand how to get code coverage? I have tried to use Jacoco but it did not generate any coverage details. Is there any dependency for node projects for jacoco that I should use ? Or is there any other way of generating code coverage for this type of scenario ?
I have added dependencies to pom.xml as here
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.1.201405082137</version>
<executions>
<execution>
<id>default-prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>default-report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
<execution>
<id>default-check</id>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
<configuration>
<dataFile>target/jacoco.exec</dataFile>
<outputDirectory>/target/jacoco</outputDirectory>
</configuration>
</plugin>

My Jacoco report is showing very little code coverage

I am trying to get an idea of how much code coverage I have with some unit tests in Scala and with the source code in Java(tests and code are in different packages). When I run the Jacoco report, it is showing only 4 percent code coverage.
When I go take a look at the report, it shows 0 percent for a lot of files that I have created tests for. What I suspect is that the unit tests are not being included in the report, maybe since all of the test class files are in the target/test-classes directory.
I have tried including the includeTests tag but that didn't have any effect.
Here is how the plugin looks like in the pom:
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco-maven-plugin.version}</version>
<configuration>
<includeTests>true</includeTests>
<!--<includes>
<include>**/test-classes/**</include>
</includes>-->
<excludes>
<exclude>**/dto/**</exclude>
<exclude>**/exceptions/**</exclude>
</excludes>
</configuration>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
Right now I have commentted out the include tag becasue if I include it, the test compiles with 0 classes. How else can I can include the target/test-classes directory?
Sorry if this is a simple fix, I'm very new with unit testing and Jacoco. Thanks!
This is my personal setup. It's a bit messy and I'm still working out some parts of it but it works for me. You could try this.
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.4</version>
<executions>
<execution>
<id>default-prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>default-report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
<execution>
<id>default-check</id>
<goals>
<goal>check</goal>
</goals>
<configuration>
<rules>
<rule>
<element>BUNDLE</element>
<limits>
<limit>
<counter>COMPLEXITY</counter>
<value>COVEREDRATIO</value>
<minimum>0.20</minimum>
</limit>
</limits>
</rule>
</rules>
</configuration>
</execution>
</executions>
</plugin>

How to exclude generated code from a code coverage report?

I've got the following jacoco-maven-plugin configuration:
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco.version}</version>
<configuration>
<excludes>
<exclude>**/Header*.java</exclude>
</excludes>
</configuration>
<executions>
<execution>
<id>prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
</executions>
</plugin>
Which should exclude from a code coverage report all generated java files that begin with Header. Unfortunately, I still see these classes in my code coverage report which makes the coveralls-maven-plugin fail when I call the coveralls:report. The error I get when I call coveralls:report is:
: No source found for HeaderMyClass.java ->
Which makes me think that the JaCoCo coverage report still contains the data for this class which was automatically generated.
Just add exclusions for report goal in jacoco-maven-plugin configuration
<execution>
<id>coverage-report</id>
<phase>post-integration-test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<excludes>
<exclude>**/*Dto.class</exclude>
<exclude>com/foo/config/*</exclude>
</excludes>
</configuration>
</execution>
Changing the pattern to:
<excludes>
<exclude>**/Header*.*</exclude>
</excludes>
did the trick

Instruct Jacoco and Coveralls to ignore some files from the report

I use Coveralls in a Java project with Maven to create a coverage report for my project and I want to avoid some classes from being included in the coverage computation.
Now, I think that I the build phase I can exlcude the class, e.g. MyClassTest.java as follows:
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.5.201505241946</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
<configuration>
<excludes>
<exclude>**/*MyClassTest.java</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.eluder.coveralls</groupId>
<artifactId>coveralls-maven-plugin</artifactId>
<version>4.1.0</version>
</plugin>
Anyway, the coverage doesn't decrease, so I guess that the class MyClass is still computed from the Jacoco report.
In the travis file I call the report phase as follows:
- mvn clean test jacoco:report coveralls:report
Any idea?
My bad!
First the .java has to be removed and replace with .class as correctly suggested by Tunaki.
Second, I don't have to exclude the test class, but the class that I don't want to measure in the coverage!
So it is:
<excludes>
<exclude>**/*MyClass</exclude>
</excludes>
instead of
<excludes>
<exclude>**/*MyClassTest.java</exclude>
</excludes>

Categories