如何使用 Jacoco.exec 报告

我从 Jacoco 那里生成了一个代码覆盖报告,也就是 Jacoco.exec,但是我不知道怎么用它..。

我通过命令行生成它:

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

然后我拿到了 Jacoco.exec 的报告。我所需要的只是百分比的数量,并且我只使用命令行。有没有办法把这个报告转换成一个可读的文本文件?

谢谢大家

138337 次浏览

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.

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.

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

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

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.

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

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

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]

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.

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)

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.