Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I run test before generate signed apk?

I consider that Android Studio will run test before generate signed apk.

But AS didn't do that for me.It's not nice before package my apk, I need run the tests myself.

I'm not sure if dependsOn or other way can help me.I'm not sure weather my build.gradle has mistakes.

Some relevant code in gradle maybe like this:

defaultConfig {
    applicationId "com.xx.xx"
    versionCode getDefaultVersionCode()
    minSdkVersion 19
    targetSdkVersion 19
}
dependencies {
    testCompile 'org.robolectric:robolectric:3.0'
    testCompile 'junit:junit:4.12'
}

I didn't write testOption.

My directory is like this(content before them is package name):

enter image description here

like image 270
user2545386 Avatar asked Jul 04 '16 03:07

user2545386


1 Answers

To run all available tests, when building a release, make the task, that builds the release (e.g. assembleRelease) depend on the test tasks:

android {
    // ...
}
afterEvaluate {
    assembleRelease.dependsOn testReleaseUnitTest, connectedAndroidTest
}

The afterEvaluate closure is executed after evaluation (when the android tasks have been created). At this time the android tasks can be referenced as variable.

Instead of testReleaseUnitTest you can just use test, which runs unit tests for all variants.

Keep in mind that there are by default no instrumentation tests for the release version of your app (build with assembleRelease). So in the above example, connectedAndroidTest runs the instrumentation tests for the debug version only.

like image 97
thaussma Avatar answered Dec 08 '22 09:12

thaussma