Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jacoco: Find code coverage for external tests

I recently used EclEmma plugin in eclipse to find code coverage of JUnits in my application and it worked great.

In the next step, I want to be able to find code coverage for end-to-end tests / functional tests which are called on our application from outside(they reside on a separate server).

Basically, we package our application as a jar (we use maven) and deploy it on a server. And we can trigger the functional tests at this location.

Is there a way to find code coverage in this case?

My understanding of how code coverage tool works (in raw language) is that it loads the classes and generates a report based on the part of code that has been hit. So as per this understanding, I don't need to have access to the test-code. I only need to somehow plug the code coverage tool into my application jar and whenever any code in this jar is called, report will be updated. Is my understanding correct?

NOTE: I am open to use other code coverage tool if this is possible with other tools.

like image 351
tryingToLearn Avatar asked May 02 '18 11:05

tryingToLearn


3 Answers

You can run your code on a server, instrumented at runtime by the JaCoCo agent, by adding the agent to the Java command line. For example if your process is currently launched with:

java -jar myApp.jar

You can change it to

java -jar myApp.jar -javaagent:/some/path/jacocoagent.jar

By default this will write coverage data to the file jacoco.exec when the VM terminates, but you can override this with options, enabling you to get coverage data over TCP/IP. See the JaCoCo Agent docs.

You can format this into a report (e.g. HTML) using:

java -jar jacococli.jar report jacoco.exec [options]

See the JaCoCo CLI docs for options.

... or you can use EclEmma to analyse the output.

If there are genuine reasons you can't use the agent, as a last resort you can statically convert your class files into instrumented class files, using the instrument command in jacocococli. See the JaCoCo 'Offline Instrumentation' documentation for information about this.

Directly invoking the agent and using jacococli.jar are the most basic ways of using JaCoCo. Since you are using Maven, you can get many of the same effects using the JaCoCo Maven plugin.

like image 174
slim Avatar answered Oct 13 '22 00:10

slim


I had the same issue. This is how I fixed it locally. By adding the jacoco agent in the vm args. In addition, after jacoco version 6.1, the exec file is created at the beginning (blank) and then later populated after the server is gracefully shutdown which apparently eclipse doesn't do. So we get an empty .exec file. The key is to add output=tcpserver and then import the code coverage.

-javaagent:C:\Users\JohnDoe\Downloads\jacoco-0.8.5\lib\jacocoagent.jar=output=tcpserver.

Import the results into Eclipse. This is done from File -> Import -> Coverage Session -> select Agent address radio button but leave defaults -> enter some name and select code under test. Default should be 127.0.0.1 with port 6300.

like image 28
Mayur Avatar answered Oct 12 '22 22:10

Mayur


This is how i did it with maven in a jenkins pipeline

mvn clean org.jacoco:jacoco-maven-plugin:prepare-agent test -Dmaven.test.failure.ignore=true 

This will create a target/jacoco.exec that has the code coverage data.

Obviously we cannot interpret the output, but tools and plugins like SonarQube can do that.(In my case i used sonarqube)

You can however use below to generate into html/csv format which will be located in target/site/jacoco/index.html , target/site/jacoco/jacoco.csv

mvn org.jacoco:jacoco-maven-plugin:report OR

java -jar jacococli.jar report jacoco.exec [options]

Alternatively, you can refer to this tutorial which did all this in the pom.xml

like image 20
codereal Avatar answered Oct 12 '22 23:10

codereal