Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run UI automation tests with gradle without uninstalling

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?

like image 458
Tore Olsen Avatar asked Jun 10 '15 12:06

Tore Olsen


People also ask

How do I run a test using Gradle command?

Use the command ./gradlew test to run all tests.

What is espresso testing in Android?

The Espresso Test Recorder tool lets you create UI tests for your app without writing any test code. By recording a test scenario, you can record your interactions with a device and add assertions to verify UI elements in particular snapshots of your app.

In which directory does Android Studio Place UI tests Why do they need to be there?

Instrumented UI tests in Android Studio To run instrumented UI tests using Android Studio, you implement your test code in a separate Android test folder - src/androidTest/java . The Android Plug-in for Gradle builds a test app based on your test code, then loads the test app on the same device as the target app.


1 Answers

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}
like image 100
Tore Olsen Avatar answered Oct 02 '22 22:10

Tore Olsen