Home » Android » android – Run connectedAndroidTest and skip uninstall

android – Run connectedAndroidTest and skip uninstall

Posted by: admin June 15, 2020 Leave a comment

Questions:

Is there a way to call the task connectedAndroidTest and skip the uninstall task at the end of the process ?

At the end of the test execution, the app is uninstalled from the device, but I would like to keep the app on the device.

from http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Running-tests :

As mentioned previously, checks requiring a connected device are launched with the anchor task called connectedCheck. This depends on the task connectedDebugAndroidTest and therefore will run it. This task does the following:

  • Ensure the app and the test app are built (depending on assembleDebug and assembleDebugAndroidTest).
  • Install both apps.
  • Run the tests.
  • Uninstall both apps.
How to&Answers:

Looking at the sorce of gradle plugin there is no way to prevent uninstalling app at the end of test task. You can check that in SimpleTestCallable class of android gradle plugin.

From what i see there are two options to acchive what you want.

First one is to reinstall app after your connected check is done. Command to do that would look something like this. ./gradlew connectedCheck installDebug installDebugAndroidTest This will execute test on device and delete apps from it. But after that it will reinstall app and test app. So app will still be removed and then installed which means a bit of owerhead but at least apps will not be recompiled twice since you are executing in same gradle execution.

Second option is to not use gradle for executing tests but use adb instead.
To do this you first need to install app and test app through gradle.
./gradlew installDebug installDebugAndroidTest

After that you can execute tests through adb. by caling adb shell am instrument -w com.example.test/android.support.test.runner.AndroidJUnitRunner.

When this is done you can run your cli tests since both app and test app are still installed.

With second approach you would lose all the benefits of executing test wit gradle. Such as code coverage and executing in multiple proceses, etc.