"No one is harder on a talented person than the person themselves" - Linda Wilkinson ; "Trust your guts and don't follow the herd" ; "Validate direction not destination" ;

May 07, 2011

TestNG - Grouping Test Cases, Executing Test Multiple Times

Our next post is grouping test cases and executing a test case more than once. Below is sample code with comments and TestNG XML file for the code is also provided. We have covered
  • Grouping of Test Cases
  • Executing Particular Test Case more than once
  • Dependency based Tests
import org.testng.annotations.*;
public class TestNGExample
{
      @Test(threadPoolSize = 4, invocationCount = 5,  timeOut = 10000, groups = { "functional","BVT" })
      //This method will be run a total of 5 times using 4 threads
      public void TestCaseOne()
      {
            try
            {
                  System.out.println("In Test Case One - Functional + BVT");
            }
            catch(Exception e)
            {
                  System.out.println(e.toString());
            }
      }
     
      @Test(dependsOnMethods = "TestCaseOne" , groups = { "functional","BVT" }, invocationCount = 2,  timeOut = 10000)
      //This method will be run a twice
      //Depends on TestCaseOne
      public void TestCaseOneContinued()
      {
            try
            {
                  System.out.println("In Test Case One Continued Functional + BVT");
            }
            catch(Exception e)
            {
                  System.out.println(e.toString());
            }
      }

      @Test(groups = { "functional" })
      //Grouped as Functional Test Case
      public void TestCaseTwo()
      {
      try
      {
            System.out.println("In Test Case Two  - Functional Testcase");
      }
      catch(Exception e)
      {
            System.out.println(e.toString());
      }
      }
     
      @Test(groups = { "BVT" })
      //Part of BVT - Build Verification Cases
      public void TestCaseThree()
      {
      try
      {
            System.out.println("In Test Case Three - BVT Test Case");
      }
      catch(Exception e)
      {
            System.out.println(e.toString());
      }
      }
     
}

TestNG XML config is provided below

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="TestNGSuite" parallel="none" verbose="1">
<test name="BVTTestCaseExecution"> 
  <groups> 
    <run> 
      <include name="BVT"/> 
    </run> 
  </groups> 
  <classes> 
    <class name="TestNGExample"/> 
  </classes> 
</test>
<test name="Functional Test Cases Execution"> 
  <groups> 
    <run> 
      <include name="functional"/> 
    </run> 
  </groups> 
  <classes> 
    <class name="TestNGExample"/> 
  </classes> 
</test>
</suite>
Below is the output for the program. Right click on XML and run it as TestNG test


Good Read
How does TestNG invoke a test method using multiple threads?

Happy Reading!!

No comments: