使用Robolectric和ANT进行测试

6
我正在为连续构建环境设置Robolectric,并在设置过程中遇到一些问题。我的Android Eclipse项目结构包括一个Android项目和一个JUnit4测试项目,正如Robolectric的“Eclipse快速入门”指南所描述的那样。我的样本测试在Eclipse中工作正常,但我也需要能够使用ant进行测试。我该如何构建支持此设置的ANT build.xml?以及如何应用与Eclipse测试项目相同的更改?我一直在研究RobolectricSample项目的build.xml文件,但它只包含单个项目,其中生产和测试代码都位于项目的src文件夹下。我知道这是maven假设的情况(???),但我想仅使用ANT。

你解决这个问题了吗?我也遇到了类似的情况,想要使用ANT进行构建。 - bianca
1个回答

4

这篇文章虽然有点年代久远,但也许能帮助到其他人。最近,我运用了robolectric、mockito、Jenkins和ant等技术进行了测试。以下是我使用的ant构建脚本。你需要设置库文件的路径并创建一个目标来启动测试。另外,我还将android.jar文件和maps.jar文件复制到测试项目的lib文件夹中,这似乎可以使事情变得更加容易,或者说你有更好的方法可以实现。

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project name="unit_tests" default="test-report-junit" basedir=".">
<description>
    Sample Robolectric Ant Build
</description>

<!-- set global properties for this build, if you have libraries, include them here with a relative path...I have samples as library1, library2, library3, located on the same level as your project, but you  may need to modified this to get it to work for your setup. -->

<property name="libs.dir" value="./lib/"/>
<property name="build.dir" value="./build/"/>
<property name="android.library1.classpath" value="./../../library1/bin/classes/"/>
<property name="android.library2.classpath" value="./../../library2/bin/classes/"/> 
<property name="android.library3.classpath" value="./../../library3/bin/classes/"/>     
<property name="test.report.dir" value="./test-reports/"/>
<property name="test.html.dir" value="./test-report-html/"/>
<property name="source.dir" value="./src/"/>    

<filelist id="android_jars" dir="${libs.dir}">
    <file name="android.jar"/>
    <file name="maps.jar"/>
</filelist>

<filelist id="libs_jars" dir="${libs.dir}">
    <file name="junit.jar"/>
    <file name="hamcrest.jar"/>
    <file name="json.jar"/>
    <file name="google-play-services.jar"/>
    <file name="mockito-all-1.9.5.jar"/>
    <file name="robolectric-1.1-jar-with-dependencies.jar"/>
</filelist>

<path id="compile_classpath">
    <filelist refid="libs_jars"/>
    <filelist refid="android_jars"/>
    <pathelement path="${android.project.classpath}"/>
    <pathelement path="${android.library1.classpath}"/>
    <pathelement path="${android.library2.classpath}"/>
    <pathelement path="${android.library3.classpath}"/>
    <pathelement path="${build.dir}"/>
</path>

<path id="junit_classpath">
    <pathelement path="${build.dir}"/>
    <pathelement path="${android.library1.classpath}"/>
    <pathelement path="${android.library2.classpath}"/>
    <pathelement path="${android.library3.classpath}"/>

    <!-- NOTE: junit.jar must come before android.jar! -->
    <filelist refid="libs_jars"/>
    <filelist refid="android_jars"/>
</path>

<!-- targets -->

<target name="init">
    <!-- Create the time stamp -->
    <tstamp/>
    <mkdir dir="${build.dir}"/>
</target>

<target name="compile" depends="init" description="compile test source">
    <javac srcdir="${source.dir}" destdir="${build.dir}" debug="true" >
        <classpath refid="compile_classpath" />
    </javac>

    <copy todir="build">
        <fileset dir="src" includes="**/*.xml,**/*.properties,**/*.txt,**/*.ico" />
    </copy>
</target>

<target name="test-run" depends="compile" description="Run JUnit tests">
    <mkdir dir="${test.report.dir}"/>
    <echo message="Running JUnit Tests in directory ${source.dir}..."/>
    <junit showoutput="true" printsummary="yes" failureproperty="junit.failure" fork="yes" forkmode="once" maxmemory="512m">
        <formatter type="plain"/>
        <formatter type="xml"/>
        <batchtest todir="${test.report.dir}">
            <fileset dir="${source.dir}">
                <include name="**/*Test.java"/>
            </fileset>
        </batchtest>
        <classpath refid="junit_classpath"/>
    </junit>
    <fail if="junit.failure" message="Unit test(s) failed. See reports!"/>
</target>

<target name="test-report-junit" depends="test-run" description="Generate JUnit HTML reports">
    <mkdir dir="${test.html.dir}"/>
    <junitreport todir="${test.report.dir}">
        <fileset dir="${test.report.dir}" includes="TEST-*.xml"/>
        <report format="frames" todir="${test.html.dir}"/>
    </junitreport>  
</target>

<target name="clean" description="Clean Up" >
    <delete dir="${build.dir}"/>
    <delete dir="${test.report.dir}"/>
    <delete dir="${test.html.dir}"/>
    <delete file="${basedir}/tmp/cached-robolectric-classes.jar"/>
</target>
</project>

最后,我从Jenkins运行以下命令,以启动所有内容:
ant -f ./build-ant.xml test-report-junit

为什么需要提供 test-report-junit 参数? - IgorGanapolsky

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接