---
title: "How To Use DataProvider In TestNG With Examples | CoderPad"
description: "When it comes to testing there are many tools and techniques available. In this post, we'll discuss how to use DataProvider with TestNG."
canonical: "https://coderpad.io/blog/development/how-to-use-dataprovider-in-testng-with-examples/"
last_modified: "2026-04-15 16:20:49"
---
[Back to blog](https://coderpad.io/blog/)

# How To Use DataProvider In TestNG With Examples

April 12, 2023 4 Minute Read

By Keshav Malik

[Development](https://coderpad.io/blog/development/)

Testing is an essential part of software development, especially in the agile world. By testing in the early stages of development, developers can ensure that their code is of high quality and meets customer expectations.

In the agile world, it’s especially important to test early and often, as changes are made frequently and new features are added constantly. By testing regularly, developers can catch bugs early and prevent them from becoming major issues later on.

There are many tools and techniques available for testing. In this post, we’ll discuss how to use DataProvider with [TestNG](https://testng.org/).

## Prerequisites

Before we get started with the guide, it’s important that you fulfill all the prerequisites. This will ensure that you have all the necessary tools and knowledge to complete the guide successfully. You’ll need

- an understanding of the [Java](https://docs.oracle.com/en/java/) programming language,
- Java IDE such as [NetBeans](https://netbeans.apache.org/) (or online IDE like Coderpad), and
- a basic understanding of testing frameworks like JUnit or NUnit.

Once you’ve fulfilled all of the prerequisites, you’re ready to get started with the guide.

## What Is TestNG?

TestNG is a testing framework for the Java programming language. The name “TestNG” is a play on the word “testing.” The initials “NG” stand for “next generation.”

The goal of TestNG is to help developers perform a wider range of test cases— [unit, functional, end-to-end, integration](https://coderpad.io/blog/development/integration-tests-vs-unit-tests-integration-matters-more/), etc.—with more ease and flexibility.

TestNG is an open-source project hosted on GitHub and is released under the Apache software license. It was developed by Cédric Beust and was first released in July 2004.

TestNG enables a user to create tests that are more maintainable and easier to understand. It also provides support for a variety of features, such as data-driven testing, parameterized testing, and multi-threaded testing.

## Understanding TestNG With an Example

Let’s check out an example of how to use TestNG to write a test case using the following code snippet:

```
@Test

public void TestLogin(){

    WebDriverManager.chromedriver().setup();

    WebDriver driver = new ChromeDriver();

    driver.manage().window().maximize();

    String username = "username", password = "*****";

    driver.get("<https://demosite.executeautomation.com/>");

    driver.findElement(By.name("UserName")).sendKeys(username);

    driver.findElement(By.name("Password")).sendKeys(password);

    driver.findElement(By.name("Login")).submit();

    Assert.assertEquals("User Form", driver.findElement(By.tagName("h2")).getText());

    driver.quit();

}Code language: Java (java)
```

In the above code snippet, we’re going to the URL [https://demosite.executeautomation.com](https://demosite.executeautomation.com/), which has a login page that accepts any username and password.

There, we’ll assert user form text from the next page after a successful login.

The goal of the above code is to understand how to use the`@ Test`annotation from TestNG and write an automation test case using Selenium in Java.

### How to Perform Parameterization Using TestNG

To perform parameterization, we use the`@Parameters({"parameter1", "parameter2", ..})`annotation.

Test methods will read these parameters from XML files, as shown below:

```
<?xml version="1.0" encoding="UTF-8"?>

<suite name="TestSuite">

    <parameter name="username" value="username" />

    <parameter name="password" value="*****" />

    <test name="TestLogin">

        <classes>

            <class name="DataProvider.TestNg" />

        </classes>

    </test>

</suite>Code language: Java (java)
```

Here, the parameter’s name from the XML file should match the name we mentioned in the annotation in the test method above.

@Test

```
@Parameters({"username", "password"})

public void TestLogin(String username, String password){

    WebDriverManager.chromedriver().setup();

    WebDriver driver = new ChromeDriver();

    driver.manage().window().maximize();

    driver.get("<https://demosite.executeautomation.com/>");

    driver.findElement(By.name("UserName")).sendKeys(username);

    driver.findElement(By.name("Password")).sendKeys(password);

    driver.findElement(By.name("Login")).submit();

    Assert.assertEquals("User Form", driver.findElement(By.tagName("h2")).getText());

    driver.quit();

}Code language: Java (java)
```

The use of parameterization in TestNG gives you the power to perform data-driven testing more efficiently.

### What Is DataProvider in Java?

DataProvider is like a container that passes the data to our test methods so that our single test method can execute itself with multiple data sets.

To make any method act as a DataProvider, we need to use this annotation—`@DataProvider`—which will return a 2D array of an object where columns are the arguments needed in one test execution and rows are the number of data passed in each execution.

```
@Test(dataProvider="LoginDataProvider")

public void TestLogin(String username, String password){

    WebDriverManager.chromedriver().setup();

    WebDriver driver = new ChromeDriver();

    driver.manage().window().maximize();

    driver.get("<https://demosite.executeautomation.com/>");

    driver.findElement(By.name("UserName")).sendKeys(username);

    driver.findElement(By.name("Password")).sendKeys(password);

    driver.findElement(By.name("Login")).submit();

    Assert.assertEquals("User Form", driver.findElement(By.tagName("h2")).getText());

    driver.quit();

}

@DataProvider(name = "LoginDataProvider")

public Object[][] LoginData(){

return new Object[][] 

{

        { "abc", "***" },

        { "xyz", "***" },

        { "mno", "***" }

};

}Code language: Java (java)
```

Now, when we run the test above, the same test will run the method`TestLogin()`three times as LoginData returns three rows of data.

If you don’t specify any name while declaring the DataProvider method and simply use`@DataProvider`while calling it in the test, we need to use the method’s name as a data provider name. In the above case, it’ll be`@Test(dataProvider="LoginData")`.

Also, if DataProvider is created in a different class, then we need to include the dataProviderClass argument:

```
@Test(dataProvider="<name>",dataProviderClass=<dataProvider_ClassFileName>.class)Code language: Java (java)
```

Here are a few samples:`DataSets.java:`

```
public class DataSets{

@DataProvider(name = "LoginDataProvider")

public Object[][] LoginData(){

    return new Object[][] {

        { "abc", "***" },

        { "xyz", "***" },

        { "mno", "***" }

    };

}

}Code language: Java (java)
```

TestAuthorization.java

```
public class TestAuthorization{

@Test(dataProvider="LoginDataProvider", dataProviderClass="DataSets.class")

public void TestLogin(String username, String password){

    WebDriverManager.chromedriver().setup();

    WebDriver driver = new ChromeDriver();

    driver.manage().window().maximize();

    driver.get("<https://demosite.executeautomation.com/>");

    driver.findElement(By.name("UserName")).sendKeys(username);

    driver.findElement(By.name("Password")).sendKeys(password);

    driver.findElement(By.name("Login")).submit();

    Assert.assertEquals("User Form", driver.findElement(By.tagName("h2")).getText());
    driver.quit();
    }

}Code language: Java (java)
```

### Why Use DataProvider in TestNG?

If we have to test the same test case for multiple data (username and password) without using DataProvider, it’ll look as shown below.

```
@Test

public void TestLogin(){

    WebDriverManager.chromedriver().setup();

    driver = new ChromeDriver();

    driver.manage().window().maximize();

    Login("abc", "***");

    Login("mno", "***");

    Login("xyz", "***");

    Login("def", "***");

    Login("dod", "***");

   driver.quit();
}

public void Login(String username, String password){

    driver.get("<https://demosite.executeautomation.com/>");

    driver.findElement(By.name("UserName")).sendKeys(username);

    driver.findElement(By.name("Password")).sendKeys(password);

    driver.findElement(By.name("Login")).submit();

    Assert.assertEquals("User Form", driver.findElement(By.tagName("h2")).getText());

}Code language: Java (java)
```

Conclusion

We hope you enjoyed reading about DataProvider in testing with examples.

TestNG is a testing framework that you can use with any Java project. It’s used in Agile methodology to test any application’s functionality.

DataProvider is a feature of the TestNG library that allows a developer to run the same suite of test cases with different data sets. It helps the developer to [test](https://coderpad.io/features/) the application with different values. You can use this to test the business logic of any application.

We hope that you found this post informative and helpful.

This post was written by Keshav Malik. [Keshav](https://theinfosecguy.xyz/) is a full-time developer who loves to build and break stuff. He is constantly on the lookout for new and interesting technologies and enjoys working with a diverse set of technologies in his spare time. He +loves music and plays badminton whenever the opportunity presents itself.

## Subscribe for more CoderPad insights

## Trending Articles

Previous slide Next slide [Development

## What is Prompt Engineering?

Introducing AI tools such as ChatGPT and Bard helps increase the efficiency of tasks such as data generation, code generation and completion, text summarization, etc. How were these AI tools trained to answer almost every question you ask them? The answer, prompt engineering, is discussed in this blog post.

Read the post](https://coderpad.io/blog/development/what-is-prompt-engineering/) [Development

## A Guide To Using React’s useCallback() Hook

Are you looking to optimize your React applications? Have you heard of the useCallback hook? useCallback is a powerful React hook that helps you optimize your application by preventing unnecessary re-renders. In this post, we'll dive into React's useCallback() hook, define referential equality and callback functions and how to tie it all together.

Read the post](https://coderpad.io/blog/development/a-guide-to-using-reacts-usecallback-hook/) [Development

## MySQL vs. PostgreSQL: How Do They Compare?

A database is a convenient tool that allows users to access information, maintain records, and apply commonly used operations, such as insertion, modification, deletion, and data organization. Learn about MySQL vs PostgreSQL, their features, advantages, and disadvantages. Finally, check out which one is the best choice for you.

Read the post](https://coderpad.io/blog/development/mysql-vs-postgresql-how-do-they-compare/) [Development

## PostgreSQL LIKE Operator: A Detailed Guide

When using databases, searching for values that match a given pattern is a familiar necessity. Learn how you can retrieve entries with queries including a particular string using Postgres LIKE operator.

Read the post](https://coderpad.io/blog/development/postgresql-like-operator-a-detailed-guide/) [Development

## How To Get Up And Running With Django Migrations: A Guide

Django migrations are a way of handling the application of changes to a database schema. By following the steps outlined in this guide, you can easily set up and use migrations in your Django projects.

Read the post](https://coderpad.io/blog/development/how-to-get-up-and-running-with-django-migrations-a-guide/) [Development

## Writing A Parameterized Test In JUnit With Examples

A JUnit parameterized test allows you to decouple data from structure, generating cleaner and more reliable tests. Learn more in this guide.

Read the post](https://coderpad.io/blog/development/writing-a-parameterized-test-in-junit-with-examples/) [Development

## How To Use Assert In JUnit With Examples

The JUnit Assert class is what you use to write unit testing assertions in JUnit. Learn more about it in this guide, full of examples.

Read the post](https://coderpad.io/blog/development/how-to-use-assert-in-junit-with-examples/) [Development

## A Simple Guide To TypeORM: What It Is And How To Use It

Considering current languages have object-oriented programming features, most database management systems are based on the relational model, with tables instead of objects. For these reasons, mapping relational objects is very important in development. In this article, you will learn what TypeORM, one of the main tools used today, is.

Read the post](https://coderpad.io/blog/development/a-simple-guide-to-typeorm-what-it-is-and-how-to-use-it/) [Development

## Using PreparedStatement in JDBC With Examples

In this post, we'll see why and how to work with JDBC and PreparedStatement. We will also learn along with examples using code.

Read the post](https://coderpad.io/blog/development/using-preparedstatement-in-jdbc-with-examples/) [Development

## React vs React Native: The Differences Explained

React and React Native are similar in that they both allow web developers to build applications based on the unique concept of components. But the two frameworks do this in radically different ways. In this post, we cover the key differences between react and react native frameworks, implementation, and which is an appropriate choice.

Read the post](https://coderpad.io/blog/development/react-vs-react-native-the-differences-explained/) [Development

## Logging in Django

The most challenging part of fixing a problem is identifying it. Unfortunately, this statement is exceptionally true when it comes to debugging. Learn how to use the power of logging into your Django applications to improve your debug process in this article.

Read the post](https://coderpad.io/blog/development/logging-in-django/) [Development

## How To Get Started With TypeScript in Node.js

TypeScript was the third most popular programming language in 2022, following Rust and Python. It’s widely admired by the JavaScript community and used by many companies to build front-end and back-end applications. In this post, let's dive into TypeScript, learn how to get started with TypeScript in a Node.js application, and then cover ts-node.

Read the post](https://coderpad.io/blog/development/how-to-get-started-with-typescript-in-node-js/)

## Hire with confidence.

See why engineering teams trust CoderPad to evaluate developers with realistic, AI-ready hiring workflows.

[Request a demo](https://coderpad.io/sales/)
