Question

Basically, what I want to achieve is something like this but using Robotium in Ant. I want to test my packages (suites?) individually using an Ant command.

The reason for this is because the complexity of the application is so large that it encounters problems when I run everything at once, usually terminating midway due to a memory leak as Robotium plows through the cases, and how some cases affect future cases due to a change in state somewhere in the application.

Everything works perfectly when running just the current package or running each case individually though (using my IDE), so I want to know how this is achieved through Ant instead.

Was it helpful?

Solution

I've figured out the problem myself. It's actually pretty simple as I've mimicked the behavior of ant test.

The idea is that whenever ant test is run, it simply invokes the adb shell command and triggers the instrumentation test runner am instrument, along with other parameters. The parameters can be customized to define which classes or packages would be tested.

The solution is to define a target on your custom_rules.xml (I use macrodef so I can reuse this for other targets) which performs this.

    <macrodef name="test-class">
    <attribute name="class"/>

    <sequential>
        <echo level="info">Running tests for @{class}</echo>
        <exec executable="${adb}" failonerror="false">
            <arg line="${adb.device.arg}"/>
            <arg value="shell"/>
            <arg value="am"/>
            <arg value="instrument"/>
            <arg value="-w"/>
            <arg value="-e"/>
            <arg value="class"/>
            <arg value="@{class}"/>
            <arg value="com.example.application/${test.runner}"/>
        </exec>
    </sequential>

</macrodef>

So if you want an Ant command such as test-example that tests the .tests.ExampleTest test cases, you can define it as such:

<target name="test-example">
     <test-class class="com.example.application.tests.ExampleTest />
</target>

And then run it as

ant clean debug install test-example 

or just

ant test-example

Oh and this isn't Robotium-exclusive as Robotium actually builds on the existing test framework Android provides.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top