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.