androidgradleandroid-gradle-pluginautomated-tests

Run connectedAndroidTest and skip uninstall


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.

Solution

  • Looking at the source of the gradle plugin, there is no way to prevent uninstalling the app at the end of the test task.

    You can check that in the SimpleTestCallable class of the android gradle plugin.

    From what I see there are 2 options to achieve what you want.

    Option 1

    Reinstall the app after your connectedCheck is done.

    Command to do that would look something like this:

    ./gradlew connectedCheck installDebug installDebugAndroidTest
    

    This will execute the tests on the device and delete the apps from it.

    But after that it will reinstall the app and the test builds.

    So app will still be removed and then installed, which means a bit of overhead, but at least apps will not be recompiled twice since you are running the tasks as part of the same gradle execution.

    Option 2

    This time we do not use gradle for executing the tests but adb instead.

    To do this, you first need to install the app and test builds through gradle:

    ./gradlew installDebug installDebugAndroidTest
    

    After that, you can execute tests through adb, by calling:

    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 tests with gradle. Such as code coverage, executing in multiple processes, etc.