Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ant, run all jUnit tests

Tags:

java

ant

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?

like image 610
James Raitsev Avatar asked Apr 15 '12 18:04

James Raitsev


People also ask

Can JUnit tests be run automatically?

JUnit is best at creating repeatable test cases. If there is a need to run a test in JUnit 100 times, one doesn't need to write it 100 times or not even execute it manually 100 times. This process is automated.

Can JUnit be integrated with ant?

There are a number of JUnit extensions available. If you are unfamiliar with JUnit, you should download it from www.junit.org and read its manual. This chapter shows how to execute JUnit tests by using Ant. The use of Ant makes it straight forward through the JUnit task.

What are JUnit Mcq fixtures?

What are fixtures in JUnit? Explanation: Tests need to run against the backdrop of a set of predefined or known objects. This set of objects is called a test fixture.


1 Answers

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.

like image 183
nobeh Avatar answered Sep 30 '22 21:09

nobeh