Running APIDemos Tests in Android SDK 1.5pre

I learned last night that the Android SDK 1.5 pre was out.  I downloaded it and tried to write a simple service test work without any luck.  I couldn’t find any sample test code for service that’s clear enough to help me.  After a few posts in Android’s google group I finally get it working.  Hopefully Android team will in the future write some good documentation on how to setup the test environment in Eclipse.  Here is what I did to get it work in case people running into same problems.

I assume that you’ve downloaded and installed the SDK and ADT .9 inside Eclipse.

1. Create a project for api demo:

In Eclipse, create a new Android project using existing source for ApiDemos:

/projects/android-sdk-mac_x86-1.5_pre/platforms/android-1.5/samples/ApiDemos

I installed the SDK on a macbook, so you’ll need to adjust the directory based on your installation.

2. Create a project for api demo tests:

In Eclipse, create another Android project using existing source.  This time, the path to source should be:

projects/android-sdk-mac_x86-1.5_pre/platforms/android-1.5/samples/ApiDemos/tests

You will get some errors because the test project needs to know where the api demo project is.  So open project properties -> Java Build Path -> Projects, and add the first project you created as required projects on the build path.

3. Make sure there’s no errors in either project.  I have added java sdk and junit libraries to the build path to get rid some errors.

4. Run the first project as Android project.  You should see the emulator starts and api demo being deployed.

5. Run the 2nd (test) project as Android project and you should see something like this:

[2009-04-14 13:32:47 – com_example_android_apis_tests] Uploading com_example_android_apis_tests.apk onto device ’emulator-5554′
[2009-04-14 13:32:47 – com_example_android_apis_tests] Installing com_example_android_apis_tests.apk…
[2009-04-14 13:32:50 – com_example_android_apis_tests] Success!
[2009-04-14 13:32:50 – com_example_android_apis_tests] Project dependency found, installing: apidemos
[2009-04-14 13:32:50 – apidemos] Application already deployed. No need to reinstall.
[2009-04-14 13:32:50 – com_example_android_apis_tests] /com_example_android_apis_tests/bin/com_example_android_apis_tests.apk installed on device
[2009-04-14 13:32:50 – com_example_android_apis_tests] Done!

6. Open a commandline, assume your have SDK’s tools directory in your path, type this to run the LocalServiceTest:

adb shell am instrument -w -e class \

com.example.android.apis.app.LocalServiceTest  \

com.example.android.apis.tests/android.test.InstrumentationTestRunner

Note: above 3 lines is one command.

You should see something like this:

zlu-macbook:tests zlu$ adb shell am instrument -w -e class com.example.android.apis.app.LocalServiceTest com.example.android.apis.tests/android.test.InstrumentationTestRunner

com.example.android.apis.app.LocalServiceTest:…..
Test results for InstrumentationTestRunner=…..
Time: 0.637

OK (5 tests)

7.  If properly setup, you can run test from within Eclipse using Run As…, Then choose Android JUnit test on LocalServiceTest.

You should see this:

android-junit

Now you can go about doing your own TDD!

Advertisements

13 thoughts on “Running APIDemos Tests in Android SDK 1.5pre

  1. I struggled about 30 minutes with step seven.
    I used run as -> JUnit, then got the message “multiple launchers available. Select one” and selected “Android JUnit Test Launcher” which will fail.

    I totally missed the “Android JUnit Test” option in the list of available configurations. Is that new in 1.5? After that it worked out of the box.

    Anyway, thx a lot, now I can finally test my lausy code 🙂

  2. Hi. Doing all as you write, there is no possible to get projects without errors. After first step I have 3 messages:
    [ApiDemos] libpng warning: Ignoring attempt to set cHRM RGB triangle with zero area
    [ApiDemos] libpng warning: Ignoring attempt to set cHRM RGB triangle with zero area
    [ApiDemos] Error generating final archive: Debug certificate expired on 10/29/09 6:00 PM!
    Doing exactly as you write in the step 2, I have the same “Error generating final archive: Debug certificate expired on 10/29/09 6:00 PM!”

    Windows system locale/location is English, United States.
    Any suggestions? Thanks.

  3. Oh! My mistake! In my experiments, I swap generated debug.keystore by previous one generated from sdk 1.1. Sorry for prev mess))

  4. Great article. Even today, there are not enough docs about android unit testing so this is a great resource. Just do steps 1 – 4 and then step 7. No reason to do steps 5 and 6. Step 7 covers them both and in addition shows the test result in a standard Eclipse jUnit reporting window, which is really well done and convenient.

  5. So I open TWO identical projects? What name(s) am I supposed to use?

    I’m clearly a total noob. I wish the docs didn’t assume I had some knowledge I don’t yet have.

    A GREAT tutorial prepares the reader for the steps before presenting them, for example: “…We’ll be creating two projects in the same workspace. The first is named Bob, the second is named Carol…”

  6. Thanks for this post; now I am able to run the tests!

    I would have thought that a test project with access to the test source files could have been created with File -> New -> Other -> Android Test Project (Eclipse 3.6.1), but that doesn’t work. When using “an existing android project” as the “Test target” (so that the project is created in the tests subdirectory of APIDemos), an error about AndroidManifest.xml not existing occurs (although AndroidManifest.xml does exist). After this the project is created, but there is an error about java.lang.Object.

  7. Pingback: android development with Eclipse « Software Engineering/Development of Jeremy Caton

  8. Pingback: dien dan cong dong it lon nhat viet nam

  9. Pingback: mobile phone guides

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s