I don't know of a great way to run tests automatically from Eclipse, but I've put together a straight forward method of automatically building and deploying tests using ant.
My project is organized as follows:
- Let's call the project root directory root
- Inside, I have the build.xml generated by the activityCreator script in the SDK.
- I have a second project containing my tests located in root/tests
- This project has it's own AndroidManifest.xml (see the structure of the Android API Demos as an example)
- This project also has it's own build.xml
In order to support junit in the root/tests/build.xml, you need to add the path to junit. One way to do this is to add the path to the compile, dex, debug, and release targets (release is omitted, but it needs the same change that debug does). Also in the compile target we include the ../src path:
<!-- Compile this project's .java files into .class files. -->
<target name="compile" depends="dirs, resource-src, aidl">
<javac encoding="ascii" target="1.5" debug="true" extdirs=""
srcdir="src/:../src"
destdir="${outdir-classes}"
bootclasspath="${android-jar}">
<classpath>
<fileset dir="${external-libs}" includes="*.jar"/>
<fileset file="${junit-path}"/>
</classpath>
</javac>
</target>
<!-- Convert this project's .class files into .dex files. -->
<target name="dex" depends="compile">
<echo>Converting compiled files and external libraries into ${outdir}/${dex-file}...</echo>
<apply executable="${dx}" failonerror="true" parallel="true">
<arg value="--dex" />
<arg value="--output=${intermediate-dex-ospath}" />
<arg path="${outdir-classes-ospath}" />
<fileset dir="${external-libs}" includes="*.jar"/>
<fileset file="${junit-path}"/>
</apply>
</target>
<!-- Package the application and sign it with a debug key.
This is the default target when building. It is used for debug. -->
<target name="debug" depends="dex, package-res">
<echo>Packaging ${out-debug-package}, and signing it with a debug key...</echo>
<exec executable="${apk-builder}" failonerror="true">
<arg value="${out-debug-package-ospath}" />
<arg value="-z" />
<arg value="${resources-package-ospath}" />
<arg value="-f" />
<arg value="${intermediate-dex-ospath}" />
<arg value="-rf" />
<arg value="${srcdir-ospath}" />
<arg value="-rj" />
<arg value="${external-libs-ospath}" />
<arg value="-rj" />
<arg value="${junit-path}" />
<arg value="-nf" />
<arg value="${native-libs-ospath}" />
</exec>
</target>
Now, we can build both projects separately. The final touch is to add a new target to root/build.xml that will build and deploy the project and tests and execute the tests.
To do this add the following target to root/build.xml:
<target name="tests" depends="reinstall">
<echo>Building and installing tests..</echo>
<exec executable="ant" failonerror="true">
<arg value="-f" />
<arg value="tests/build.xml" />
<arg value="reinstall"/>
</exec>
<mkdir dir="${log-dir}" />
<exec executable="${adb}">
<arg value="shell" />
<arg value="am" />
<arg value="instrument" />
<arg value="-w" />
<arg value="-e" />
<arg value="class" />
<arg value="org.yourproject.AllTests" />
<arg value="org.yourproject.tests/android.test.InstrumentationTestRunner" />
</exec>
</target>
Once this is all in place, start the emulator, and run "ant tests". This will build, deploy, and execute your tests in one command.