Ant and jUnit Integration and Generating jUnit Report

Here we will show you how to run a junit test in Ant build. Also show you Ant and jUnit Integration into Ant build file and generating jUnit Report using ANT build file. You can run your JUnit unit tests with Apache Ant – an open source build tool.

Ant and jUnit Integration and Generating jUnit Report

JUnit is the commonly used unit testing framework for Java-based developments. It is easy to use and easy to extend.

Ant has a built-in task called “junit” which can run your unit tests. Here is a simple example:

<target name="unittest">
  <mkdir dir="reports"/>
  <mkdir dir="reports/raw"/>
  <mkdir dir="reports/html/"/>
    <junit printsummary="yes" haltonfailure="yes" showoutput="yes">
      <classpath>
       <pathelement location="${build.dir}/classes"/>
             <fileset dir="${basedir}">
                 <include name="lib/*.jar" />
             </fileset>
         </classpath>
          <test name="com.dineshonjava.algo.AppTestCases" todir="reports/raw"/>
      <formatter type="xml"/>
    </junit>
 </target>
 

In the classpath element I add the output locations of my project code and junit jars, and test code. In other words, the directories where my compiled code + test code is located.

In the test element I declare which unit test class to execute. I also specify the name of the output report in the todir attribute (“reports/raw”).

Now jUnit test with report generation in the HTML.

<target name="unittest">
  <mkdir dir="reports"/>
  <mkdir dir="reports/raw"/>
  <mkdir dir="reports/html/"/>
    <junit printsummary="yes" haltonfailure="yes" showoutput="yes">
      <classpath>
       <pathelement location="${build.dir}/classes"/>
             <fileset dir="${basedir}">
                 <include name="lib/*.jar" />
             </fileset>
         </classpath>
          <test name="com.dineshonjava.algo.AppTestCases" todir="reports/raw"/>
      <formatter type="xml"/>
    </junit>
 </target>
 
 <target name="test-reports" depends="unittest">
        <junitreport todir="reports">
            <fileset dir="reports/raw/">
                <include name="TEST-*.xml" />
            </fileset>
            <report format="noframes" todir="reports/html/" />
        </junitreport>
    </target>

Here second target “test-report” generate the report in the form of HTML to the given location in todir attribute “reports/html”.

Following file is our test class with some test methods.

AppTestCases.java

/**
 * 
 */
package com.dineshonjava.algo;

import static org.junit.Assert.*;

import org.junit.Test;

/**
 * @author Dinesh.Rajput
 *
 */
public class AppTestCases {

 @Test
 public void add() {
  assertEquals(5, 3+2);
 }
 
 @Test
 public void subs() {
  assertEquals(1, 3-2);
 }
 
 @Test
 public void mult() {
  assertEquals(6, 3*2);
 }

}

build.xml

<?xml version="1.0" encoding="UTF-8"?>
<project name="AlgoTest.makejar" default="makejar" basedir=".">
 <presetdef name="javac">
  <javac includeantruntime="false"/> 
 </presetdef>
  <property name="build.dir" value="build" />
  <target name="clean" description="cleans up the build by deleting the build,dist, web directories">
     <delete dir="${build.dir}"/>
   </target>
   <target name="init" depends="clean" description="Setup for build script">   
     <mkdir dir="${build.dir}"/>
   </target>
   <target name="compile" depends="init" description="Compiles .java files to WAR directory">
     <mkdir dir="${build.dir}/classes"/>
       <javac srcdir="src" destdir="${build.dir}/classes">
        <classpath>
            <fileset dir="${basedir}">
                <include name="lib/*.jar" />
            </fileset>
        </classpath>
       </javac>
     </target>    
      <target name ="makejar" description="Create a jar for the AlgoTest project" depends="compile">
     <jar jarfile="${build.dir}/AlgoTest.jar" basedir="${build.dir}/classes">
      <manifest>
        <attribute name = "Main-Class" value = "com.dineshonjava.algo.Test"/>
     </manifest>
     </jar>
   </target>
 <target name="run">
  <java jar="${build.dir}/AlgoTest.jar" fork="true"></java>
 </target>
 <target name = "generate-javadoc">
  <mkdir dir="doc"/>
   <javadoc packagenames="com.dineshonjava.algo.*" sourcepath="src" 
        destdir = "doc" version = "true" windowtitle = "Algo Application">
     <doctitle><![CDATA[= Algo Application =]]></doctitle>
         <bottom>
            <![CDATA[Copyright © 2016. All Rights Reserved.]]>
         </bottom>
         <group title = "algo packages" packages = "com.dineshonjava.algo.*"/>
  </javadoc>
  <echo message = "java doc has been generated!" />
 </target>
 
 <target name="unittest" depends="compile">
  <mkdir dir="reports"/>
  <mkdir dir="reports/raw"/>
  <mkdir dir="reports/html/"/>
    <junit printsummary="yes" haltonfailure="yes" showoutput="yes">
      <classpath>
       <pathelement location="${build.dir}/classes"/>
             <fileset dir="${basedir}">
                 <include name="lib/*.jar" />
             </fileset>
         </classpath>
          <test name="com.dineshonjava.algo.AppTestCases" todir="reports/raw"/>
      <formatter type="xml"/>
    </junit>
 </target>
 
 <target name="test-reports" depends="unittest">
        <junitreport todir="reports">
            <fileset dir="reports/raw/">
                <include name="TEST-*.xml" />
            </fileset>
            <report format="noframes" todir="reports/html/" />
        </junitreport>
    </target>
 
</project>

This example shows the execution of JUnit on the com.dineshonjava.algo.AppTestCases junit class. Running the above code produces the following output:

jUnit Test case with ANT

After running this test cases please go to directory /report/html and we observe one html file is there now open this html file into any browser and look into the output as below.

Ant and jUnit Integration and Generating jUnit Report

For this working codes and whole project you could checkout from here

https://github.com/DOJ-SoftwareConsultant/AntAndJunitTestReport

 

Previous