Automating JUnit TestCases Using TestSuite and Ant - JUnit
Home » JUnit

Automating JUnit TestCases Using TestSuite and Ant

12 November 2009 One Comment

Unit testing is now a days an integrated part of any development process. It saves considerable effort when we write repeatable unit test classes, instead of relying on manual unit testing. JUnit can be called leader in Java unit testing. This framework provides many commonly needed features, which make unit testing really effortless. JUnit also provides feature of grouping test cases as a test suite. Test cases can be grouped logically, functionally and even according to purpose of group – regression test, sanity test, smoke test etc.

junit
We would also need these test cases to be repeatable without any manual intervetion. Means it should be possible to automate test suite execution. We achieve this using another most common tool – Ant. Ant is used to build projects. Ant provides many tasks to compile, generating web archieve (war) and also a task to run a test suite. In this article, we combine JUnit and Ant to automate test suite execution. Also, we use Ant to generate unit test summary reports which we need for management overview.

Here is list of what we need.
- Test Class (Example.java)
- Test Suite (AllTests.java)
- Ant build file (build.xml)
- JUnit jar (Available on JUnit site)
- Ant set up (Details available on Ant site)

This is how our Eclipse Java project structure will be.

junitsuiteexample
     - build.xml
     – bin (class files generated)
     – lib
          – junit-4.4.jar
     – src
          – test
              – AllTests.java
              – ExampleTest.java
  Â
In our example, the important files are two .java files and build.xml. Here are the file contents.

ExampleTest.java

package test;

import org.junit.Before;
import org.junit.Test;

import junit.framework.TestCase;

public class ExampleTest extends TestCase {
    @Before
    public void setUp() throws Exception {
    	super.setUp();
    	System.out.println("Set Up Complete.");
 	}

	@Test
	public void testMethod(){
		System.out.println("Sample test Successful");
	}
}

AllTests.java

package test;

import junit.framework.Test;
import junit.framework.TestSuite;

public class AllTests {

	public static Test suite() {
		TestSuite suite = new TestSuite("Test for test");
		suite.addTestSuite(ExampleTest.class);
		return suite;
	}

}

build.xml

<?xml version="1.0"?>
<project name="junitsuiteexample" default="testsuite">

 <property name="project.name" value="junitsuiteexample"/>
 <property name="src.dir" value="src"/>
  <property name="lib.dir" value="lib"/>
  <property name="target.dir" value="target"/>
  <property name="classes.dir" value="${target.dir}/classes"/>
  <property name="junit.out.dir.xml"  value="${target.dir}/report/junit/xml"/>
  <property name="junit.out.dir.html" value="${target.dir}/report/junit/html"/>

  <echo>Cleaning classes dir...</echo>
  <target name="clean" description="Delete earlier classes">
     <delete dir="${target.dir}" quiet="yes"/>
  </target>

  <echo>Make required directories...</echo>
  <target name="makedir" depends="clean" description="Create required directories">
   <mkdir dir="${classes.dir}"/>
  </target>

  <echo>Compile</echo>
  <target name="compile" depends="makedir" description="Compile java code.">
   <javac debug="true" destdir="${classes.dir}">
    <src path="${src.dir}"/>
    <classpath>
     <fileset dir="${lib.dir}" includes="**/*.jar"/>
    </classpath>
   </javac>

  </target>
    <path id="classpath.test">
      <path path="${java.class.path}"/>
      <pathelement location="${classes.dir}"/>
			<fileset dir="${lib.dir}" includes="**/*.jar"/>
      <path location="${classes.dir}"/>
     </path>

  <echo>Running junit test suite...</echo>
   <target name="testsuite" depends="compile" description="Running Test Suite">
     <delete dir="${junit.out.dir.xml}"/>
     <mkdir  dir="${junit.out.dir.xml}"/>
     <junit printsummary="yes" haltonfailure="no" fork="yes" maxmemory="512m">
       <classpath refid="classpath.test"/>
       <sysproperty key="ant.home" value="${ant.home}"/>
       <formatter type="xml"/>
       <batchtest fork="yes" todir="${junit.out.dir.xml}">
         <fileset dir="${classes.dir}" includes="**/AllTests.class"/>
       </batchtest>
    </junit>
  </target>

</project>

We can execute the test cases by running ant command. Simply type ‘ant’ from command prompt at build.xml directory and see following results.

output

 

More Related Posts in JUnit

One Comment »

  • Stijn said:

    Very very useful post!
    Nice work, I was searching the whole week to get a very clear post on this topic.

    Thank you very much!
    Keep on going this way!

    Greets

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.