Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get test coverage or ignore by Jacoco for Kotlin data classes

Jacoco shows 0% coverage for Kotlin's data classes. How to configure it to measure coverage or ignore data classes at all?

like image 507
tse Avatar asked Jan 23 '18 08:01

tse


People also ask

How do you exclude classes in JaCoCo code coverage?

Starting from JaCoCo 0.8. 2, we can exclude classes and methods by annotating them with a custom annotation with the following properties: The name of the annotation should include Generated. The retention policy of annotation should be runtime or class.

Does JaCoCo work for Kotlin?

Many tools, both open-source, and commercial, have been created to serve this need and JaCoCo is one among them. In this article, we will learn how to set up Kotlin code coverage with JaCoCo and Gradle. JaCoCo is an open-source project, which can be used to check source code for test code coverage.

How do you exclude classes from code coverage?

The easiest way to exclude code from code coverage analysis is to use ExcludeFromCodeCoverage attribute. This attribute tells tooling that class or some of its members are not planned to be covered with tests. EditFormModel class shown above can be left out from code coverage by simply adding the attribute.

How code coverage is calculated in JaCoCo?

To calculate the code coverage percentage, simply use the following formula: Code Coverage Percentage = (Number of lines of code executed by a testing algorithm/Total number of lines of code in a system component) * 100.


1 Answers

I was looking for the solution for same test coverage issue with auto-generated code for data model classes and stumbled upon following :
Kotlin + JaCoCo: Tuning Compiler to Skip Generated Code

Solution: Update JaCoCo plugin to 0.8.2 and your issues is resolved.

JaCoCo has solved this very issue in 0.8.2 release, please read the changelog-

  • Classes and methods annotated with runtime visible and invisible annotation whose simple name is Generated are filtered out during generation of report (GitHub #731).
  • Methods added by the Kotlin compiler that do not have line numbers are filtered out during generation of report. Idea and implementation by Nikolay Krasko (GitHub #689).
like image 198
Arpit Avatar answered Nov 15 '22 19:11

Arpit