Monday, January 18, 2016

Test NG DataProvider with Groups using XML

This is the XML



 <?xml version="1.0" encoding="UTF-8"?>  
 <suite name="DataProviderSuite">  
      <test name="DataProviderTest">  
           <groups>  
                <run>  
                     <include name="dpGroup1" />  
                     <exclude name="dpGroup2" />  
                </run>  
           </groups>  
           <classes>  
                <class name="com.sudas.com.sudas.TestNG.BareBoneTest"></class>  
           </classes>  
      </test>  
 </suite>  

This is the DataProvider class

 package com.sudas.com.sudas.TestNG;  
 import org.testng.annotations.DataProvider;  
 import org.testng.annotations.Test;  
 public class DataProviderExample {  
      public static void main(String[] args) {  
           understandMultiArray();  
      }  
      // Data provider method  
      /***  
       * Notes: If the data generator is on the same class where the test method  
       * is a non static method would work other wise needs a static method A Test  
       * NG data provider has return type of multidimensional array object  
       *   
       * @return  
       *   
       */  
      @DataProvider(name = "sampleDP")  
      public static String[][] dataGenerator() {  
           // I am declaring a 2-dimensional object of type string  
           String data[][] = { { "v1A", "v2A", "v3A" }, { "v1B", "v2B", "v3B" }, { "v1C", "v2C", "v3C" } };  
           return data;  
      }  
      @Test(testName = "TestForDP", suiteName = "DataProviderTestSuite", dataProvider = "sampleDP")  
      public void testDP(String a, String b, String c) {  
           System.out.println(a);  
           System.out.println(b);  
           System.out.println(c);  
      }  
      // To understand the 2-dimensional array  
      public static void understandMultiArray() {  
           String data[][] = { { "v1A", "v2A", "v3A" }, { "v1B", "v2B", "v3B" }, { "v1C" } };  
           for (int i = 0; i < data.length; i++) {  
                for (int j = 0; j < data[i].length; j++) {  
                     System.out.println(data[i][j]);  
                }  
           }  
      }  
 }  

This is the test class

 package com.sudas.com.sudas.TestNG;  
 import org.testng.annotations.Test;  
 import org.testng.annotations.BeforeMethod;  
 import org.testng.annotations.AfterMethod;  
 import org.testng.annotations.DataProvider;  
 import org.testng.annotations.BeforeClass;  
 import org.testng.annotations.BeforeGroups;  
 import org.testng.Reporter;  
 import org.testng.annotations.AfterClass;  
 import org.testng.annotations.AfterGroups;  
 import org.testng.annotations.BeforeTest;  
 import org.testng.annotations.AfterTest;  
 import org.testng.annotations.BeforeSuite;  
 import org.testng.annotations.AfterSuite;  
 public class BareBoneTest {  
      @Test(groups = {  
                "dpGroup1","test" }, dataProvider = "sampleDP", dataProviderClass = com.sudas.com.sudas.TestNG.DataProviderExample.class)  
      public void dataProvider01(String a, String b, String c) {  
           Reporter.log("This is parameter 01" + a, true);  
      }  
      @Test(groups = {  
                "dpGroup2","test" }, dataProvider = "sampleDP", dataProviderClass = com.sudas.com.sudas.TestNG.DataProviderExample.class)  
      public void dataProvider02(String a, String b, String c) {  
           Reporter.log("This is parameter 02" + b, true);  
      }  
      @BeforeMethod  
      public void beforeMethod() {  
           System.out.println("@BeforeMethod");  
      }  
      @AfterMethod  
      public void afterMethod() {  
           System.out.println("@AfterMethod");  
      }  
      @BeforeClass  
      public void beforeClass() {  
           System.out.println("@BeforeClass");  
      }  
      @AfterClass  
      public void afterClass() {  
           System.out.println("@AfterClass");  
      }  
      @BeforeTest  
      public void beforeTest() {  
           System.out.println("@BeforeTest");  
      }  
      @AfterTest  
      public void afterTest() {  
           System.out.println("@AfterTest");  
      }  
      @BeforeSuite  
      public void beforeSuite() {  
           System.out.println("@BeforeSuite");  
      }  
      @AfterSuite  
      public void afterSuite() {  
           System.out.println("@AfterSuite");  
      }  
      @BeforeGroups  
      public void beforeGroups() {  
           System.out.println("@BeforeGroups");  
      }  
      @AfterGroups  
      public void afterGroups() {  
           System.out.println("@AfterGroups");  
      }  
 }  
OUTPUT Report


More TesNG Data Provider Examples



 @DataProvider  
      public Object[][] phoneObjectDP() {  
           // example  
           String s1[][] = { {}, {} };  
           // example  
           String[][] s2 = new String[3][];  
           Object phoneObjects[][] = { {}, {}, {} };  
           return phoneObjects;  
      }  
      @DataProvider  
      public Object[][] fruitsObject() {  
           return new Object[][] { { 1, "Apple" }, { "Banana", 1.32 } };  
      }  

No comments:

Post a Comment