vanniktech / gradle-android-junit-jacoco-plugin

Gradle plugin that generates JaCoCo reports from an Android Gradle Project
http://vanniktech.com
Apache License 2.0
399 stars 72 forks source link

Task combinedTestReport not showing instrumented coverage #191

Open dsuresh-ap opened 3 years ago

dsuresh-ap commented 3 years ago

I feel like this might be related to https://github.com/vanniktech/gradle-android-junit-jacoco-plugin/issues/138

Running the combinedTestReport command generates the jacocoCombined report directory and files, however it does not contain any coverage on instrumented tests.

However if I check reports/coverage/**/ (generated by the createDebugCoverageReport task) I see the instrumented coverage there. It seems like the plugin task create the combined report isn't working.

I am using the 0.17 snapshot.

dsuresh-ap commented 3 years ago

I think I found the issue. Not sure if its because of how our project is setup but I had to change the logic (also migrated the plugin to Kotlin)

if (combined) {
    // add instrumentation coverage execution data
    doFirst {
        def instrumentationTestCoverageDirs = subProject.fileTree("${subProject.buildDir}/outputs/code_coverage")
                .matching { include "**/*.ec" }
        def allCodeCoverageFiles = instrumentationTestCoverageDirs.files + executionData.files
        subProject.logger.with {
            info("using following code coverage files for ${taskName}")
            allCodeCoverageFiles.each { coverageFile ->
                info(coverageFile.path)
            }
        }
        executionData.setFrom(allCodeCoverageFiles)
    }
}

to (in Kotlin)

if (combined) {
    executionData.setFrom(
        subProject.fileTree(
            mapOf(
                "dir" to project.buildDir,
                "includes" to listOf(
                    "jacoco/test${sourceName.capitalize()}UnitTest.exec",
                    "outputs/code_coverage/${sourceName}AndroidTest/connected/**/*.ec"
                )
            )
        )
    )
}

For some reason executionData.setFrom(allCodeCoverageFiles) does not work with the current logic. Running combinedTestReport<> --info shows the following logs. Note the missing of .ec files. With my modified logic I see both coverage files.

[ant:jacocoReport] Loading execution data file s/app/build/jacoco/testDevelopmentDebugUnitTest.exec
[ant:jacocoReport] Loading execution data file s/app/build/outputs/code_coverage/developmentDebugAndroidTest/connected/android_emulator_30(AVD) - 11-coverage.ec

but with the plugin I only see ANT loading the .exec file.

vanniktech commented 3 years ago

Personally, I've never used the combine task. Do you care to submit a PR?

dsuresh-ap commented 3 years ago

I can but not anytime soon since the project is setup to use the re-written plugin.

The change is really simple as you can see in my previous post but I am not sure if there is a reason why the original code does the following:

def instrumentationTestCoverageDirs = subProject.fileTree("${subProject.buildDir}/outputs/code_coverage")
     matching { include "**/*.ec" }
def allCodeCoverageFiles = instrumentationTestCoverageDirs.files + executionData.files
executionData.setFrom(allCodeCoverageFiles)