Running Junit test cases using Ant

This tutorial explains setting up a simple junit example using Ant Junit task

Ant has task named Junit which can be used for unit testing using Junit 3.0 and Junit 4.0 framework. To run junit using Ant you need junit-4.9.jar ( or latest ) and ant-junit.jar .

ant-junit jar would be already there in ant lib folder so you don’t have to do anything for that. Incase you need help in setting up Ant for this tutorial , just read the tutorial on how to setup ant for your system.

You can download latest junit-x.y.jar from junit website

To do simple unit testing i have created one simple Calculator class and its corresponding test class.

No need to copy manually , You can also download the full code for this tutorial from link mentioned below

Code for Calculator.java

package com.learnjunit;
public class Calculator {

public double add(double number1, double number2) {
return number1 + number2;
}
}

And its corresponding test class is CalculatorTest.java

package com.learnjunit.test;

import com.learnjunit.*;
import org.junit.Test;
import static org.junit.Assert.*;

public class CalculatorTest {

@Test
public void testadd() {
Calculator c = new Calculator();
//Asserts that two doubles or floats are equal to within a positive delta.
double result=c.add(10,20);
// Check if result is equal to expected value of 30
// IF they are not equal , exception is throws with given message
assertEquals("Checking add method failed",30,result, 0);

}
}

Now comes the main part , the build file for junit task

  1: <project name="learnjunit" default="junit">
  2: <description>Build file to do junit testing</description>
  3: 
  4: 	<property file="build.properties" />
  5: 	<property name="src.dir" location="src" />
  6: 	<property name="test.dir" location="test" />
  7: 	<property name="src.dir.classes" location="${src.dir}/classes" />
  8: 	<property name="test.dir.classes" location="${test.dir}/classes" />
  9: 	<property name="junit.lib.dir" location="lib" />
 10: 	<property name="test.result.dir" location="target" />
 11: 
 12: 	<path id="junit.lib">
 13: 		<fileset dir="${junit.lib.dir}">
 14: 			<include name="**/*.jar" />
 15: 		</fileset>
 16: 		<pathelement location="${src.dir.classes}" />
 17: 	</path>
 18: 
 19: 	<target name="clean" description="Delete all directories which are created as part of build">
 20: 		<delete dir="${src.dir.classes}" />
 21: 		<delete dir="${test.dir.classes}" />
 22: 		<delete dir="${test.result.dir}" />
 23: 	</target>
 24: 
 25: 	<target name="init" depends="clean" description="Create classes directory for compilation">
 26: 		<mkdir dir="${src.dir.classes}" />
 27: 		<mkdir dir="${test.dir.classes}" />
 28: 	</target>
 29: 
 30: 	<target name="compile.src" depends="init" description="Compile to source code">
 31: 		<javac srcdir="${src.dir}" destdir="${src.dir.classes}" />
 32: 	</target>
 33: 
 34: 	<target name="compile.test" depends="compile.src" description="Compile Junit test classes">
 35: 		<javac srcdir="${test.dir}" destdir="${test.dir.classes}">
 36: 			<classpath>
 37: 				<pathelement location="${src.dir.classes}" />
 38: 				<pathelement path="${junit.lib}" />
 39: 			</classpath>
 40: 		</javac>
 41: 	</target>
 42: 
 43: 	<target name="junit" depends="compile.test" description="Run the junit test">
 44: 		<mkdir dir="${test.result.dir}/test-results" />
 45: 		<junit fork="true" forkmode="perBatch" haltonfailure="false"
 46: 			printsummary="true" dir="${test.result.dir}" failureproperty="test.failed">
 47: 			<classpath>
 48: 				<pathelement location="${src.dir.classes}" />
 49: 				<pathelement location="${test.dir.classes}" />
 50: 			</classpath>
 51: 			<formatter type="brief" usefile="false" />
 52: 			<formatter type="xml" />
 53: 			<batchtest todir="${test.result.dir}/test-results">
 54: 				<fileset dir="${test.dir.classes}">
 55: 					<include name="**/*Test.class" />
 56: 				</fileset>
 57: 			</batchtest>
 58: 		</junit>
 59: 		<mkdir dir="${test.result.dir}/test-report" />
 60: 		<junitreport todir="${test.result.dir}/test-report">
 61: 			<fileset dir="${test.result.dir}/test-results">
 62: 				<include name="TEST-*.xml" />
 63: 			</fileset>
 64: 			<report format="frames" todir="${test.result.dir}/test-report" />
 65: 		</junitreport>
 66: 		<fail if="test.failed" />
 67: 	</target>
 68: 
 69: </project>

In junit target , it first created one directory where it can place the test results


In classpath for junit we are giving the classes folder for both source and test java files. These class files are created by targets compile.src and compile.test


When junit target runs it see all the files in test.dir.classes which end with .class and consider them for batch run as junit.


In the junitreport we are making report in the form of frames similar to javadoc reports for hava source code.


 


To run this example , download complete source zip and extract it to your computer.


Open command prompt


run command like


ant junit


The build successful message will appear


The build will create target directory and place the junit test results


Thanks for reading , please share your comments

No comments:

Post a Comment

Please share your views and comments below.

Thank You.