Ant, run all jUnit tests

2020-05-23 03:18发布

问题:

The problem: Tests are (seemingly) not executed

Step 1: Compile source to 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>

Step 2: Compile tests to bin

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

Step 3: Find Test.class(es) and run them

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

Output:

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

What am i missing please?

回答1:

I believe you can use batchtest inside junit task:

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

Note the following:

  • In fileset dir="${test}" should point to the source directory for the tests.
  • In include name="**/*Test*" you should not specify .class extension; it should be .java or nothing.
  • You need to add the test output directory as a "classpath" for junit task element.

I had a test with a simple project and with the same configuration I got the brief results. I used Apache Ant 1.7.1.



回答2:

Use "batchtest" like this:

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

Example present over here.

EDIT:

For printing summary see ant junit task does not report detail and this

Note that you no longer need the attributes printsummary="yes" and showoutput="true" in the junit task. The formatter is taking care of output now.

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


标签: java ant