I have been trying to use the new code coverage feature in Android Studio 1.2. There seems to be no documentation for the feature, but so far I figured out to add
testCoverageEnabled true
to the debug flavor of my Gradle file.
Still I can only create code coverage reports for JUnit test cases, not instrumented Android test cases.
Is there any way to generate code coverage for instrumented android test cases ?
To run tests with coverage, follow the same steps as described in Run tests, only instead of clicking Run , click Run test-name with coverage . In the Project window, this option might be hidden behind More Run/Debug.
Computing the source code lines that were executed during the test is done through code coverage. Code coverage requires inserting additional counters into your source code before running it. This step is called instrumentation.
Instrumented tests run on Android devices, whether physical or emulated. As such, they can take advantage of the Android framework APIs. Instrumented tests therefore provide more fidelity than local tests, though they run much more slowly.
Create an Instrumented Unit Test Class To create an instrumented JUnit 4 test class, add the @RunWith(AndroidJUnit4. class) annotation at the beginning of your test class definition. You also need to specify the AndroidJUnitRunner class provided in the Android Testing Support Library as your default test runner.
In the project build.gradle file (root/build.gradle) add url "https://plugins.gradle.org/m2/"
under the buildscript > repositories sections. In my project is looks like this:
buildscript {
repositories {
mavenCentral()
jcenter()
maven {
url "https://plugins.gradle.org/m2/"
}
}
The plugin can be applied in the project build.gradle or (as in my case) to the specific module's build.gradle (module/build.gradle):
apply plugin: 'com.vanniktech.android.junit.jacoco'
Apply the plugin at the very top of the build script before you enter the android
section.
From the Terminal run:
Windows
gradlew.bat connectedCheck
Linux (other)
./gradlew connectedCheck
References:
https://plugins.gradle.org/plugin/com.vanniktech.android.junit.jacoco https://github.com/vanniktech/gradle-android-junit-jacoco-plugin/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With