Jacoco shows 0% coverage for Kotlin's data classes. How to configure it to measure coverage or ignore data classes at all?
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.
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.
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.
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.
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-
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With