Ant,运行所有jUnit测试

时间:2012-04-15 18:56:11

标签: java ant

问题:测试(似乎)未执行

第1步:将源代码编译为bin

<target name="compile" depends="init" description="compile the source ">
    <javac srcdir="${src}" destdir="${build}" includeantruntime="true" nowarn="yes" debug="true" />
    <javac srcdir="${src}" destdir="${bin}" includeantruntime="true" nowarn="yes" debug="true" />
</target>

第2步:将测试编译到bin

<target name="compileTest" depends="compile" description="compile jUnit Test cases ">
    <javac srcdir="${test-dir}" destdir="${bin}" includeantruntime="true" nowarn="yes" debug="true" />
</target>

第3步:找到Test.class并运行它们

<target name="test" depends="compileTest">
        <junit>
            <formatter type="plain" usefile="false" />
            <formatter type="plain" />
            <batchtest>
                <fileset dir="${bin}" includes="**/Test*.class" />
            </batchtest>
        </junit>
    </target>

输出:

Buildfile: /Users/xx/Documents/repositories/app/build.xml
clean:
   [delete] Deleting directory /Users/xx/Documents/repositories/app/build
   [delete] Deleting directory /Users/xx/Documents/repositories/app/bin
init:
    [mkdir] Created dir: /Users/xx/Documents/repositories/app/build
    [mkdir] Created dir: /Users/xx/Documents/repositories/app/bin
compile:
    [javac] Compiling 145 source files to /Users/xx/Documents/repositories/app/build
    [javac] Compiling 145 source files to /Users/xx/Documents/repositories/app/bin
compileTest:
    [javac] Compiling 24 source files to /Users/xx/Documents/repositories/app/bin
test:
dist:
   [delete] Deleting: /Users/xx/Documents/repositories/app/dist/app.jar
      [jar] Building jar: /Users/xx/Documents/repositories/app/dist/app.jar
     [copy] Copying 1 file to /Users/xx/Documents/repositories/app/dist
   [delete] Deleting: /Users/xx/Documents/repositories/app/dist/app.war
      [war] Building war: /Users/xx/Documents/repositories/app/dist/app.war
     [copy] Copying 1 file to /Users/xx/Documents/repositories/app/dist
BUILD SUCCESSFUL
Total time: 5 seconds

我缺少什么?

2 个答案:

答案 0 :(得分:23)

我相信你可以在junit任务中使用batchtest

<target name="test" depends="compileTest">
  <junit>
    <classpath>
      <pathelement location="bin" />    
      <pathelement location="lib/junit-4.10.jar"/>
    </classpath>    
    <batchtest>
       <fileset dir="${test}">
            <include name="**/*Test*" />
       </fileset>
    </batchtest>
    <formatter type="brief" usefile="false"/>
  </junit>
</target>   

注意以下内容:

  • fileset dir="${test}"中应指向测试的源目录。
  • include name="**/*Test*"中,您指定.class分机;它应该是.java或者没有。
  • 您需要将测试输出目录添加为junit任务元素的“类路径”。

我通过一个简单的项目进行了测试,并且使用相同的配置获得了简短的结果。我使用了Apache Ant 1.7.1。

答案 1 :(得分:1)

像这样使用“batchtest”:

  <batchtest>
    <fileset dir="${tst-dir}" includes="**/Test*.class" />
  </batchtest>

here以上的示例。

修改

有关打印摘要,请参阅ant junit task does not report detailthis

请注意,junit任务中不再需要属性printsummary="yes"showoutput="true"。格式化器现在正在处理输出。

<target name="test" depends="compileTest">
    <junit>
        <formatter type="plain" usefile="false" /> <!-- to screen -->
        <formatter type="plain" /> <!-- to file -->

        <batchtest>
            <fileset dir="${bin}" includes="**/Test*.class" />
        </batchtest>
    </junit>
</target>