How to get code coverage using Android Studio?
With the new Android Studio 1.2, you are able to run your unit tests and see the coverage all within the IDE.
First, you'll need to get your unit tests running in the IDE. (if you already can, then skip this step)
This guide and demo will help you.
Secondly, you'll need to create a JUnit Run configuration
Inside this configuraiton, you'll be able to choose
- Test Kind: "All in Package"
- Package: [the package where your tests reside, eg: "com.myapp.tests"]
- Search for tests: Across Module Dependencies (could be diff for your
setup) - VM -options: -ea
- Working Directory: [your project's directory]
- Use classpath of mod: [select your module]
If you have any issue creating your JUnit Run Configuration, you should visit this guide for help.
Lastly, in the latest Android Studio, you should be able to run your JUnit-Run Configuration by clicking on the 'Run with Coverage' button.
In Android Studio 2.1.3 the is label Run Unit tests with Coverage
where Unit test
is the name of your test configuration as shown in the following screenshot:
How to find the code coverage of an android application for manual testing?
Make these changes into the build.gradle file:
apply plugin: 'jacoco'
Also add testCoverageEnabled true
to the debug {} in buildTypes.
I cannot emphasize the importance of testCoverageEnabled, it instruments the files and you won't be able to get coverage without it. Make sure this line is added correctly.
For correct setup of 'build.gradle' check the 'build/intermediates'.
Add read and write external storage permission to the AndroidManifest.xml
In the onDestroy() function of the MainActivity.java add these lines:
Log.d("StorageSt", Environment.getExternalStorageState());
String coverageFilePath = Environment.getExternalStorageDirectory() + File.separator+ "coverage.exec";
File coverageFile = new File(coverageFilePath);
super.onStop();
if(BuildConfig.DEBUG)
{
try {
Class<?> emmaRTClass = Class.forName("com.vladium.emma.rt.RT");
Method dumpCoverageMethod = emmaRTClass.getMethod("dumpCoverageData",coverageFile.getClass(),boolean.class,boolean.class);
dumpCoverageMethod.invoke(null, coverageFile,true,false);
}
catch (Exception e) {}
Run your app and you'll find the coverage.exec in /sdcard/. If the coverage is 37bytes on emulator, try it on a real device or build the APK and drop it into the emulator to install it.
You can then pull the coverage.exec into your computer and generate the HTML report from it using jacoco.
How can I configure the Code Coverage reports in Android Studio 2.3?
You want to do the following:
- Run unit tests:
gradlew testDebug
- Run android tests:
gradlew connectedDebugAndroidTest
- Create code coverage for Android tests:
gradlew createDebugCoverageReport
- After these steps, you can finally combine them using a single task
Apply testCoverageEnabled
:
android {
buildTypes {
debug {
testCoverageEnabled true
}
}
}
Apply includeNoLocationClasses
:
android {
testOptions {
unitTests.all {
jacoco {
includeNoLocationClasses = true
}
}
}
}
Now you can create a task like this one:
apply plugin: 'jacoco'
task jacocoTestReport(type: JacocoReport, dependsOn: ['testDebugUnitTest', 'createDebugCoverageReport']) {
reports {
xml.enabled = true
html.enabled = true
}
def fileFilter = ['**/R.class', '**/R$*.class', '**/BuildConfig.*', '**/Manifest*.*', '**/*Test*.*', 'android/**/*.*']
def debugTree = fileTree(dir: "${buildDir}/intermediates/classes/debug", excludes: fileFilter)
def mainSrc = "${project.projectDir}/src/main/java"
sourceDirectories = files([mainSrc])
classDirectories = files([debugTree])
executionData = fileTree(dir: "$buildDir", includes: [
"jacoco/testDebugUnitTest.exec",
"outputs/code-coverage/connected/*coverage.ec"
])
}
And run it:
gradle clean jacocoTestReport
Source: https://medium.com/@rafael_toledo/setting-up-an-unified-coverage-report-in-android-with-jacoco-robolectric-and-espresso-ffe239aaf3fa
Related Topics
Android - Textview Change Color on Changing of State
Integrating Video File in Android App as App Background
How to Remove Child Nodes in Firebase Android
Vectordrawablecompat Resources$Notfoundexception on Kitkat and Below
Use Dll Files in Android Application
Fcm (Firebase Cloud Messaging) How to Send to All Phones
Android Browser Ignores Responsive Web Design
Black Screen Before Splash Screen Appear in Android
Widgets Don't Respond When Re-Added Through Code
Android: Create a Toggle Button with Image and No Text
Modifying The Color of an Android Drawable
How to Adjust Text Kerning in Android Textview
Why Should We Use Sp for Font Sizes in Android