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

3
  • Execute .jar via adb shell? Maybe you meant .apk with tests? Commented Nov 23, 2015 at 16:04
  • 1
    From the specs I've found, you can run your jar file inside the cellphone with adb shell uiautomator runtest File.jar Commented Nov 23, 2015 at 16:13
  • Probably related: stackoverflow.com/questions/17218295/… Commented Nov 23, 2015 at 16:53

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Of course, you can use this solution stackoverflow.com/questions/19034466/… to generate a .jar file with tests, but I am not sure it is a right way.
The apk was generated with the assembleAndroidTest command you gave, but how can I run the tests on the device and have the results?
To install apk use command adb install ./module_name/build/outputs/apk/module_name-debug-androidTest-unaligned.apk
I updated my answer to have it complete for all cases
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: developer.android.com/tools/testing/testing_otheride.html
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.