Reputation: 437
So this question might not be really specific, but i'm asking it anyway.
I'm trying to use JaCoCo with IntelliJ to gather coverage reports on unit tests. However, i don't have any experience whatsoever to set this up.
In Netbeans you just have to install the JaCoCo plugin and select to test with coverage. How does this work in IntelliJ?
I have googeled for quite a while without success. If someone has a good tutorial to set this up, i'd really appreciate it!
Upvotes: 28
Views: 83191
Reputation: 63
For those who stumble over this question in 2024: the setting moved from the run configuration to the main settings (File -> Settings)
Upvotes: 5
Reputation: 1
Latest versions of intellij community version has shifted coverage setting to File->Settings->Coverage->Java Coverage->Choose coverage runner->JaCoCo (as of 18/10/24)
Upvotes: 0
Reputation: 526
Thank you to all of the preceding answers. The following may be helpful for anyone using later versions of Intellij:
Run → Edit Configurations → Modify Options → Specify alternate coverage runner
and finally Apply → OK
Upvotes: 13
Reputation: 11
To apply JaCoCo to all future coverage runs, you can modify the Junit template and next coverage runs use this coverage.
Run/Debug Configurations
Edit Configuration templates...
Build and run
, click Modify Options
and select Code Coverage/Specific alternative coverage runner
Code Coverage
appear. Select JaCoCo
OK
or Apply
buttonUpvotes: 1
Reputation: 388
With IntelliJ 2021.2.1 (and possibly prior to that), Jetbrains switched the default to a bundled code coverage runner they provide. But it does not provide branch coverage, as JaCoCo does. JaCoCo is not listed in Plugins, but it was available as an alternative for me (perhaps because it's in the Maven POM).
The solution above using Run -> Edit Configurations will change a single run configuration (e.g. for a single JUnit test class). To change it for all new ones, after going to Run -> Edit Configurations, click Edit Configuration Templates on the lower left, and change the coverage runner there.
Upvotes: 3
Reputation: 891
To elaborate on jwenting's answer, go to Run > Edit Configurations..., and on the Code Coverage tab of your Run Configuration, select JaCoCo from the Choose coverage runner dropdown.
Upvotes: 25
Reputation: 535
You have to open your Run Configuration
1A. (top right by default) Tests in <project package>
1B. If you can't find it, you can go to the Run
menu at the top, then click the Run > ...
option. This will show you your configurations.
click Edit Configurations
. Find your Tests in <project package>
configuration.
Find Second tab from the left is Code Coverage
. Choose Coverage Runner: Jacoco
.
Apply
. OK
.
Run tests again :).
Upvotes: 0
Reputation: 2921
Intellij shows code coverage in the IDE. There is no need to install any extra plugins
when you right click on a Test.java file to run the unit test then there will be 3 options
Run "Test"
Debug "Test"
Run "Test" with **coverage**
Secondly, if you also want the branch coverage details then,
after running the test(as mentioned above) once, then then go to edit configuration -> at left side under Junit select the respective Test.java file -> select Code Coverage tab in the main window -> select Tracing radio button and tick the Track per test coverage checkbox
Upvotes: 3
Reputation: 5663
You don't need to do anything, if you have a recent version of IntelliJ. Just select to use JaCoCo as the coverage tool in the run configuration for your test suite or project and it will use its built-in JaCoCo version.
Upvotes: 18
Reputation: 4165
Have you looked at this document : https://www.jetbrains.com/help/idea/2017.1/code-coverage.html
They describe how to do such a task.
Upvotes: 4