Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A custom_rules XML file in Ant for performing Robotium tests.
<?xml version="1.0" encoding="UTF-8"?>
<!-- custom_rules.xml contains a set of added functions for Ant to use. This includes useful macrodefs for
testing a particular test case or package, and you can even define your own that executes them for convenience. -->
<project name="RobotiumMyProject">
<property name="reports.dir" value="reports"/>
<property name="tests.dir" value="com.jerieljan.myproject.tests"/>
<target name="init-props">
<xpath input="${tested.project.dir}/AndroidManifest.xml"
expression="/manifest/@package" output="tested.project.app.package"/>
</target>
<!-- The following target shows how to fetch the default report using adb pull. -->
<target name="fetch-test-report" depends="init-props">
<echo>Downloading XML test report...</echo>
<mkdir dir="${reports.dir}"/>
<exec executable="${adb}" failonerror="true">
<arg line="${adb.device.arg}"/>
<arg value="pull"/>
<arg value="/data/data/${tested.project.app.package}/files/junit-report.xml"/>
<arg value="${reports.dir}/junit-report.xml"/>
</exec>
</target>
<!-- Ant targets for Robotium testing -->
<target name="robotium">
<echo>Running all tests...</echo>
<antcall target="robotium-login"/>
<antcall target="robotium-myprofile"/>
</target>
<!-- Ant targets for each Robotium test package.
Each test-class line is done in sequence.-->
<target name="robotium-login">
<test-class class="LoginTests" package="login"/>
<test-class class="PasswordRecoveryTests" package="login"/>
</target>
<target name="robotium-myprofile">
<test-class class="MyProfileTests" package="myprofile"/>
<test-class class="EditProfileTests" package="myprofile"/>
</target>
<!-- Macro definitions for cherry-picked Robotium testing -->
<macrodef name="test-class">
<attribute name="package"/>
<attribute name="class"/>
<sequential>
<echo level="info">Preparing tests for @{package}/@{class}</echo>
<sleep seconds="2"/>
<echo level="info">Running tests for @{package}/@{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="com.jerieljan.myproject.@{package}.@{class}"/>
<arg value="${tests.dir}/${test.runner}"/>
</exec>
<echo level="info">EXEC: ${adb.device.arg} shell am instrument -w -e class
ph.com.smart.netphone.@{package}.@{class} ${tests.dir}/${test.runner}
</echo>
<fetch-report package="@{package}" class="@{class}"/>
</sequential>
</macrodef>
<macrodef name="test-package">
<attribute name="package"/>
<sequential>
<echo level="info">Running tests for @{package}</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="package"/>
<arg value="com.jerieljan.myproject.@{package}"/>
<arg value="${tests.dir}/${test.runner}"/>
</exec>
</sequential>
</macrodef>
<macrodef name="fetch-report">
<attribute name="class"/>
<attribute name="package"/>
<sequential>
<xpath input="${tested.project.dir}/AndroidManifest.xml"
expression="/manifest/@package" output="tested.project.app.package"/>
<echo>Retrieving XML test results for @{package}/@{class}</echo>
<mkdir dir="${reports.dir}"/>
<exec executable="${adb}" failonerror="true">
<arg line="${adb.device.arg}"/>
<arg value="pull"/>
<arg value="/data/data/${tested.project.app.package}/files/junit-report.xml"/>
<arg value="${reports.dir}/@{package}/@{class}.xml"/>
</exec>
<echo>Report copied to ${reports.dir}/@{package}/@{class}.xml</echo>
</sequential>
</macrodef>
</project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment