TIL: Code Coverage With Intellij

As unit testing is still very much a new thing to me, I was interested to see how much of my code my tests were actually hitting. I knew that there was some functionality built into intellij, but I was under the impression that it was locked behind a paywall. Turns out it’s completely free to use in the community version!

Turning it on

I noticed that the option was available to me straight from the start on my Windows box, but back on my Mac I didn’t see the option. I don’t know if I accidently turned it off when I installed on the Mac or what, but it threw me for a second.

In order to ensure that you have code coverage turned on, you need to ensure that the plugin is enabled. To get there, you can use cmd + , on Mac, or go to File -> Preferences. Click on “Plugins” on the left menu and then look for “Coverage”.

/img/posts/2018/2018-08-30_4.png
Coverage setting

Check the box and then apply the changes (this will require a restart of Intellij).

Running Code Coverage

Easiest way to check for your coverage is to run your suite of tests. You can start by right clicking on either your project name or your test file (I only have one in my current project), and looking for the option to Run '<tests>' with Coverage:

/img/posts/2018/2018-08-30_1.png
Right Click menu

This will kick off a run of our test(s). When the run has finished, it will give you a results summary of the coverage that it found:

/img/posts/2018/2018-08-30_2.png
Results summary

This will give you an idea of what still is missing tests. By going into a specific file, you’re shown either a green or red bar next to your sections of code. This will show you what’s being included (green) and what is not (red):

/img/posts/2018/2018-08-30_3.png
Coverage Colors

It’s such a small thing but a really big help for trying to debug where you’re missing test coverage in your code.

💚