Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inline functions are causing errors in unit test code coverage report

I'm writing some unit tests for my Android app written in Kotlin, and I'm getting errors from the included inlined collection functions, in this case specifically sortedBy

 override fun onDaysSelected(dayOfWeekList: ArrayList<DayOfWeek>) {
        view.userRoutingRule.days = dayOfWeekList.sortedBy { it.dayOfWeek }
        renderRule()
 }

This is the error I'm getting when I run my unit tests with coverage

---- IntelliJ IDEA coverage runner ---- sampling ... include patterns: com.mypackage..* exclude patterns:[2019.02.02 14:49:40] (Coverage): Class data was not extracted: com.mypackage\myfile$onDaysSelected$$inlined$sortedBy$1: java.lang.Throwable

Process finished with exit code 0

My unit tests all pass accordingly, but when I go to look at the coverage report, it's almost completely bare as this error stopped it from completing.

Is there any solution to this at this point? It's hard to know if I missed some condition if I can't just look at the report.

like image 914
Ben987654 Avatar asked Feb 02 '19 22:02

Ben987654


People also ask

What is line coverage in unit testing?

Line coverage reports on the execution footprint of testing in terms of which lines of code were executed to complete the test. Edge coverage reports which branches or code decision points were executed to complete the test. They both report a coverage metric, measured as a percentage.

What should I exclude 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.

What should be the unit test coverage?

Aim for 95% or higher coverage with unit tests for new application code. When developers unit test as they program, they improve the longevity and quality of the codebase. The time a development team invests in unit tests pays off with less time spent troubleshooting defects and analyzing problems later.


1 Answers

It's a known issue with calculating coverage of imline methods that use lambdas. (see No coverage report for inlined Kotlin methods and https://discuss.kotlinlang.org/t/inline-functions-coverage/5366)

If you want to run coverage in the current state, you'll need to use other approaches, for example implementing Comparable interface in your DayOfWeek and using .sorted() method.

like image 121
TpoM6oH Avatar answered Nov 01 '22 18:11

TpoM6oH