TestNG DataProvider

Filed Under: TestNG

DataProvider is one of the best features of TestNG framework. We can use TestNG DataProvider to inject arguments into our test methods.

TestNG DataProvider

TestNG DataProvider helps us in creating loosely coupled test cases. We can separate testing logic and input data into different methods, this way we can also reuse the input data from a DataProvider for multiple test methods.

Let’s see how we can create a simple DataProvider for a TestNG class using Eclipse plugin. In Eclipse, go to New | Other | TestNG Class and in the wizard screen, select @DataProvider annotation.

TestNG DataProvider

When you click on Finish button, we get a default implementation of TestNG test class with DataProvider method.


package com.journaldev.dataprovider;

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

public class Test4 {
  @Test(dataProvider = "dp")
  public void f(Integer n, String s) {
	  
  }

  @DataProvider
  public Object[][] dp() {
    return new Object[][] {
      new Object[] { 1, "a" },
      new Object[] { 2, "b" },
    };
  }
}

Note that it’s not necessary to use Eclipse TestNG plugin, but it helps us in easily creating a base class and then change it according to our project use case.

Notice that dp() method is annotated with @DataProvider annotation and our we have used it in @Test configuration.

DataProvider method must return Object array and its dimension must match to the test method. Since our test method has two arguments, our DataProvider method must return Object[][] so that it gets mapped to the method arguments.

Just run the above class as TestNG test and we will get following output in the console.


[RemoteTestNG] detected TestNG version 6.14.3
PASSED: f(1, "a")
PASSED: f(2, "b")

TestNG @DataProvider Properties

@DataProvider annotation has three properties:

  1. name: This is used to specify the data provider method name, default value is the function name. We can use it to give a proper name to our Data Provider method.
  2. indices: This is a very important property, this allows us to specify the indexes we want to use for our testing purpose. This is helpful when our data provider method may have a large number of values and we want to run our test cases for only a few of them. Its default value is “all” so tests will run for all the inputs generated by the data provider method.
  3. parallel: This parameter tells TestNG to run tests in parallel. So the input to test methods can be injected in random order. By default inputs are injected in the order and tests are executed sequentially.

Let’s see an example where I have changed the name of DataProvider method and also using only a few of the inputs generated by the method.


package com.journaldev.dataprovider;

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

public class Test4 {
  @Test(dataProvider = "fooDP")
  public void foo(Integer n) {
	  System.out.println("Inside foo, input = "+n);
  }

  @DataProvider(name="fooDP", indices= {1,3,5,7,9})
  public Object[] dp() {
	  Object[] ints = new Object[100];
	  for(int i =0 ; i<100; i++) {
		  ints[i] = 100+i;
	  }
    return ints;
  }
}

Now when we run our test class, it generates following output.


[RemoteTestNG] detected TestNG version 6.14.3
Inside foo, input = 101
Inside foo, input = 103
Inside foo, input = 105
Inside foo, input = 107
Inside foo, input = 109
PASSED: foo(101)
PASSED: foo(103)
PASSED: foo(105)
PASSED: foo(107)
PASSED: foo(109)

Now change the DataProvider method to run in parallel.


@DataProvider(parallel=true, name="fooDP", indices= {1,3,5,7,9})
public Object[] dp() {
  //method logic
}

The output generated when test class is executed:


[RemoteTestNG] detected TestNG version 6.14.3
Inside foo, input = 105
Inside foo, input = 107
Inside foo, input = 109
Inside foo, input = 103
Inside foo, input = 101
PASSED: foo(103)
PASSED: foo(105)
PASSED: foo(107)
PASSED: foo(109)
PASSED: foo(101)

Note that indices values don't cause any exceptions if the DataProvider is returning fewer numbers of objects than specified. So below DataProvider implementation will work fine too.


@DataProvider(parallel = true, name = "fooDP", indices = { 1, 3, 5, 7, 9 })
public static Object[] dp() {
	Object[] ints = new Object[3];
	ints[0] = 0;ints[1] = 1;ints[2] = 2;
	return ints;
}

TestNG DataProvider Class

TestNG is designed to be loosely coupled, so it's no wonder that we have the option to separate our test class with data provider class. We can use @Test property dataProviderClass to provide the name of the class to look for DataProvider method.

If this attribute is specified, the data provider method needs to be static in the specified class. If not specified, the data provider will be looked in the test class or its superclasses.

Here is an example of separate TestNG DataProvider class.


package com.journaldev.dataprovider;

import org.testng.annotations.DataProvider;

public class Test4DataProvider {

	@DataProvider(parallel = true, name = "fooDP", indices = { 1, 3, 5, 7, 9 })
	public static Object[] dp() {
		Object[] ints = new Object[100];
		for (int i = 0; i < 100; i++) {
			ints[i] = 100 + i;
		}
		return ints;
	}
}

Our updated Test class:


package com.journaldev.dataprovider;

import org.testng.annotations.Test;

public class Test4 {
	@Test(dataProvider = "fooDP", dataProviderClass = Test4DataProvider.class)
	public void foo(Integer n) {
		System.out.println("Inside foo, input = " + n);
	}
}

TestNG DataProvider with Factory

We can use DataProvider with Factory methods too. Below code snippet shows a simple example for using DataProvider with Factory annotation.


@Factory(dataProvider = "dp")
public Object[] getTestClasses(String s) {
	Object[] tests = new Object[2];
	tests[0] = new Test1(s);
	tests[1] = new Test2();
	return tests;
}

@DataProvider
public Object[] dp() {
	return new Object[] {"A", "B"};
}

I have explained the use case and the benefits of using DataProvider with Factory in detail at TestNG Factory annotation tutorial.

Summary

TestNG is an awesome testing framework. DataProvider is one of the best features it has that I totally love and use a lot in my test cases.

You can download project source code from our GitHub Repository.

Comments

  1. kiran says:

    how to run this rest assured classes please help me

Leave a Reply

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

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages