Unit Testing with Android
There is one word that is guaranteed to generate excitement in mobile development communities right now - Android. Google’s venture into the mobile SDK realm was eagerly anticipated, and its release, paired with a development challenge, didn’t disappoint. Now however, as the freshness wears off and serious development begins to become more commonplace, Android’s first real shortcoming becomes clear. Unit Testing…
At first glance it looks like unit testing is easy in Android. Indeed, the SDK includes JUnit, and even extends it into a testing package. Being the Test Driven Development aficionado that I am, I of course tried the testing package out when I made my first “Hello World” android application. And that is when I found out that creating the Android UI objects, such as the Bundles and Activities, in a test context is pretty much impossible. Documentation for using the testing package is non-existent, another un-Google=like characteristic of Android. Searching through the various development forums tells me that I am not the only one having problems.
All is not lost however. First, there is always the tried and true method of moving business functionality out of the UI layer and into a seperate service layer, which is easier to test. And then there are third party libraries than have begun to spring up in development circles. Positron has emerged as the most popular. Positron contains a method called startActivity() that allows you to use Android’s libraries to start and initialize the activity in a testing context. More importantly, it also has documentation that supplies instructions and examples of how to use it. So until someone out there figures out how to use Android’s internal testing package, I will be using Positron.

Comments
Paul Bourdeaux, thanks for the information, i spent almost 2 weeks behind Android testing. Positron might be the solution i should look into.
I hope that Positron is still a viable solution for you. When I wrote this blog, Positron was originally designed for the original release of Android, and there were problems using it in 1.5 (Cupcake). Now that Android is up to 2.1 (Éclair - or Flan depending on who you believe), I am not sure that Positron is still compatible.
Here’s a screencast I’ve made to demonstrate how to create and run Unit Tests for your Android Project
http://www.gubatron.com/blog/2010/05/02/how-to-do-unit-testing-on-android-with-eclipse/
Leave A Comment