Run UI automation tests with gradle without uninstalling

3.4k views Asked by At

When I run instrumentation tests from within Android Studio, I see that the app remains on the device afterwards. But I can't figure out to do this from the command line with gradlew. My intention is to run tests that save screenshots in e.g /data/data/MyApp/cache/screenshots and download these with adb pull afterwards.

./gradlew connectedAndroidTest

causes the app to be uninstalled. I also tried

./gradlew connectedAndroidTest -x uninstallAndroidTest

but that didn't make any difference. What's causing the uninstallation, and how can I avoid it?

2

There are 2 answers

2
Tore Olsen On BEST ANSWER

I solved this by letting gradle only build the apk, and then handling the install/test/uninstall work with adb. Here's an approximation of my script.

PKGNAME=com.corp.app
./gradlew assembleAndroidTest
adb install -r app/build/outputs/apk/app-debug.apk
adb install -r app/build/outputs/apk/app-debug-androidTest-unaligned.apk

adb shell am instrument -w ${PKGNAME}.test/android.support.test.runner.AndroidJUnitRunner

[ -d screenshots ] || mkdir screenshots
adb pull /data/data/${PKGNAME}/cache/screenshots screenshots

# Now we can uninstall.
adb uninstall ${PKGNAME}.test
adb uninstall ${PKGNAME}
1
Alexey On

I didn't find the way out of this problem. Looks like there is no way to run instrumentation test without uninstalling. You can use gradle commands to build and install your app and testApp. It is a better way to use these commamds because apps will be installed on all connected devices.

gradlew installVersionDebug
gradlew installVersionDebugAndroidTest
adb shell am instrument -w -r -e debug false -e class com.example.android.EspressoUITest {PKGNAME}.test/android.support.test.runner.AndroidJUnitRunner

But there is still a problem with reports. You can use custom testRunner to generate a JUnit style XML report. Such report could be converted to HTML format with common approach.