Sunday, November 09, 2008

Android: Testing on the Android platform - ApiDemos tests

This document can be read in Google Docs (, cut and paste link.

A previous post,Android: Testing on the Android platform - Unit tests, showed how to add your tests and run them from inside Eclipse. Although these are very useful tests in our quest to Test Driven Development, they cover only a small part of our needs.

Android 1.0 introduced a whole new world of tests cases like ActivityUnitTestCase to run isolated unit tests, ServiceTestCase to tests services and even functional test of activities like ActivityInstrumentatonTestCase.

ApiDemos provides a set of tests but you will not be able to easily run them unless in a non convoluted way as the way they are structured it's not supported by current Android Development Tools (ADT) Eclipse plugin and you would need other tools, but the solution is so simple that I wonder why they are not originally provided in the way we will be changing them.

Google documentation is not helping either.

ApiDemos tests

Tests are located in the tests/src folder and they have their corresponding AndroidManifest.xml.

Inside this folder, tests like this one are found


 * Copyright (C) 2008 Google Inc.



import android.test.ActivityInstrumentationTestCase;


 * Make sure that the main launcher activity opens up properly, which will be

 * verified by {@link ActivityInstrumentationTestCase#testActivityTestCaseSetUpProperly}.


public class ApiDemosTest extends ActivityInstrumentationTestCase<ApiDemos> {

    public ApiDemosTest() {

        super("", ApiDemos.class);



but we will not be able to run them not resorting to ant or maven, unless we make the following set of changes.

ApiDemosTest Android project

Create new project

Let's create a new Android project from within Eclipse, as usual New -> Project -> Android -> Android Project

Activity and Application name are set to Dummy because they are not used as we are seeing shortly.

Copy ApiDemos tests

Copy the tests/src folder from te original ApiDemos project and turn it into a source folder.

Fix the project setup to use original ApiDemos project to resolve references.

Copy AndroidManifest.xml

Again, from the original ApiDemos project copy AndroidManifest.xml overwriting the existing one

Build and run

If necessary, remove ApiDemos.apk if it has been already installed in the emulator.

Build ApiDemos and ApiDemosTests projects and install them.

Now, if you execute Dev Tools in the emulator and go to Instrumentation, you'll find Test for Api Demos. in the list.

Select it and you will obtain the tests results. Unfortunatelly by now, the reslts will be only visible in the logcat window

11-09 22:56:35.132: INFO/instrumentation(153): Test results for InstrumentationTestRunner=......................

11-09 22:56:35.132: INFO/instrumentation(153): Time: 6.739

11-09 22:56:35.132: INFO/instrumentation(153): OK (22 tests)

or alternativelly, from the command line

$ adb shell am instrument -w

Test results for InstrumentationTestRunner=......................

Time: 7.493

OK (22 tests)


So simple that I can't understand why they are not provided in this way by the original Android ApiDemos project.

Next time we will try to present test results in a more concise way inside the emulator as Electro did (video1, video2, video3).

Post a Comment