4

I have some android instrumented tests under AndroidTests directory in Android Studio. Of course I can manually execute the tests but I need to execute all the tests in the suite after each build ("release" type of build, not during normal debug build). I need to do that because I'd like to validate the new code against the tests before releasing the new app's apk. How to do that? I google it but I didn't find a proper solution yet.

Any idea?

Daniele
  • 1,332
  • 1
  • 13
  • 24

3 Answers3

12

Finally I managed to do that. I have added the following configuration to my build.gradle file in my android studio project:

// execute android tests before realising a new apk
tasks.whenTaskAdded { task ->
    if (task.name == 'assembleRelease')
        task.dependsOn('connectedAndroidTest')
}

android {
signingConfigs {
    release {
        keyAlias 'key'
        keyPassword 'password'
        storeFile file('/path/to/release_keystore.jks')
        storePassword 'password'
    }

}
buildTypes {
    debug {
        signingConfig signingConfigs.release
    }
    release {
        signingConfig signingConfigs.release
    }
}

After doing that I'm able to run all android tests when building a release apk. If the tests are failing no apk is built.

Daniele
  • 1,332
  • 1
  • 13
  • 24
  • connectedAndroidTest will build and test debug apk not your release one. It could be important if you are using minifyEnabled true for your release. Did you find the way to make it test your original release apk which you intended to deploy? E.g. using testBuildType "staging". – engilyin Mar 18 '18 at 09:35
  • Does this supposed to work when I select Build -> Generate Signed APK/AAB? I tried it but no tests are run at all. – netcyrax May 22 '20 at 10:12
3

In general you can define a dependencies for the task using the dependsOn method.

For example:

task A << {
    println 'Hello from A'
}

task B << {
    println 'Hello from B'
}

B.dependsOn A

You will obtain

> gradle -q B
Hello from A
Hello from B

In your case you can specify:

assemble.dependsOn test

If you would like to specify a dependency only for the release build:

assembleRelease.dependsOn test

Use:

connectedAndroidTest to run the tests on a connected emulator or device.
test to run the unit test on your local host.

Graham
  • 7,431
  • 18
  • 59
  • 84
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
  • yeahh I think something like that but what is "test"? Is not recognized by gradle. Do I need to specify it somewhere? – Daniele Sep 15 '16 at 13:46
  • @Daniele use test to run the unit test or connectedAndroidTest to run the android tests – Gabriele Mariotti Sep 15 '16 at 16:02
  • ok I added this: tasks.whenTaskAdded { task -> if (task.name == 'assembleRelease') task.dependsOn('connectedAndroidTest') } and now it seems to work but the problem is that no tests are executed die to the signing certificate issue. the problem is that that connectedAndroidTest runs test against a "build" type not a "release" type of build. Found more info here: http://stackoverflow.com/questions/23923218/android-gradle-running-tests-on-non-debug-builds. Not clear what to do now. – Daniele Sep 15 '16 at 16:15
  • This message means that the same app is just installed in the device but signed with another certificate (probably as you are trying to install the release version, there is the app signed by default with the debug certificate) – Gabriele Mariotti Sep 19 '16 at 19:13
0

I think you should dive in one certain topic: Continuous Integration.

This would allow you to run tests suites after every build or commit, build variants, publish on Google Play and much more... Start with classic Jenkins CI, which may not be the most polished and easy to use, I mean user-friendly tool, but in compare to Travis or Circle CI it provides huge configuration possibilities, nice app community and it's free to use.

Start with this article: http://www.vogella.com/tutorials/Jenkins/article.html

Other tools: I'm already using Travis for my Github projects, but Circle CI or Green House CI might be also good choice.

Hope it will help

piotrek1543
  • 19,130
  • 7
  • 81
  • 94
  • yes you're right but I don't need for now that because I'm simply developing a simple app. I don't need CI for now, I'm just a solo developer. Thanks for the references anyway but I'm looking for something different, I think I'm asking something that should have an easy answer but maybe not. :-) – Daniele Sep 15 '16 at 15:54
  • Jenkins is great open-source tool and great skill if you would mind changing a job. I'm already Travis using for my really small projects, check my github, nick is the same as my username – piotrek1543 Sep 15 '16 at 15:58