Ixbitz
Ixbitz

Reputation: 437

JaCoCo with IntelliJ

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

Answers (10)

Mexallon
Mexallon

Reputation: 63

For those who stumble over this question in 2024: the setting moved from the run configuration to the main settings (File -> Settings)

enter image description here

Upvotes: 5

Anabil Debnath
Anabil Debnath

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

henry sneed
henry sneed

Reputation: 526

Thank you to all of the preceding answers. The following may be helpful for anyone using later versions of Intellij:

Community 2022.1 & Community 2022.2

RunEdit ConfigurationsModify OptionsSpecify alternate coverage runner

enter image description here

enter image description here

enter image description here

and finally ApplyOK

Upvotes: 13

To apply JaCoCo to all future coverage runs, you can modify the Junit template and next coverage runs use this coverage.

  1. Go to Run/Debug Configurations
  2. Click Edit Configuration templates...
  3. In Build and run, click Modify Options and select Code Coverage/Specific alternative coverage runner
  4. A new Code Coverage appear. Select JaCoCo
  5. Click OK or Apply button

Upvotes: 1

Ron HD
Ron HD

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

solimant
solimant

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.

Run Configuration Screenshot

Upvotes: 25

Stevers
Stevers

Reputation: 535

  1. 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.

  2. click Edit Configurations. Find your Tests in <project package> configuration.

  3. Find Second tab from the left is Code Coverage. Choose Coverage Runner: Jacoco.

  4. Apply. OK.

  5. Run tests again :).

Upvotes: 0

firstpostcommenter
firstpostcommenter

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

jwenting
jwenting

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

kgui
kgui

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

Related Questions