September 7, 2020 Jonny Steiner

TestNG DataProviders – An End-to-End Overview and Demo

Share this knowledge!

Cedric Beust created the popular test automation framework TestNG in 2003 using the Java programming language. It is similar to JUnit and NUnit but solves some of their shortcomings such as poor configuration control, static programming models, lack of dependency testing, etc. It is designed to cover a wide range of testing projects including functional, unit, and integrated system testing. TestNG offers powerful features that not only enable testers to perform complex testing tasks but also make functionalities easy to use. While TestNG is mostly popular for its annotations, TestNG DataProviders is another handy feature that makes data-driven testing easy and efficient. With data parameterization, Data listeners, DataProviders, HTML reports, logs, test case grouping, and prioritizing and parallel testing abilities, TestNG has quickly become a favorite for most testers.

What are TestNG DataProviders?

One of the important aspects of TestNG is data-driven testing. DataProviders feature is a part of data-driven testing wherein it provides different values to a test case in a single execution. It means you can run a single test case once but can execute a method with different data values. For instance, you can test a registration page with different inputs but you will be executing the test only once. TestNG data-providers feature passes all these values to the test case one at a time so that it tests different data sets in a single execution.

The need for TestNG DataProviders

TestNG Parameters is a similar feature that provides values to functions via an XML file. Using parameters, you can run multiple functions with a single data set or run a single function with multiple data sets. However, the challenge with parameters is that you can execute the test case with that value only once. In a standard testing project, you need to run test cases with multiple values. In such a case, you have to execute the test case multiple times with different data sets. TestNG data-providers solve this challenge by enabling you to execute the test case only once but use different data sets. It provides arrayed data values to test methods via Annotations.

How does it work?

TestNG DataProviders feature uses the following syntax. For eg:

@DataProvider (name = “ExperitestDataProvider”)
public Object[][] ExperitestDPMethod() {
return new Object [][] { values}
}

If you check the syntax, it uses TestNG annotations. Only one parameter is used here which is the name. This parameter takes only a string value as the name of the data provider. They have a method of their own. For eg: ExperitestDPMethod() here. By default, TestNG takes the name of the method as the name of the data provider when not specified.

When you execute the test case, the data provider method is called by the data provider which returns a list of 2D objects to perform data-driven tests for each data set.

Installation

Here are the following tools required to use the TestNg DataProviders feature:

  1. Java
  2. Selenium
  3. Eclipse IDE
  4. TestNG

Step 1: Install Java

To run TestNG, you need the Java Development Kit (JDK) 8.0 or higher. Visit this link to download and install Java on your machine.

https://www.oracle.com/in/java/technologies/javase-downloads.html

Java SE 14.0.2 is the latest version. So, download the executable file and double-click to install the software.

Step 2: Configure Environment Variable

After installing Java, you should specify the Java path to the system. To do so:Open System -> Advanced System Properties -> Advanced -> Environment Variables.

testng dataproviders

For ‘user variables for User’, enter:

  • C:\Program Files\Java\jdk-14.0.2

For the System Variables path, enter the bin folder path:

  • C:\Program Files\Java\jdk-14.0.2\bin

Now, Java is ready for use.

Step 3: Install Eclipse

To create and edit Java scripts, you need a Java IDE. Eclipse and IntelliJ are two popular IDEs for Java. To install Eclipse, visit the following link:

https://www.eclipse.org/downloads/

The latest version is Eclipse IDE 2020-06. Once the software is downloaded, double-click it, and install the software.

eclipse testng providers

There are multiple versions of Eclipse. So, choose ‘Eclipse for Java Developers’ and proceed with the installation.

eclipse testng dataproviders

Once the installation is completed, open the IDE. Every time you open the IDE, it will prompt you to choose a folder for the workspace. You can proceed with the default location or choose a new one.

workspace testng dataproviders

When you open Eclipse for the first time, it will display a welcome screen. Now, IDE is ready for use.

eclipse testng dataproviders

Step 4: Install Selenium

Selenium is a popular open-source test automation framework that has been quite popular in recent times. Visit this link to download Selenium software:

https://www.selenium.dev/downloads/

Download the Selenium Standalone Server which comes as a jar file. The latest stable version is 3.141.59. You don’t have to install it. Simply save it to your machine.

Step 5: Install TestNG

In the earlier versions of Eclipse, TestNG was available in the Eclipse Marketplace wherein you can simply click install and add TestNG to Eclipse. With the latest version, you have to manually download TestNG and add them as library files. Visit this link to download TestNG:

https://dl.bintray.com/testng-team/testng-eclipse-release/

Click on the Zipped link and choose the latest version and save the files to your device.

testng dataproviders zip

To add TestNG to Eclipse, open the IDE and click on ‘Install New Software’. In the next screen, click on add and manually add the files.

Alternatively, you can directly provide the TestNG link there so that the tool will automatically download the files.

testng dataproviders available software

Select TestNG and click on next and accept the license agreement and click finish. Restart Eclipse to complete the TestNG configuration procedure.

To check if TestNG is properly installed, click Ctrl+N and see if TestNG is listed there.

wizard testng dataproviders

Now, TestNG is ready for use.

First Test with TestNG DataProviders

Now that all the tools are installed, let us run the first test to see how TestNG DataProviders work.

Open the Eclipse IDE and click on New -> Java Project. Provide a name to the project and click next.

new project

On the next screen, click on Library and click on ‘Add external jar’.

external jar

Select the Selenium Standalone Server jar file and click ok. Selenium gets added to your project.

Now, click on ‘Add Library’ and select TestNG in the next screen to add TestNG to your project.

add library

Selenium and TestNG are added to your project along with the Java runtime environment.

Click on New -> Package to create a new package.

new package

Create a new class in that package. Eg: ExperitestDataProviders.java

Now, add this code to the class. Note that it contains TestNG DataProviders.

package ExperitestDataProviders;

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

public class ExperitestTestNGDP {

	@DataProvider (name = "Experitest-DataProvider")
	 public Object[][] ExperitestDPMethod(){
	 return new Object[][] {{"Experitest1"}, {"Experitest2"}, {"Experitest3"}, {"Experitest4"}, {"Experitest5"}};
}
	@Test (dataProvider = "Experitest-DataProvider")
    public void ExperiTest (String val) {
        System.out.println("Passed Parameter Is : " + val);
    }
}
  • Here, we have defined a Data provider with a name ‘ Experitest-DataProvider’ and provided 5 values (Experitest1, Experitest2, etc.)
  • During the test (@Test), we have called the data provider.
  • We wrote a method with a string parameter and stored the value in ‘val’
  • The test case will take the string value and print it.

When you run this test case once, it will execute it 5 times and each time one string value is printed. So, using the DataProviders feature, we have run a single test case multiple times.

testng dataproviders

This is the output that shows that 5 tests ran and 5 tests passed.

testng variables

SeeTest Cloud Demo

Here is another test demo that runs in the SeeTest Cloud. This test case opens a web browser in the cloud and then opens the Google website in that browser. It then types ‘Experitest’ in the search box and searches the web for that keyword. Then, it will open the Google site again and search for the keyword ‘SeeTest’. Here, we have written only one test case but we have defined a TestNG Data Provider called ‘ Experitest-DataProvider’ which provides two values to the test case.

   @DataProvider (name = "Experitest-DataProvider")
			 public Object[][] ExperitestDPMethod(){
			 return new Object[][] {{"Experitest"}, {"SeeTest"}};
			     }

So, with the data provider feature, you are running a single test case twice here.

Here is the code:

package ExperitestDataProviders;

import org.openqa.selenium.*;
import org.openqa.selenium.remote.BrowserType;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import java.net.URL;

public class DPexample {
	private String host = "https://cloud.seetest.io";
	 private static final String ACCESS_KEY = 	 "Enter your access key here";
	     private RemoteWebDriver driver;
	     private URL url;
	     private DesiredCapabilities dc = new DesiredCapabilities();
	     @DataProvider (name = "Experitest-DataProvider")
		 public Object[][] ExperitestDPMethod(){
			 return new Object[][] {{"Experitest"}, {"SeeTest"}};
			     }
  
	@BeforeMethod
	     public void setUp() throws Exception {
	         url = new URL("https://cloud.seetest.io/wd/hub");
	         dc.setCapability(CapabilityType.BROWSER_NAME, BrowserType.CHROME);
	         dc.setCapability(CapabilityType.PLATFORM, Platform.ANY);
         dc.setCapability("accessKey", ACCESS_KEY);
	         dc.setCapability("testName", "Quick Start Chrome Browser Demo");
	         driver = new RemoteWebDriver(url, dc);
	     }
	     @Test (dataProvider = "Experitest-DataProvider")
	     public void ExperiTest (String val) {
	         driver.get("https://www.google.com");
	      new WebDriverWait(driver, 10).until(ExpectedConditions.presenceOfElementLocated(By.name("q")));
	         WebElement searchBar = driver.findElement(By.name("q"));
	         searchBar.click();
	         searchBar.sendKeys(val);
	         searchBar.sendKeys(Keys.ENTER);
			     }

     @AfterMethod
	     public void tearDown() {
	         System.out.println("Report URL: "+ driver.getCapabilities().getCapability("reportUrl"));
			         driver.quit();
			     }
}

Here is the result:

results

SeeTest is a comprehensive web and mobile test automation tool that enables you to easily run and manage your tests from an intuitive dashboard. It also offers a powerful reporting tool that makes it easy to analyze your tests.

Here is the report of the demo test:

test report

Here are the details of the test:

quick start

Summary

DataProviders is a handy feature for testers to write a single test case and run it multiple times with predefined data-set values. It also enables you to run multiple methods using a single data-set. TestNG DataProviders make data-driven testing simple and easy. SeeTest platform enables you to fully leverage the TestNG DataProviders feature to easily run test cases with different data sets while being able to generate customized reports.

Guy ArieliCTO

Share this knowledge!