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" >

   
      
         
            
               
               
            
         
      
   

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


   
      
         
         
      
   
   
   
      
         
         
      
   
   
      
      
   

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" >

   
      
         
       
   


   
      
   
   
   
      
         
      
   
   
      
      
   

and can invoke this as

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

Join the Conversation

7 Comments

  1. This worked great! Thanks! The only issues I had were:

    1. The SAX parser did not like the blanks on the DOCTYPE line in the .xml file (org.xml.sax.SAXParseException: The markup in the document preceding the root element must be well-formed.) I removed the two space (one at the beginning of the line and one at the end) and the problem went away.

    2. With the verbose=”2″ option to testng, an innocuous warning was printed ([org.testng.internal.PoolService] Shutting down poolservice org.testng.internal.PoolService@111ded2 terminated:false). When I removed the verbose option, the warning went away.

  2. @Nalin: Thanks for ur response Nalin. When I try to run a single class, all the classes are getting executed. I dont understand where the mistake is.

  3. Thank you So much for taking precious time to provide this needed solution. It is very helpful. I haven’t used it yet but I have read it and i am sure it will work without a problem. thanks again

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.