we are building an Android app which is tested by using Appium. Now I would like to see the test coverage of our Appium tests. I think this is possible, because Jacoco suppo
I solved this problem by adding broadcast receiver to the application you test! (you can add the receiver only to debug folder cause no need it for to exist in main source)
public class CoverageReceiver extends BroadcastReceiver {
private static final String EXEC_FILE_PATH = "/mnt/sdcard/coverage.exec";
private static final String TAG = "CoverageJacoco";
private static final String BROADCAST_RECEIVED_MESSAGE = "EndJacocoBroadcast broadcast received!";
private static final String EMMA_CLASS = "com.vladium.emma.rt.RT";
private static final String EMMA_DUMP_METHOD = "dumpCoverageData";
@Override
public void onReceive(Context context, Intent intent) {
try {
Log.d(TAG, BROADCAST_RECEIVED_MESSAGE);
Class.forName(EMMA_CLASS)
.getMethod(EMMA_DUMP_METHOD, File.class, boolean.class,
boolean.class)
.invoke(null, new File(EXEC_FILE_PATH), true,
false);
} catch (Exception e) {
Log.d(TAG, e.getMessage());
}
}
}
In manefist add (you can add this debug folder so it won't exist in main source)
In the build.gradle of the application I added
apply plugin: 'jacoco'
jacoco {
toolVersion = "0.7.4+"
}
model {
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "com.example.app"
minSdkVersion.apiLevel 23
targetSdkVersion.apiLevel 23
versionCode 12
versionName "1.11"
}
buildTypes {
debug {
testCoverageEnabled true
}
}
you build your application as debug, than install and run it.
send broadcast through ADB "adb shell am broadcast -a com.example.action" to create coverage.exec pull coverage from device - adb pull /mnt/sdcard/coverage.exec
after you run this you need to create the coverage from the file
**
* This task is used to create a code coverage report via the Jcoco tool.
*/
task jacocoTestReport(type: JacocoReport) {
def coverageSourceDirs = [
'src/main/java',
]
group = "Reporting"
description = "Generates Jacoco coverage reports"
reports {
csv.enabled false
xml{
enabled = true
destination "${buildDir}/jacoco/jacoco.xml"
}
html{
enabled true
destination "${buildDir}/jacocoHtml"
}
}
classDirectories = fileTree(
dir: 'build/intermediates/classes',
excludes: ['**/R.class',
'**/R$*.class',
'**/BuildConfig.*',
'**/Manifest*.*',
'**/*Activity*.*',
'**/*Fragment*.*'
]
)
sourceDirectories = files(coverageSourceDirs)
executionData = files('build/coverage.exec')
}
this task is one way to create coverage files in coverageSourceDirs add all the locations of your applicaiton source code, so it will know which code to take and create coverage based on them executionData is the location where you put the coverage.exec you pulled from the device
Run the task the files will created for html and xml you can also add csv (notice it will be create in the build folder of the application)!
Need to know, you must run the task against the same code you built your application debug version