17

问题:测试(似乎)没有执行

第一步:编译源码到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(es) 并运行它们

<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

请问我错过了什么?

4

2 回答 2

26

我相信您可以使用batchtest内部junit任务:

<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>   

请注意以下事项:

  • Infileset dir="${test}"应该指向测试的源目录。
  • include name="**/*Test*"你不应该指定扩展.class名;它应该是.java或什么都不是。
  • 您需要将测试输出目录添加为junit任务元素的“类路径”。

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

于 2012-04-15T19:02:18.957 回答
1

像这样使用“ batchtest ”:

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

示例在这里

编辑:

有关打印摘要,请参阅ant junit 任务不报告详细信息

请注意,您不再需要 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>
于 2012-04-15T19:02:58.293 回答