Howto: Android Unit Test Bootstrapping in Eclipse

Today we run in some trouble, while trying to “run” our test suites against our android code. So here is a short howto run your tests in eclipse. As example we are going to use the APIDemo tests…

1. Import API Demos to eclipse:
New -> AndroidProject -> Create Project from existing source -> Location: <android-sdk>/platforms/android-1.5/samples/ApiDemos/

2. Test code in APIDemo project:
Take a look at the tests folder in APIDemos android project. The whole test code for APIDemos is located in there.

3. Create a test project APIDemoTest:
Right-click on the “tests” folder and create a new Project <APIDemosTest>:
New -> AndroidProject -> Create Project from existing source -> Location: <APIDemo/tests folder>

4. Customize the AndroidManifest.xml in APIDemoTest project as follows:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.android.apis.tests">
 
   <!-- We add an application tag here just so that we can indicate that
        this package needs to link against the android.test library,
        which is needed when building test cases. ->    
   <application>
       <uses-library android:name="android.test.runner" />
   </application>
 
   <!-
   This declares that this app uses the instrumentation test runner targeting
   the package of com.example.android.apis.  To run the tests use the command:
   "adb shell am instrument -w com.example.android.apis.tests/android.test.InstrumentationTestRunner"
   -->
   <instrumentation android:name="android.test.InstrumentationTestRunner"
                    android:targetPackage="com.example.android.apis"
                    android:label="Tests for Api Demos."/>
 
</manifest>

4. Deploy both apps on emulator or mobile device:
<project> -> Run as -> Android Application

5. Go to CLI and type:

adb shell am instrument -w com.example.android.apis.tests/android.test.InstrumentationTestRunner

6. Test results:
Now your tests should have been invoked and you will be provided with an output similiar to:

Test results for InstrumentationTestRunner=……………F.F……
Time: 6.658

FAILURES!!!
Tests run: 22, Failures: 2, Errors: 0

7. Note:
Every dot in ……………F.F…… represents a successful test and F the ones that failed.

8. Have fun with testing your own code!!

Wenn dir dieser Beitrag gefällt, lade den Autor doch mal zu einem Drink ein ;-)

Broadcast us
  • Yigg
  • Webnews.de
  • Digg
  • MisterWong
  • del.icio.us
  • Technorati
  • DZone
  • Facebook
  • Google Bookmarks
  • Reddit
  • StumbleUpon
  • TwitThis

Tags: , , , , , , ,

Leave a Reply