Monday, August 25, 2008

Android Instrumentation Example

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.

5 comments:

Diego Torres Milano said...

I've published a post featuring a simple way to build and run ApiDemos
tests not needing ant or maven. This method can also be used to build
and run tests for other projects as well.
See Android: Testing on the Android platform - ApiDemos tests.

Comments are gladly welcome.

Grace Hu said...

Hi,when i build my android applicaion use ant, it give me a error of create the .apk file.The detail is as below:
compile:
[javac] Compiling 2 source files to D:\testbuild\bin\classes
dex:
[echo] Converting compiled files and external libraries into bin/classes.dex...
package-res:
package-res-and-assets:
[echo] Packaging resources and assets...
debug:
[echo] Packaging bin/TestBuild-debug.apk, and signing it with a debug key...
[exec] Unable to access jarfile ..\framework\apkbuilder.jar

BUILD FAILED
D:\testbuild\build.xml:210: exec returned: 1

So, could you help me?

Grace Hu said...

Hi,when i build my android applicaion use ant, it give me a error of create the .apk file.The detail is as below:
package-res-and-assets:
[echo] Packaging resources and assets...
debug:
[echo] Packaging bin/TestBuild-debug.apk, and signing it with a debug key...
[exec] Unable to access jarfile ..\framework\apkbuilder.jar
So, could you help me?

Unknown said...

Hi,

When We execute the tests its executing twice....

If I want to perform some activity after completeion of all tests when I need to do...

Please help me in this....


Youhave posted that for success you il print one .(DOT) but there its priniting 6 dots for 3 testcases...

What s the prob.. how to solv it.... Please help me out...

కే.కే said...

Hi Dan, this example of running Instrumentation was very useful. I would like to know if there is any way we can get the logreport of failures and success of tests in a text file.
pls email me if so.. :)