As Android approaches maturity with its recent
0.9r1
release, I began to think its time that my main project updates to match. I decided to take another look at Android's instrumentation and unit testing features to build robust tests for my critical services and activities. Unfortunately, there isn't anything new in the way of documentation however there is now is an example we can use in ApiDemos.
Looking at the new ApiDemos we see that there is a new
tests
directory which contains a second
AndroidManifest.xml
with no user activities defined. This is important as this structure allows us to have two separate APKs, one for production and one for testing, so our main distribution and build doesn't need to be polluted. Unfortunately, this approach is not compatible with the Eclipse plugin, and so we must define everything in an external build environment.
For convenience, I packaged the complete ApiDemos project with my Ant build environment here:
ApiDemos-instrumentation-build.tar.gz. To build, start the emulator and run the following commands:
$ adb shell rm /data/app/ApiDemos.apk
$ ant install
$ cd tests && ant install
It is essential above that you remove the old ApiDemos.apk as the instrumentation APK must be signed with the same key as the package it tests.
Once we've got both APKs installed we can begin running our unit tests. There is no UI in Android for this, however it can be invoked more conveniently through adb as such:
$ adb shell am instrument -w com.android.samples.tests/android.test.InstrumentationTestRunner
This will run all defined tests and print a dot character for each successful test, F for failure, or E for invocation error. See the javadoc in
tests/src/com/android/samples/AllTests.java
for more sample command lines to run individual test cases or specific tests within them.
My main project,
Five, has recently been updated to include unit tests in the five-client component, with more test coverage to follow in the next few weeks. For a non-trivial test case, see my
CacheServiceTest.
NOTE: Previously, I was using the
masa Android plugin for Maven, however it has not yet been updated to support
0.9r1
. I feel that using Maven for this would have been cleaner, but the Ant approach is sufficient for this simple demonstration. Once updated, I will return here and finish this example with a more sophisticated build environment to automate testing.