Kotlin / kotlinx-kover

Apache License 2.0
1.28k stars 48 forks source link

Kover 0.6.0 doesn't work at all on Android #231

Closed NinoDLC closed 1 year ago

NinoDLC commented 1 year ago

I followed the guide on the README for Android (a bit under https://github.com/Kotlin/kotlinx-kover#configuring-jvm-test-task), so I get:

build.gradle (project):

plugins {
    id 'com.android.application' version '7.2.2' apply false
    id 'org.jetbrains.kotlin.android' version '1.7.10' apply false
    id 'com.google.dagger.hilt.android' version "2.42" apply false
    id 'org.jetbrains.kotlinx.kover' version "0.6.0"
}

build.gradle (module):

...
android {
    ...
    testOptions {
        ...
        unitTests.all {
            if (name == "testDebugUnitTest") {
                kover {
                    disabled = false
                }
            }
        }
    }
}

On sync, it gets an exception :

org.gradle.api.internal.tasks.DefaultTaskContainer$TaskCreationException: Could not create task ':app:testDebugUnitTest'
...
Caused by: groovy.lang.MissingPropertyException: Could not set unknown property 'disabled' for extension 'kover' of type kotlinx.kover.api.KoverProjectConfig.

I tried disabled.set(false), isDisabled = false, isDisabled.set(false), to no avail.

enabled = true doesn't produce an exception, but running ./gradlew koverHtmlReport doesn't output anything in the console, nor it creates any files...

I'm quite lost, how do I make Kover 0.6.0 work for Android ?

shanshin commented 1 year ago

Hi, may you clarify which language is used for writing scripts, Groovy or Kotlin?

NinoDLC commented 1 year ago

Groovy !

qwwdfsad commented 1 year ago

This very issue is mentioned in migration guide: https://github.com/Kotlin/kotlinx-kover/blob/main/docs/migration-to-0.6.0.md#type-of-isdisabled-property-changed-from-boolean-to-propertyboolean

You have to use isDisabled.set(true) instead

NinoDLC commented 1 year ago

Hello @qwwdfsad, it seems you answered and closed the wrong issue. I already tried to use isDisabled.set(true) (or false), as I said in my initial message.

qwwdfsad commented 1 year ago

My bad, thanks!

shanshin commented 1 year ago

@NinoDLC, does disabling a task in this way solve your problem? In the module's build.gradle, you need to add

kover {
    // ...
    instrumentation {
        excludeTasks.add "testDebugUnitTest"
    }
}

and delete unitTests.all { ... }

thekie commented 1 year ago

I am having the same problem. @shanshin
I think he wants to enable kover not exclude it. Or do I get your answer wrong?

And when I try your suggestion I get the following error:


Cannot set the value of read-only property 'excludeTasks' for object of type kotlinx.kover.api.KoverProjectInstrumentation.
shanshin commented 1 year ago

@thekie, sorry, this was an example for build.gradle.kts, for Groovy try

kover {
    // ...
    instrumentation {
        excludeTasks.add "testDebugUnitTest"
    }
}
NinoDLC commented 1 year ago

If I put

kover {
    instrumentation {
        excludeTasks.add "testDebugUnitTest"
    }
}

inside my build.gradle (module) and then I run ./gradlew koverHtmlReport, nothing happen. Not a single line is printed in the console, only "BUILD SUCCESSFUL in 791ms".

If you want to check my project (which works with 0.5.0 but won't work at all with 0.6.0), here's the commit I tried for implementing 0.6.0 : https://github.com/NinoDLC/OpenClassrooms_P5_TodoK_Example/compare/master...kover_dot_six_integration

shanshin commented 1 year ago

@NinoDLC, please refer to migration guide in order to migrate to 0.6.0.

For your case applying the plugin to the root project no longer causes it to be recursively applied to all subprojects - you must explicitly apply it to all projects that will be covered: if you want to generate HTML report for app module you sholud apply kover plugin in it.

Also note that merged tasks are no longer created by default.

NinoDLC commented 1 year ago

It works, finally.

For future googlers (and gradle noobs like me 😁), please be advised that :
1/ Android projects are "multi project" (even with only one "app" module), so basically "app" is a subproject. Android is always "multi project" I guess ? 2/ With 0.5.0, for an Android project, you could generate your report with ./gradlew koverHtmlReport. Now, you have to use ./gradlew koverMergedHtmlReport instead. Also, if you re-run this task, it won't print anything to the console anymore. I don't know if it's intended or not. Finally, ./gradlew koverHtmlReport still works but won't apply the filters you put in the project build.gradle. Once again, I don't know if it's intended or not.

Example of Kover migration from 0.5.0 to 0.6.0 on Android available here : https://github.com/NinoDLC/OpenClassrooms_P5_TodoK_Example/commit/2f3046d1f965e3cce9fe3a1765db6538fd742272

shanshin commented 1 year ago

Finally, ./gradlew koverHtmlReport still works but won't apply the filters you put in the project build.gradle. Once again, I don't know if it's intended or not.

The task is created in each project (module) in which the plugin is applied. Filters from one module do not affect reports in the other. If your tests are located in the app module, then you need to add filters to the file build.gradle of this app module

Also, if you re-run this task, it won't print anything to the console anymore.

When restarting, the task is not executed, and the report is taken from the build cache, so a message is not printed to the console that the report has been generated. Perhaps you should always print such a message.

NinoDLC commented 1 year ago

For the record, here's the kover migration 0.5.0 to 0.6.0 in gradle.kts style : https://github.com/NinoDLC/OpenClassrooms_P5_TodoK_Example/commit/e300478e02a1ee0b518778f87c5a2d6138997fa6