Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Do I Stop Gradle Instrumented Test Runs From Uninstalling the App?

If I:

  • Create a brand-new Android Studio 3.5.1 project (Kotlin, API 21, "Empty Activity" template)
  • Run the app from inside the IDE
  • Confirm the app is installed and has a launcher icon
  • Run the connectedAndroidDebugTest Gradle task (from inside Android Studio or via gradlew)

The app winds up being uninstalled by the test run. I get that behavior even if I add a testApplicationId value to defaultConfig to have the test code use a different application ID.

How do I stop that behavior? How can I run instrumented tests from the command line, without disturbing an existing app installation?

like image 205
CommonsWare Avatar asked Nov 01 '19 18:11

CommonsWare


Video Answer


2 Answers

The connectedCheck task has the type DeviceProviderInstrumentTestTask. For a simple test run on one device it uses a SimpleTestRunner, which in turn uses a SimpleTestRunnable to actually execute the test. Here you find a structure of

try {
    // connect to device
    // install all APKs
    // run tests
} catch(Exception e) {
    // handle error
} finally {
    // get test report
    // uninstall all APKs
    // disconnect from device
}

I'm not perfectly sure if I've found the most recent implementations, but this exact behavior dates back several years. So I guess you can't achieve what you're asking for.

like image 125
tynn Avatar answered Oct 21 '22 09:10

tynn


Maybe try to run it via adb like this:

adb shell am instrument -w com.android.demo.app.tests/android.support.test.runner.AndroidJUnitRunner

It will not uninstall your app.

here it is described in more details.

like image 27
Pavlo Ostasha Avatar answered Oct 21 '22 11:10

Pavlo Ostasha