DataProvider annotation in TestNG

@DataProvide annotation is another important feature of testNG which is used to perform Data-driven Automation

@DataProvider annotation in TestNG

In other words, DataProvider is a way to pass data sets to test methods without using XML parameters. It is used when we need to pass complex data sets while reading data from any external resources like excel, Database or property file.

@DataProvider(name=” DataProvider_Name”) tag is used to define any method as data provider and
@Test(dataProvider = “DataPRovider_Name”) is used to pass the data set in the test method as we can see in the below examples.

DataProvider returns a double Object class array

Define DataProvider Method

@DataProvider(name = "data-provider")
public Object[][] dataProviderMethod() {
return new Object[][] { { "Value 1" }, { "Value 2" },{"Value 3"} };
}

Pass Data In Test Method

@Test(dataProvider = "data-provider")
public void testMethod(String data) {
System.out.println("Data is: " + data);
}

We can define the Data provider method in the same class or any other class. Let’s see some example

@DataProvider and @Test in same class

In the below example, We are using @DataProvider annotation and @Test in the same class. It’s really easy to pass data from the same class just need to pass the name of data provider in my case its “data-provider”.

The count of test method execution depends on the size of the object array or the number of data set. In my case, it will execute 4 times and using different data in every execution.

package com.AutomationTut;

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

public class DataProviderExample {
  
  @DataProvider(name = "data-provider")
  public Object[][] dataProviderMethod() {
      return new Object[][] { { "Value 1" }, { "Value 2" },{"Value 3"},{"Value 4"} };
  }

  @Test(dataProvider = "data-provider")
  public void testMethod(String data) {
      System.out.println("Data is: " + data);
  }
}

Output:

TestNG @DataProvider and @Test in different class

In the real-time work environment, it’s really good practise to keep all the files or classes separate. So, Suppose we create a common class to provide data in all the test methods for a complete automation framework.

In this case, let’s see how to read data in @Test method from another class.

Create data provider class(DataProviderCtl .java)

package com.AutomationTut;

import org.testng.annotations.DataProvider;

public class DataProviderCtl {
  
   @DataProvider(name = "data-provider")
    public Object[][] dataProviderMethod() {
        return new Object[][] { { "Value 1" }, { "Value 2" },{"Value 3"},{"Value 4"} };
    }

}

To call the test data will use “dataProviderClass” attribute.

Syntax:

@Test(dataProvider = "data-provider", dataProviderClass = DataProviderCtl.class)

Example:

package com.AutomationTut;

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

public class DataProviderExample {
  
 

  @Test(dataProvider = "data-provider", dataProviderClass = DataProviderCtl.class)
  public void testMethod(String data) {
      System.out.println("Data is: " + data);
  }
}

Output: