how to use jacoco.exec report
Asked Answered
E

12

112

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

Exception answered 31/7, 2013 at 14:23 Comment(0)
D
141

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

Dreeda answered 3/5, 2017 at 12:53 Comment(4)
for this you first have to install the third party Emma PluginGinsberg
"Emma Plugin" is not really needed for this purposeHyperbolism
So, if Emma plugin not needed, how can I open the file if I don't have Show Coverage data menu ? version : 2019.3Lichenology
It looks like the command has been moved in the Run menu > Show Code Coverage DataDreeda
A
30

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.

Afghani answered 20/2, 2014 at 10:7 Comment(3)
Little correction. Right click > Import Session>Coverage Session >Next> {select compiled jar file of source code on which you have run javaagent to generate jacoco.exec}>FinishRori
Since when does Eclipse has a commandline-only mode like OP required?Drye
Its: Window > Show View > Java > Coverage nowAllurement
B
23

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]
Brother answered 15/4, 2020 at 10:9 Comment(1)
Hey, @wlhee, could you please mark this answer as correct for future generations? Thank you!Delacourt
H
19

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
Headsail answered 28/6, 2018 at 19:42 Comment(2)
I don't think the report would be generate. If you go look at documentation, you will see only jacoco.exec file will be generated. Once jacoco.exec is generated, you can run the report goal to get all the report generated. moreover if you using maven, you can just do mvn clean verify. as the goal report is attached to the verify phase of maven life cycle.Liguria
My answer probably assumes that you've done a build/test in a well-constructed Spring Boot project. This is likely the default if you create a skeleton with 'spring initializr'.Headsail
A
14

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.

Alithea answered 22/8, 2013 at 15:4 Comment(1)
and what if I do not use Ant?Mohammedanism
F
6

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.

Fawnfawna answered 24/4, 2020 at 0:6 Comment(0)
E
4

For IntelliJ Users you can select from main Menu Bar Run-> show coverage data from Menu Bar as shown in image.

For IntelliJ Users you can select from main Menu Bar Run-> show coverage data from Menu Bar as shown in image.

Now + button and select Jacoco-ut.exec and click show selected button

Now + button and select Jacoco-ut.exec and click show selected button

Now on the right side you will see coverage opened and you can also generate html files by button shown in image.

Now on the right side you will see coverage opened and you can also generate html files by button shown in image.

Enactment answered 13/1, 2023 at 7:3 Comment(0)
D
1

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.

Disorganization answered 19/9, 2017 at 10:13 Comment(0)
A
0

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

Alliber answered 14/9, 2018 at 21:39 Comment(0)
P
0

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)

Psychotechnology answered 29/4, 2020 at 12:54 Comment(0)
A
0

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.
Aldon answered 9/3, 2021 at 16:15 Comment(0)
L
-5

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

Lofty answered 4/9, 2017 at 6:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.