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.

Cherry-Picking for Fun and Profit

In Git, it is often useful to merge one specific commit from one branch into another. Frequently this happens when Using Git Interactive Rebase With Feature Branches, as you develop in the branch, you realize that one or more of your commits should be added to master right away, but not all the CLs. Enter git cherry-pick <changelist hash> for the win! The cherry pick command allows you to merge one CL from a ...

Gaming Engine - Snake Demo v2

Last week I was busy at GDC and have not had time to put together a detailed article, so in the spirit of GDC, I thought I would share the latest iteration of my HTML5 gaming engine (still very rough). There has been a lot of progress around the Game class to support stages (or levels) and a score board to track the player’s score. The stages are demoed by a new version ...

Use && Instead of Semicolon to Separate Commands

Today’s article will be short and will cover a bash topic that frustrates me to no end. Please don’t use ; to separate commands, when you mean &&. There is an important difference between the two and many developers never realize that they want to be using && in their scripts.

How do it…

Here is a common oneliner that you might use to compile a package:
 ./configure ; make ; make ...

Using Promises to Cache Static AJAX JSON Data

This article showcases a useful caching strategy for static data that is fetch via AJAX. We will use jQuery to setup a promise and cache the data in the localStorage for subsequent page loads or data loads.

Getting ready

A modern web browser supporting localStorage and JSON. Also, a basic understanding of promises[2] is helpful.

How do it…

Here is the code:
 (function($) { var oKeyDeferredMap = {}; function fnReadData(sKey) ...

Connecting to Github and EC2 Through a Proxy on Port 80 or 443

Today we’ll cover how to connect to github and EC2 through a draconian proxy allowing only port 80 and 443. Github uses SSH, so like EC2 it can be connected to using SSH tunnelling. This article is based on a blog post by tachang[1], which needed some additional explanation and changes to work behind my proxy. I will be explaining how to connect on a unix-based machine, but these settings should also work on ...

Introducing Gaming Engine - Snake Demo v1

In my not so copious spare time over the past few months, I’ve been working on a game engine to power two dimensional board-based games. The engine has a long way to go, but I have reach the first demo milestone and wanted to share it with you. Here is a basic version the snake game written using the game engine. It illustrates a working main thread, responsiveness to keyboard commands, interaction between a ...

jQuery Function for Change Event and Delayed Keydown Event

In my experience, it is rare to assign only a change event to a text input, as any callback that would be executed for the change event should also be called on a key event as well, but with a slight delay (think how an autocomplete shows results as you type). This is a common pattern and I was surprised to not immediately find a jQuery plugin implementing it, so I decided to add one ...

Merge Sort

Continuing to evaluate efficient sorting algorithms, today we’ll look at merge sort. Merge sort[1] is a comparison sort using a divide and conquer algorithm, developed by John von Neumann[2] in 1945. It recursively divides the list into smaller sublists of length one, then repeatedly merges the sublists in order until there is only one sublist left. It has a worst case runtime of (O(nlogn)), making it worst-case more efficient than Quicksort.

Quicksort

We’ve looked a variety of in-efficient sorting algorithms, today we’ll look at Quicksort (aka. partition exchange sort), as a first foray into faster and more frequently used sorting algorithms. Quicksort[1] is a comparison sort using a divide and conquer algorithm, developed by Tony Hoare[2] in 1960. It recursively divides the list into smaller lists around a pivot value and sorts them, which means much smaller data sets when actually sorting. It has a ...