1

Short version:

1) Upgrade your Android Test Support Library (ATSL, currently version 0.4.1). This is the key point for 0.3.0 is buggy.

    androidTestCompile 'com.android.support.test:runner:0.4.1'
    // Set this dependency to use JUnit 4 rules
    androidTestCompile 'com.android.support.test:rules:0.4.1'
    // Set this dependency to build and run Espresso tests
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1'
    // Espresso-contrib for DatePicker, RecyclerView, Drawer actions, Accessibility checks, CountingIdlingResource
    androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.2.1'

2) Set testCoverageEnabled in debug closure

    debug{
        testCoverageEnabled true
    }

3) Run ./gradlew tasks you will see a task named createDebugAndroidTestCoverageReport. That is exactly the task to generate the report.

4) Run ./gradlew createDebugAndroidTestCoverageReport. Find the reports in app/build/reports/coverage/debug/index.html. Voila!

Long version:

It is late October 2015 and android test tools are rapidly improving. Code coverage is a commonly used method to assist test case writing. But when you search the Google "android test coverage" or "android jacoco" you can hardly find a working solution.

The reason is that android have provided the support for the coverage report in gradle build system long time ago, BUT it broke afterwards. So you can find a demo in github https://github.com/srideviaishwariya/Automation-Tools-for-Android/tree/master/Sample1. The codes is using out-dated android build tools which means, you copy the code and use the latest android build tools and everything goes wrong.

Oleksandr Kucherenko wrote a post detailing how the android test coverage failed and a possible workaround in Jun 2015. At that time the latest android-support-test-runner version is 0.3.0.

The test coverage bug was not fixed until android team announced an update of Android Test Support Library in Sep 2015. The post itself only mentioned the jacoco bug is fixed and nothing more, leaving the google search results are still presenting the old wrong way or work arounds with old test runners.


凤梨豪
437 声望7 粉丝

杭州树洞网络科技 CTO