Learn How to Work With DataProvider in TestNG Effectively

Data Provider in TestNG

DataProvider in TestNG

data providers are useful when implementing data-driven tests. Data providers separate test methods and test data. This is applicable while we are doing complex framework and the data is coming from other source – objects, objects read from a property file, database, file system etc. There can be the parameter of the parameter.

This can be achieved in two different ways

Ads code goes here
  • Iterate over the input datasets(from a single test method.)
  • For each test method, create a test data set.

How to use DataProvider annotation in TestNG?

A data provider method is having annotations as @DataProvide. Data Provider as one String attribute- the name. In case, we have not provided Data Provider a name, the TestNG will pick up the default value as the method’s name as the Data Provide’s name.

The Data Provide method must return an array of objects. DataProvider is an important concept to remember in Parameterization in TestNG.

TestNG Data Provider returns one of the following types:

  1. An array of array objects(Object[][]). The first dimension is the size that represents the number of times the test methods will be invoked. The second dimension talks about the array of objects that will be consumed by the test method.
  2. An iterator of objects like Iterator<Object[]>. This allows us to create our own test data. Once invoked this will supply the test data in a sequential manner.
public class DataProvider{
@Test(dataProvider="dp1")
  public void myTest(int n,String s){
   system.out.println("int-"+n+"String-"+s);
}
@BeforeMethod
  public void beforeMethod(){}
     @DataProvider(name="dp1")
      public Object][] dp(){
       return new object[][]{
        new object[]{1,"one"},
        new object[]{2,"two"},
};
}
@DataProvider(name="dp2")
    public Object][] dp(){
      return new object[][]{
        new object[]{"Name",new String},
        new object[]{RollNo,new Integer(20)},
};
}
}

A data provider can return:

  • An iterator of parameter types.

public iterator<Object[]> crateTestData(){
return new MyIterator(...);
}
  • An argument of the type
public object[][] crateTestData(method m){
System.out.println("The method name"+m.getName());
return new Object[][]{new Object[]{"Name"}};
}

Let’s create an actual class to test. This class accepts an Integer value and determines if the value is odd or even. The actual class will be as follows:

package com.test;
import org.testng.annotations.Test;
public class OddEvenCalculator {
  public boolean validate(final Integer oddEven)
  {
	  if(oddEven/2==0) {
		  return true;
	  }
	return false;
	  
  }
}

The test class will look like:


package com.test;

import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class TestOddEvenCalculator {
    private OddEvenCalculator oddEven;
    @BeforeMethod
    public void setup() {
    	oddEven=new OddEvenCalculator();
    }
    @DataProvider(name="dp")
    public static Object[] oddEvenNumbers() {
    	return new Object [ ][ ] { { 2,true}, {5,false}, {9, false}, {10, true}};
    }
    @Test(dataProvider="dp")
    public void testOddEvenChecker(Integer input, boolean flag) {
    System.out.println("input "+input +" flag "+ flag);
    Assert.assertEquals(flag, oddEven.validate(input));
    }
}

the testng.xml looks like below:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="TestOddEvenCalculator" verbose="1">
  <test name="TestOddEvenCalculator">
     <classes>
      <class name="com.test.TestOddEvenCalculator">
       </class>
    </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->
DataProviders use in TestNG
DataProviders use in TestNG

The console output is as follows:

READ  What is TestNG?

[RemoteTestNG] detected TestNG version 7.0.0
input 2 flag true
input 5 flag false
input 9 flag false
input 10 flag true

===============================================
TestOddEvenCalculator
Total tests run: 4, Passes: 2, Failures: 2, Skips: 0
========

Share and Enjoy !

Leave a Comment

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