"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" ;
Showing posts with label TestNG - Examples. Show all posts
Showing posts with label TestNG - Examples. Show all posts

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

March 18, 2011

TestNG Example - testng - Passing parmeters in XML File

Our next post is based on my question in stackoverflow site. Let's try an example based on Cedric reply.

Below is example code
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import com.thoughtworks.selenium.Selenium;
import static org.testng.Assert.assertTrue;
import org.testng.annotations.*;
import com.thoughtworks.selenium.*;
public class TestNGXMLData
{
@Test
@Parameters(value={"A","B","C","D"})
public void xmlDataTest(String A, String B, String C, String D)
{ 
            System.out.println("Passing Three parameter to Test "+A +" and "+B + " and "+C);
            String[] elements = D.split(",");
            for (String element : elements)
            {
                  System.out.println("Item is "+element);
                 
            }
}
}

XML for setting parameters

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="none">
  <test name="Test" preserve-order="false">
   <parameter name="A" value="1"/> 
   <parameter name="B" value="2"/> 
   <parameter name="C" value="3"/> 
   <parameter name="D" value="4,5,6,7,8"/> 
    <classes>
      <class name="TestNGXMLData"/>
    </classes>
  </test>
</suite>

Right Click on XML and run it as TestNG Test. Below is output in console window
Passing Three parameter to Test 1 and 2 and 3
Item is 4
Item is 5
Item is 6
Item is 7
Item is 8

Extending this example and adding multiple tests and setting it for parallel execution and preserve test order.

Modified XML File
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="classes" thread-count="5">
  <test name="firstTest" preserve-order="true">
   <parameter name="A" value="1"/> 
   <parameter name="B" value="2"/> 
   <parameter name="C" value="3"/> 
   <parameter name="D" value="4,5,6,7,8"/> 
    <classes>
      <class name="TestNGXMLData"/>
    </classes>
  </test>
  <test name="secondTest" preserve-order="true">
   <parameter name="A" value="10"/> 
   <parameter name="B" value="20"/> 
   <parameter name="C" value="30"/> 
   <parameter name="D" value="40,50,60,70,80"/> 
    <classes>
      <class name="TestNGXMLData"/>
    </classes>
  </test>
  <test name="thirdTest" preserve-order="true">
   <parameter name="A" value="100"/> 
   <parameter name="B" value="200"/> 
   <parameter name="C" value="300"/> 
   <parameter name="D" value="400,500,600,700,800"/> 
    <classes>
      <class name="TestNGXMLData"/>
    </classes>    
  </test>
</suite>

Modified code adding try-catch block (Keeping up with Coding Guidelines :))

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import com.thoughtworks.selenium.Selenium;
import static org.testng.Assert.assertTrue;
import org.testng.annotations.*;
import com.thoughtworks.selenium.*;
public class TestNGXMLData
{
@Test
@Parameters(value={"A","B","C","D"})
public void xmlDataTest(String A, String B, String C, String D)
{ 
            try
            {
                  System.out.println("Passing Three parameter to Test "+A +" and "+B + " and "+C);
                  String[] elements = D.split(",");
                  for (String element : elements)
                  {
                        System.out.println("Item is "+element);
                       
                  }
            }
            catch(Exception e)
            {
                  System.out.println("Exception Occurred "+ e.toString());
            }
}
}

Output

Passing Three parameter to Test 1 and 2 and 3
Item is 4
Item is 5
Item is 6
Item is 7
Item is 8
Passing Three parameter to Test 10 and 20 and 30
Item is 40
Item is 50
Item is 60
Item is 70
Item is 80
Passing Three parameter to Test 100 and 200 and 300
Item is 400
Item is 500
Item is 600
Item is 700
Item is 800

Happy Reading!!

December 21, 2010

TestNG: Passing Array of Objects in DataProvider


Now in this example, We will try an example to pass Array of Objects in the Dataprovider for TestNG test.

Please find working code and example listed below. Please run it as TestNG test in your eclipse editor


import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
import org.testng.Assert;
import org.testng.annotations.*;
import static org.testng.Assert.*;
import java.util.*;

//Example for Array of Objects
public class TestNGExample
{

 private String InputData;
 private List<String> ExpectedResults = new ArrayList<String>();
public String getInputData()
{
  return InputData;
}

public void setInputData(String inputData)
{
  InputData = inputData;
}

public List<String> getExpectedResults()
{
  return ExpectedResults;
}

public void setExpectedResults(List<String> expectedResults)
{
  ExpectedResults = expectedResults;
}


@DataProvider(name = "Array of objects Example")
public Object[][] parameterIntTestProvider()
{
  System.out.println("Start Data Provider Section");
  List<String> TestOneExpectedList = new ArrayList<String>();
  TestOneExpectedList.add("one expected");
  TestOneExpectedList.add("Three expected");
  TestOneExpectedList.add("two expected");
  TestOneExpectedList.add("four expected");

  List<String> TestTwoExpectedList = new ArrayList<String>();
  TestTwoExpectedList.add("one expected");
  TestTwoExpectedList.add("Three expected");
  TestTwoExpectedList.add("two expected");
  TestTwoExpectedList.add("four expected");

  TestNGExample[] Arrayobj = new TestNGExample[2];
  System.out.println("Declaration Successful : Assign Data Provider");
  Arrayobj[0] = new TestNGExample(); // I made mistake here, Commenting to highlight learning
  Arrayobj[0].setInputData("One");
  Arrayobj[0].setExpectedResults(TestOneExpectedList);
  System.out.println("First Assignment : " + Arrayobj[0].getInputData());

  Arrayobj[1] = new TestNGExample();
  Arrayobj[1].setInputData("Two");
  Arrayobj[1].setExpectedResults(TestTwoExpectedList);
  System.out.println("End : Assign Data Provider");

  for (int i = 0; i < 2; i++)
  {
   TestNGExample arrayobj = Arrayobj[i];
   System.out.println("Input Data is :" + arrayobj.getInputData());
   List<String> ls = arrayobj.getExpectedResults();
   Iterator it = ls.iterator();
   while (it.hasNext())
   {
    String value = (String) it.next();
    System.out.println("Value :" + value);
   }
  }
 return new Object[][] { { Arrayobj } };
}

@Test(dataProvider = "Array of objects Example")
public void TestMethodforClass(TestNGExample[] Arrayobj)
{
  System.out.println("Start Test : Array of objects Example");
  for (int i = 0; i < 2; i++)
  {
    TestNGExample arrayobj = Arrayobj[i];
    System.out.println("Input Data is :" + arrayobj.getInputData());
    List<String> ls = arrayobj.getExpectedResults();
    Iterator it = ls.iterator();
    while (it.hasNext())
    {
     String value = (String) it.next();
     System.out.println("Value :" + value);
    }
   }
  System.out.println("End Test : Array of objects Example");
 }

}

Results
Start Data Provider Section
Declaration Successful : Assign Data Provider
First Assignment : One
End : Assign Data Provider
Input Data is :One
Value :one expected
Value :Three expected
Value :two expected
Value :four expected
Input Data is :Two
Value :one expected
Value :Three expected
Value :two expected
Value :four expected
Start Test : Array of objects Example
Input Data is :One
Value :one expected
Value :Three expected
Value :two expected
Value :four expected
Input Data is :Two
Value :one expected
Value :Three expected
Value :two expected
Value :four expected
End Test : Array of objects Example
PASSED: TestMethodforClass([LTestNGExample;@1ac2f9c)


Below link was useful for working on Array of Objects.

Happy Reading!!