Running Android Tests on a Device or Emulator

I have not been doing much web development lately, so its been difficult to come with interesting topics. However, I have been doing a lot of android development and since many engineers have to work cross discipline, I think an android article be relevant. This article will discuss how to run unit tests against your android code, directly on the android device or emulator.

Getting ready

You will need to install the Android SDK.

The SDK includes ADB (Android Debug Bridge), which is used to access the android device and run tests.

Lastly, you need an android device in developer mode (Settings -> About -> click on Build number repeatedly until developer mode is enabled) or to have setup an emulator.

Examples in this article will use an emulator. I named my emulator android_4.4.2.

How do it…

Start the emulator:

cd <pathToAndroidSdk/tools>
./emulator -avd android_4.4.2 -shell-serial tcp::4444,server,nowait

You can check the name of your emulator by running:

$ adb devices
List of devices attached
emulator-5554	device

Connect to the device for fun and profit:

adb -s emulator-5554 shell
# futz around
exit

Install your latest APK onto the device:

adb -s emulator-5554 install -r <path to your APK>

To run tests on the device:

adb -s <emulator name> shell am instrument -w -e <path to test file> <test package>:<test runner>
# Here is an example from GmsCore
adb -s emulator-5554 shell am instrument -w -e class com.google.android.gms.games.broker.PlayerAgentTest com.google.android.gms.test/android.support.test.runner.AndroidJUnitRunner

To connect a debugger, use:

adb -s <emulator name> shell am instrument -w -e debug true -e <path to test file> <test package>:<test runner>
# Here is an example from GmsCore
adb -s emulator-5554 shell am instrument -w -e debug true -e class com.google.android.gms.games.broker.PlayerAgentTest com.google.android.gms.test/android.support.test.runner.AndroidJUnitRunner

How it works…

Following these instructions you can run any tests from the command-line on your android device or emulator. The adb service will connect to the device and run the tests directly on the device, giving you the most realistic environment for executing test code. You do need to ensure that the latest APK for your application and test code are copied to the device before running the test, or you may see unexpected discrepancies.

For instructions on how to best write tests, please read Android Testing, especially the Activity Testing tutorial.

Session Driven Back Buttons

One of the biggest issues I encountered while refactoring my projects to work without the need of JavaScript, is directing the user back to the page they were previously on. This is especially important for form submission and canceling. I look around the web and mostly saw comments directing programmers to use JavaScript to handle back. However, the point of the exercise is to not require JavaScript for anything. I then spent some time looking ...