how to use jacoco.exec report

TestingCode CoverageJacoco

Testing Problem Overview


I generated a code coverage report from jacoco, which is jacoco.exec. But I don't know how to use it ...

The way I generated it is through command line:

java -javaagent:/path/to/jacocoagent.jar=include=some.package.*,output=file org.junit.runner.JUnitCore some.package.ClassTest

Then I got the jacoco.exec report. All I need is just the number of percentage, and I am using command line only. Is there a way to convert this report to a readable txt file?

Thanks all

Testing Solutions


Solution 1 - Testing

In IntelliJ Idea from the menu select Analyze > Show Coverage data. In the new window press the + button and select your .exec file. The test coverage results will appear in the editor Coverage tab.

Update:

In the latest version of Intellij Idea the menu has been moved to Run > Show Code Coverage Data

Solution 2 - Testing

For Eclipse users, you can simply use EclEmma jacoco plugin in Eclipse. Window > Show View > Coverage (of course you must install the plugin first). In the Coverage window, Right click > Import >..... Select the exec file (or other nice methods), select your source code, then see. You can also export the result to html file.

Solution 3 - Testing

I think the report will have already been generated. Look in the folder target/site/jacoco.

This provides target/site/jacoco/jacoco.csv, which is some raw text that you can interpret relatively easily -- maybe import into a spreadsheet

Most people will want target/site/jacoco/index.html, which is a report in web-page form.


If you aren't seeing these reports, try explicitly requesting them and see if any clues are provided...

mvn clean test jacoco:report

Solution 4 - Testing

Per this thread you can't use your generated jacoco.exec directly to produce a report. You can download Jacoco's sample build.xml and use it to produce a report, instead. You'll need to make these changes to build.xml: set the the paths to

  • your downloaded jacocoant.jar
  • your jacoco.exec
  • your project source code
  • your compiled project class files

I also changed the default target to "report". Then run it by typing "ant" and your reports will be generated.

Solution 5 - Testing

Jacoco provides a command line lib to process jacoco.exec data: Jacoco cli doc

After you install Jacoco, you can generate report with following command:

java -jar lib/jacococli.jar report jacoco.exec \
--html ./report \
--sourcefiles [path/to/your/source/files] \
--classfiles [path/to/your/class/files]

Solution 6 - Testing

This answer would be similar to @Evans Y. One can generate HTML files (over here in report directory) and XML file (named as cov) with the help of below command from Jacoco documentation.

java -jar lib/jacococli.jar report jacoco.exec \
--classfiles C:\Users\severalOtherDirectories\YourProject\target\classes \
--html ./report --xml cov.xml

HTML Report: This report would be able to show total number of lines covered/uncovered at the Class or method level, but won't be able to show what actual line(s) are covered/uncovered in the same.

XML file: After plugging this generated file in the project and simply using VS code coverage extension (I prefer coverage gutters) , one can visualize line-by-line status in the editor itself.

Solution 7 - Testing

we can push jacoco exec report (created as part of maven build) to the sonar(qube) server using maven-sonar-plugin's target, sonar:sonar

mvn clean install sonar:sonar -Dsonar.host.url=http://:9000 -Dsonar.projectKey= -Dsonar.branch= -Dsonar.login= -Dsonar.password=

sonar.projectKey and sonar.branch properties value can be retrieved from corresponding project created in sonarqube.

Solution 8 - Testing

If you use maven, use the report-aggregate goal.

See link below:

report aggregate maven goal

This is a snippet from my maven pom.xml file

			<plugin>
			<groupId>org.jacoco</groupId>
			<artifactId>jacoco-maven-plugin</artifactId>
			<version>0.8.2</version>
			<executions>
				<execution>
					<goals>
						<goal>prepare-agent</goal>
					</goals>
				</execution>
				<execution>
					<id>report-aggregate</id>
					<phase>prepare-package</phase>
					<goals>
						<goal>report</goal>
					</goals>
				</execution>
			</executions

The csv report file was generated under: site/jacoco/jacoco.csv

Solution 9 - Testing

To view this in IntelliJ Idea, from the menu bar, select Run > Show Code Coverage Data. In the new window (Choose Coverage Suite to Display), press the + button and select your .exec file. The test coverage results will appear in the editor Coverage tab.

To generate the Coverage Report files for the above .exec file, select Run > Generate Coverage Report. Then select your output directory and click on Save. Your reports would be generated to the selected folder. Open the index.html file in the folder to view the results on the browser. I am using IntelliJ IDEA 2019.3.4 (Community Edition)

Solution 10 - Testing

For whatever reason, I could not get EclEmma and Jacoco to work with Eclipse as people have suggested so I stumbled upon the following work-around.

  1. Insure that you have added jacoco plugin as a dependency to the maven pom
  2. Open Run Configurations... , add a new Maven Build to the project you are working on
  3. use the following as your goals: clean test jacoco:report
  4. Apply and run, refresh your /target directory and now you should see /target/site/jacoco
  5. Within the jacoco directory find index.html, right click and select Open With... and choose Web Browser
  6. Your jacoco.exec is now fully navigable within Eclipse via the Web Browser
  7. Make a code coverage change, run the Maven Build job you setup, refresh the browser and you should see the difference.

Solution 11 - Testing

terminal: mvn install jacoco:report for maven project with jacoco plugins

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionwlheeView Question on Stackoverflow
Solution 1 - TestingRobertoView Answer on Stackoverflow
Solution 2 - TestingLoc PhanView Answer on Stackoverflow
Solution 3 - TestingBrent BradburnView Answer on Stackoverflow
Solution 4 - TestingChrisView Answer on Stackoverflow
Solution 5 - TestingEvans Y.View Answer on Stackoverflow
Solution 6 - TestingYashwin MunsadwalaView Answer on Stackoverflow
Solution 7 - TestingShakti GargView Answer on Stackoverflow
Solution 8 - TestingYounessView Answer on Stackoverflow
Solution 9 - TestingArthasView Answer on Stackoverflow
Solution 10 - TestingJavaJdView Answer on Stackoverflow
Solution 11 - Testingjohn zhangView Answer on Stackoverflow