IntelliJ - exclude some classes (packages) from test coverage report
Asked Answered
G

3

53

I am currently using Java with Play framework. I have JUnit tests and I am trying to measure coverage with IntelliJ coverage runner.

Play unfortunately generates some rubbish classes and IntelliJ includes them in the report. Is there a way of excluding certain patterns from coverage report?

Griffith answered 28/1, 2015 at 14:6 Comment(1)
I'm in the same boat with framework classes sharing a package with my code - having a explicit exclude would be useful (all classes in a packed except x,y, and z).Endosmosis
H
26

You can specify which classes will be added to coverage data by editing run configuration of the test (tests) being executed.

Click on "Edit Configurations" when you have selected your test, open Code coverage settings tab and inside "Packages and classes to record coverage data" you can narrow down the packages from production code included in recording of coverage.

Hydrobomb answered 12/4, 2016 at 13:53 Comment(4)
Is there no way to exclude packages? That was the original question. I have many packages, one of which contains generated code and is polluting the coverage metrics. I'd rather not have to continually update the run settings every time I add a new packageRecalesce
Well, if you are generating some code, you should provide enough information to intellij for marking the code as "excluded" folder (through maven, gradle or so). Then it will be not included in code coverage by default. Anyway, I didn't met any explicit exclusion of the packages - but I can advice that you can set up your template for unit testing to set the packages in mentioned above way. After that each test run will be inherited from this template.Hydrobomb
When this is a Maven-project and it is reported (the refresh-like icon) the folders go back to where IntelliJ thinks they belong, gone are your exclusions. So, that's not a useful suggestion, really.Burgas
Allowing a regexp, or even multiple selections in the "Code coverage settings" UI would help a lot.Modish
I
17

Adding pictorial representation for @MichalD's answer

  1. Right click on your tst package & click on Modify Run Configuration... enter image description here

  2. In Code Coverage section, click on Modify options > Exclude classes and packages enter image description here enter image description here

  3. Then pick the classes or packages that you want to exclude by clicking on that little + icon

    • do note that here package-level inclusion / exclusion dialog box will present you an amalgamated package view (fusing together paths / classes from all packages you have added within current project), so might be little confusing

enter image description here

Ismaelisman answered 2/8, 2022 at 5:43 Comment(0)
S
6

Starting with Idea 2022.3 it is also possible to ignore methods based on annotations. It's very flexible because you can define in settings which annotations should mean that method is ignored. You can create new annotation and use it or use existing annotations (like from a framework).

This can be configured in

Settings / Preferences → Build, Execution, Deployment → Coverage

enter image description here

More details can be found in this announcement IntelliJ IDEA 2022.3 EAP 2: Improved IntelliJ Profiler, Faster IDE Startup, and More

Sizemore answered 16/10, 2022 at 16:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.