1

How can I generate a single .jar file with only my Android Test classes at src/androidTest?

The code I have is working when I run only the test class or the package, but I would like to be able to generate and push the .jar file containing all the tests and execute it via adb shell. (my project doesn't have any app source code)

I'm currently using Android Studio with Gradle.

Thanks

Marcelo
  • 2,245
  • 4
  • 20
  • 24

1 Answers1

2

There is a special gradle task to run tests on connected device or emulator

gradle :module_name:connectedCheck

Test results will be available in ./build/outputs/androidTest-results folder.

But if you want to do it in two separate steps (assemble apk & run it via adb shell), use this command to assemble the apk with tests

gradle :module_name:assembleAndroidTest

Find out the .apk file in ./build/outputs/apk folder of your module. Let install it with command

gradle :module_name:installDebugAndroidTest

or

adb install ./module_name/build/outputs/apk/module_name-debug-androidTest-unaligned.apk

Now start the tests

adb shell am instrument -w -e package <package_with_tests> <package_from_manifest>/android.test.InstrumentationTestRunner

The results appear in STDOUT.

Eugene Krivenja
  • 647
  • 8
  • 18
  • Of course, you can use this solution http://stackoverflow.com/questions/19034466/how-to-create-an-android-library-jar-with-gradle-without-publicly-revealing-sour to generate a .jar file with tests, but I am not sure it is a right way. – Eugene Krivenja Nov 23 '15 at 16:49
  • The apk was generated with the assembleAndroidTest command you gave, but how can I run the tests on the device and have the results? – Marcelo Nov 23 '15 at 16:52
  • To install apk use command `adb install ./module_name/build/outputs/apk/module_name-debug-androidTest-unaligned.apk ` – Eugene Krivenja Nov 23 '15 at 19:50
  • After than run test with command `adb shell am instrument -w -e package /android.test.InstrumentationTestRunner` Your test results appear in STDOUT – Eugene Krivenja Nov 23 '15 at 20:07
  • 1
    I updated my answer to have it complete for all cases – Eugene Krivenja Nov 23 '15 at 20:14
  • 1
    Thank you, it worked! The only difference I had in my case is that I choose to use AndroidJUnitRunner instead of InstrumentationTestRunner, but the approach is the same. This page also helped: http://developer.android.com/tools/testing/testing_otheride.html – Marcelo Nov 24 '15 at 13:10