Android – Run android unit tests & instrumentation tests on Jenkins (Gradle)

androidandroid-gradle-plugingradlejenkinsunit-testing

I have some unit and instrumentation tests in my androidTest folder.

I'm trying to run these tests on my local Jenkins.

I've successfully configured my project with Jenkins and I've also created an emulator for the instrumentation tests.

So far all the resources I've seen only focus on ant and earlier versions of Gradle. The syntax for writing tasks has slightly changed in the current version and I'm confused about this.

Here is my build.gradle file:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    defaultConfig {
        applicationId "myPackageName"
        minSdkVersion 16
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}


dependencies {
    compile 'com.android.support:appcompat-v7:21.0.3'
    compile 'com.google.android.gms:play-services:6.5.87'
}

Best Solution

You can write a jenkis script like this:

stage('Build & Install') {
//Build the apk and the test apk which will run the tests on the apk
                sh 'chmod +x gradlew && ./gradlew --no-daemon --stacktrace clean :app:assembleDevDebug :app:assembleDevDebugAndroidTest'
            }

 stage('Tests') {
//Start all the existing tests in the test package 
sh './gradlew --no-daemon --debug :app:connectedDevDebugAndroidTest' 
}

This should install the apk and the test apk into the device and start the test cases in the test apk on installation.

Here I have given DevDebug as I have a flavor constant called Dev and build type is Debug. If you don't have any of those, you don't use them.

Related Question