views:

4164

answers:

5

Currently I'm running instrumentations tests from the command line this way:

adb shell am instrument -w com.blah.blah/android.test.InstrumentationTestRunner

Is there a way to run them from Eclipse (with automatic installation of the application)?

+3  A: 

I was not able to determine automatic deployment to the emulator. However, you can take that same "adb shell" command and create an external launch configuration. I blogged on this same topic here. Launching this way is a bit more intuitive when you're also using the "-e debug true" argument.

However, I think I've gotten more mileage out of the bash shell script (if you're using a good development platform) :

function adbtest() {
    adb shell  am instrument -w -e class blah.package.$1 blah.package.test/android.test.InstrumentationTestRunner;
}

That way when I want to test blah.package.FooTest I only need to remember to type:

james@trex:~$ adbtest FooTest
James A Wilson
+3  A: 

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:

  1. Let's call the project root directory root
  2. Inside, I have the build.xml generated by the activityCreator script in the SDK.
  3. 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.

Brian Pellin
A: 

I have been able to make this technique work with Android 1.0R2, Eclipse 3.4.2, and Windows XP SP3, but it is unclear how I debug tests run using the instrumentation mechanism.

A: 

Hi Brian,

Thanks for sharing this useful information. I tried changing the build.xml's in the specified manner, but when I run "ant tests", I am getting - "Error:Unable to find instrumentation info for: ComponentInfo".

Can you please suggest if I am missing any condition.

Thanks, Lakshmi

Lakshmi
A: 

I'm looking at this issue as well. I'm using ADT 0.9.5 in Eclipse 3.4.2 on WinXP. I have my application in one project OSN and I created a project OSN_AndroidTests for tests using the ADT menu "New Test Project". I depends on the OSN project. I have written my tests in OSN_AndroidTests. When I tried to run the OSN_AndroidTests for the first time as "Android Junit Test" it failed. Then, I tried to run the main project, OSN (note, run as Android Application, not as a test). This installed the main project and the test project and ran the tests. Now, I can also run the tests by "Android Junit Test" on the OSN_AndroidTests project.

So, I don't understand this logic, but I'm able to create tests and run them from Eclipse without having to use adb directly. Has anyone some more experience with this? For your information, I'm attaching the console log generated when I request the test:


    2010-01-10 07:05:52 - OpenSatNav_AndroidTests]------------------------------
    [2010-01-10 07:05:52 - OpenSatNav_AndroidTests]Android Launch!
    [2010-01-10 07:05:52 - OpenSatNav_AndroidTests]adb is running normally.
    [2010-01-10 07:05:52 - OpenSatNav_AndroidTests]Performing android.test.InstrumentationTestRunner JUnit launch
    [2010-01-10 07:05:52 - OpenSatNav_AndroidTests]Automatic Target Mode: Preferred AVD 'my-android-1.5_r3-avd' is available on emulator 'emulator-5554'
    [2010-01-10 07:05:52 - OpenSatNav_AndroidTests]Uploading OpenSatNav_AndroidTests.apk onto device 'emulator-5554'
    [2010-01-10 07:05:52 - OpenSatNav_AndroidTests]Installing OpenSatNav_AndroidTests.apk...
    [2010-01-10 07:05:59 - OpenSatNav_AndroidTests]Success!
    [2010-01-10 07:06:00 - OpenSatNav_AndroidTests]Project dependency found, installing: OpenSatNav
    [2010-01-10 07:06:00 - OpenSatNav]Application already deployed. No need to reinstall.
    [2010-01-10 07:06:00 - OpenSatNav_AndroidTests]Launching instrumentation android.test.InstrumentationTestRunner on device emulator-5554
    [2010-01-10 07:06:24 - OpenSatNav_AndroidTests]Test run complete

droidguy