简体   繁体   中英

android jacoco coverage shows 0% with gradle however there are 95% tests covering code

I'm trying to get jacoco create a code coverage report for my android test project.

Gradle version classpath 'com.android.tools.build:gradle:2.0.0'

I have the following in build.gradle:

apply plugin: 'com.android.application'
apply plugin: 'jacoco'

jacoco {
    toolVersion = "0.7.1.201405082137"
}

android {
    buildTypes {
        release {
        }
        debug {
            testCoverageEnabled true
        }
    }
}

It shows report as below image在此处输入图片说明

There is almost 95% of the code coverage (When I ran the same report in 2015 it showed the report as 95%). Nothing much changed in the code and the test folders ever since then. So ideally it should show coverage as something like this

在此处输入图片说明

I tried running report with both JDK7 & 8 but same result. Also tried changing to latest versions of JaCoCo, but still same result.

Any ideas why the report is shown as 0% coverage? While running the Gradle task it runs my tests in androidTest folder successfully though. 在此处输入图片说明

Facing same issue as mentioned in this question - This question is unanswered yet

I had the exact same problem. But using the comment from Guna (27 Feb 2017), it seems that the problem is caused by running the coverage tests on some Samsung devices.

  • On some newer Samsung devices , when you run the Jacoco gradle task createDebugCoverageReport or createDebugAndroidTestCoverageReport , it will run the unit tests, but show 0% coverage .

  • But on a Google Nexus 5 or most emulators , when you run the same Jacoco task, it will work fine and show the correct coverage. Older Samsung devices also work well.

It's very odd.

Also remember to ensure that all your tests pass first. This is another limitation of Jacoco, because even if one small test fails in your whole suite, it will not generate a test coverage report. Another note: Your app may be automatically uninstalled when the coverage report is generated. Unknown reason why - you'll just have to reinstall it.

Update, 11 October 2018: There is a way to generate a coverage report even if an individual test fails. Use this in your app/build.gradle (fromhere ):

buildTypes {
    debug {
        testCoverageEnabled = true
    }
}

project.gradle.taskGraph.whenReady {
    connectedDebugAndroidTest {
        ignoreFailures = true
    }
}

Automated build scripts may need to use the "--continue" flag, to ensure a unit test failure will continue the gradle task; something like this:

./gradlew createDebugCoverageReport --continue

Further info, see also:

Generation of coverage for your project using command

./gradlew clean createDebugCoverageReport

works just fine:

报告

The only thing that I changed - is compileSdkVersion from 23 to 25 and buildToolsVersion from 23.0.1 to 25.0.2 , because this is the versions that I have.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM