Note: This post is originally appeared here
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.
Create a project for api demo:
In Eclipse, create a new Android project using existing source for ApiDemos:
I installed the SDK on a macbook, so you’ll need to adjust the directory based on your installation.
Create a project for api demo tests:
In Eclipse, create another Android project using existing source. This time, the path to source should be:
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.
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.
Run the first project as Android project. You should see the emulator starts and api demo being deployed.
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!
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)
You should see this:
Now you can go about doing your own TDD!