Tuesday, 22 January 2013

TestNG Tutorial 5 – Suite Test


The “Suite Test” means bundle a few unit test cases and run it together.
In TestNG, XML file is use to define the suite test. The below XML file means both unit test “TestNGTest1” and “TestNGTest2” will execute together.
<!DOCTYPE suite SYSTEM "http://beust.com/testng/testng-1.0.dtd" >
<suite name="My test suite">
  <test name="testing">
    <classes>
       <class name="TestNGTest1" />
       <class name="TestNGTest2" />
    </classes>
  </test>
</suite>
Beside classes bundle testing, TestNG provides a “Grouping” feature to bundle few methods as a single unit for testing, where every method is tie to a group.
For example, Here’s a class with four methods, three groups (method1, method2 and method3)
import org.testng.annotations.*;
 
/**
 * TestNG Grouping

 *
 */
public class TestNGTest5_2_0 {
 
 @Test(groups="method1")
 public void testingMethod1() {  
   System.out.println("Method - testingMethod1()");
 }  
 
 @Test(groups="method2")
 public void testingMethod2() {  
  System.out.println("Method - testingMethod2()");
 }  
 
 @Test(groups="method1")
 public void testingMethod1_1() {  
  System.out.println("Method - testingMethod1_1()");
 }  
 
 @Test(groups="method4")
 public void testingMethod4() {  
  System.out.println("Method - testingMethod4()");
 }    
}
You can execute the unit test with group “method1” only.
<!DOCTYPE suite SYSTEM "http://beust.com/testng/testng-1.0.dtd" >
<suite name="My test suite">
  <test name="testing">
   <groups>
      <run>
        <include name="method1"/>
      </run>
    </groups>
    <classes>
       <class name="TestNGTest5_2_0" />
    </classes>
  </test>
</suite>
Result
Method - testingMethod1_1()
Method - testingMethod1()
TestNG Grouping is highly flexible and useful, especially when you implement it in your project integration testing.

No comments:

Post a Comment