Running single test or class using TestNG and Ant

I have been using TestNG for a while now in the latest project that I am working on. We have developed a whole suite of tests for an application and TestNG has really served us well.

When it comes to running tests, we find that people (developer, testers and others) usually are hesitant to use something that involves much learning or is different from how they are used to doing things currently. So, we found it very useful to provide utilities that made it easy for anyone to be able to pick up test artifacts and with-in a few steps be able to run tests to reproduce bugs. Because we are building our project using ant, it was easy to provide a build.xml with the test distribution that will able to run the TestNG tests. This keeps things simple and uniform.

The TestNG ant task uses TestNG suites defined in XML files to run tests. It doesn’t provide an easy way to run a single test method or all the test methods in a single class. Basically, anytime someone has to run a test using this task, they need to create an XML file specifying the class and method for that test in an XML file and then execute the ant task. This can be simplified by letting ant targets take care of setting up the XML file with the require test and then executing it.

It’s quite simple. You just need to create a TestNG suite XML template, modify it with the parameters that the user passes in on command line and then execute the TestNG ant task. Here’s how this can be achieved. First, create a template XML file like the following at ${testng.templates}/testfn.xml:

< !DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Single Method Suite">
   <test name="Single Method Test">
      <classes>
         <class name="@CLASS@">
            <methods>
               <exclude name=".*" />
               <include name="@TEST@" />
            </methods>
         </class>
      </classes>
   </test>
</suite>

Now, you can add the following target to the build file:

<target name="run-single-test" 
         description="run a specific test. Requires class.name property set to fully qualified name of test class and test.name property set to method name">
   <condition property="propsSpecified">
      <and>
         <isset property="class.name" />
         <isset property="test.name" />
      </and>
   </condition>
   <fail unless="propsSpecified" 
            message="class.name and/or test.name property not specified."/>
   <copy todir="${tmp.dir}" file="${testng.templates}/testfn.xml" overwrite="true">
      <filterset>
         <filter token="CLASS" value="${class.name}"/>
         <filter token="TEST" value="${test.name}"/>
      </filterset>
   </copy>
   <testng classpathref="lib.path"
           outputDir="${results.dir}/${DSTAMP}.${TSTAMP}-single-test-${class.name}-${test.name}"
           workingDir="${results.dir}/${DSTAMP}.${TSTAMP}-single-test-${class.name}-${test.name}"
           verbose="2"
           useDefaultListeners="false"
           listeners="${testng.listeners}">
      <xmlfileset dir="${tmp.dir}" includes="testfn.xml" />
      <jvmarg value="-Xmx1024m"/>
   </testng>
</target>

The target picks the testfn.xml file, replaces the tokens with the specified input and copies it to the temp location. Now, the TestNG task can use this updated XML file to run tests. This target is invoked as:

ant run-single-test -Dclass.name=com.nalinmakar.testng.ant.Demo -Dtest.name=Test1

for the following class:

package com.nalinmakar.testng.ant;
public class Demo
{
   @Test
   public void Test1()
   {
      //do something
   }
   @Test
   public void Test2()
   {
      //do something
   }
}

Similarly, you can also create a template and another ant target for running all the test methods in a class:

< !DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Single Class Suite">
   <test name="Class Test">
      <classes>
         <class name="@CLASS@"  />
       </classes>
   </test>
</suite>
<target name="run-class"
        description="run all methods in a specific test class. Requires class.name property to be set to fully qualified name of class">
   <condition property="classNameSpecified">
      <isset property="class.name" />
   </condition>
   <fail unless="classNameSpecified"
         message="class.name property not specified. Don't know which test class to run."/>
   <copy todir="${tmp.dir}" file="${testng.templates}/class.xml" overwrite="true">
      <filterset>
         <filter token="CLASS" value="${class.name}"/>
      </filterset>
   </copy>
   <testng classpathref="lib.path"
           outputDir="${results.dir}/${DSTAMP}.${TSTAMP}-class"
           workingDir="${results.dir}/${DSTAMP}.${TSTAMP}-class"
           verbose="2"
           useDefaultListeners="false"
           listeners="${testng.listeners}">
      <jvmarg value="-Xmx1024m"/>
      <xmlfileset dir="${tmp.dir}" includes="class.xml" />
   </testng>
</target>

and can invoke this as

ant run-single-test -Dclass.name=com.nalinmakar.testng.ant.Demo


About this entry