トラッキング コード

2/09/2015

How to use Code Coverage at Android Studio

How to set the Code Coverage in build.gradle

We need to add "testCoverageEnabled". The following build.gradle is sample.

android {
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }

+        debug {
+            testCoverageEnabled = true
+        }
    }
}

How to excute Code Coverage

We need to excute Code Coverage from terminal.

./gradlew createDebugCoverageReport

When excute Code Coverage, automatically Gradle run the androidTest.
If not connected device(or Emulator), you shold show the following error.

:app:connectedAndroidTest FAILED          
              
FAILURE: Build failed with an exception.
              
* What went wrong:
Execution failed for task ':app:connectedAndroidTest'.
> com.android.builder.testing.api.DeviceException: java.lang.RuntimeException: No connected devices!

We need to connect device or wake up Emulator.

How to show the Code Covoerage Report

If successed createDebugCoverageReport、Gradle saved report in the following path.
[PROJECT]/app/build/outputs/reports/coverage/debug/index.html

We can investigate the none test Line.




1 comment: