Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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'
}
like image 695
AndroidEnthusiast Avatar asked Jan 21 '15 11:01

AndroidEnthusiast


People also ask

How do I run unit test on Android?

To run all tests in a class or a specific method, open the test file in the Code Editor and do either of the following: Press the Run test icon in the gutter. Right-click on the test class or method and click Run . Select the test class or method and use shortcut Control+Shift+R .

What is Android unit testing?

Unit tests or small tests only verify a very small portion of the app, such as a method or class. End-to-end tests or big tests verify larger parts of the app at the same time, such as a whole screen or user flow. Medium tests are in between and check the integration between two or more units.

Where do unit tests go Android?

The following is the default directory structure for your application and test code: app/src/main/java - for your source code of your main application build. app/src/test/java - for any unit test which can run on the JVM. app/src/androidTest/java - for any test which should run on an Android device.

How do you run a unit test?

To run all the tests in a default group, choose the Run icon and then choose the group on the menu. Select the individual tests that you want to run, open the right-click menu for a selected test and then choose Run Selected Tests (or press Ctrl + R, T).


1 Answers

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.

like image 155
Dinesh Avatar answered Oct 27 '22 06:10

Dinesh