Mastering Data-Driven Testing in Selenium with TestNG DataProvider
- subymmathew
- Sep 2
- 5 min read
In modern automation frameworks, handling multiple sets of test data is a crucial part of building scalable and maintainable test suites. Instead of hardcoding values or duplicating test methods for each input, we can leverage TestNG’s @DataProvider annotation to implement true data-driven testing.
This post summarizes a step-by-step guide on how to integrate Excel-based test data into your Selenium tests using TestNG.
Why Data Providers?
A DataProvider in TestNG is a method that supplies test data to test methods. Instead of writing separate test cases for each dataset, you can:
Store test data in an external source (like Excel).
Fetch it using a utility class.
Feed it directly into your test case through the @DataProvider annotation.
This ensures reusability, scalability, and clean code.
Imagine you have a login test that accepts a username and password. If you need to test with five different sets of credentials, one naive approach would be to duplicate the test five times. This leads to bloated, repetitive code.
TestNG solves this problem with @DataProvider, which allows a single test method to run multiple times with different inputs. This not only improves reusability but also makes your framework much cleaner.
DataProvider Syntax:
The TestNG DataProvider is used in the following manner:

After the introduction of this syntax, there are a few things that you should take note of before writing a test case:
The TestNG DataProvider (the annotation part) contains only one single attribute, which is its name. It is always a string type in nature. For example, "name_of_dataprovider", as mentioned above.
DataProviders are not declared on top of the functions like TestNG Parameters but have a method of their own, which in regular speaking terms called a dataprovider method. For example, dpMethod here.
If the tester has not specified the name of the dataprovider, then the method name becomes the dataprovider name by default.
TestNG dataprovider returns a 2d list of objects.
The method then performs a data-driven test for each value that you have specified.
The dataprovider name calls the dataprovider method, and if there is no name specified by the tester, then the dataprovider method is the default name used in the receiving @Test case.
How To Use DataProvider In TestNG?

You need to import the DataProvider in TestNG by adding the line import org.testng.annotations.DataProvider; Unlike parameters in TestNG, the dataproviders can be run directly through the test case file.
Inherited DataProvider In TestNG
Dataprovider and the test case method can also be in two different classes. It is inheriting the dataprovider since we are inheriting it from another file. It's required to slightly improve the above code to run the @Test case like below.

The only difference here is that along with the name of data Provider; you now need to provide the dataProviderClass by the same attribute name.

DataProviders With Method As A Parameter
In the above cases, we have used one way to provide the dataprovider to another test class, i.e., by creating a dataprovider method for each method that will be calling it. It is alright, but we will unnecessarily increase the lines of code in the java file, which is considered a bad coding practice. If you can do the same job for seven lines instead of 10, you should go for it. It is the reason that dataproviders also accept a method as a parameter, and then we can just check the method name and provide the parameters according to it.
1. Start by adding the following Maven dependencies




2. Prepare Test Data in Excel
Start by creating an Excel file (e.g., testdata.xlsx) where each row contains a set of values. For a registration form, columns could include:
First Name
Last Name
Address Line 1
Address Line 2
City
State
Zip Code
Email
Your test will fetch each row and treat it as a separate test execution.
3. Create a Utility Class for Reading Excel
Using Apache POI, build a utility method to read data from Excel.
This method:
Opens the Excel file.
Loops through rows (starting from row 2, since row 1 typically contains headers).
Fetches cell values into Java objects.
Stores each row in an Object[], and collects them inside an Array List.
Finally, the method returns this collection, which TestNG can use to feed your test.
3. Define the Data Provider Method
In your test class, create a method annotated with @DataProvider.
This method will call your Excel utility and return an Iterator<Object[]>. Each Object[] represents one row of test data.
Example:

4. Connect Data Provider to Your Test
Now, annotate your test with:

Here, the number of parameters must match the number of columns in your Excel file. TestNG automatically maps each column in a row to the corresponding parameter.
5. Execute the Tests
When you run the test:
TestNG fetches data from Excel.
The test executes once per row of data.
For example, if you have 5 rows, the same test method will execute 5 times—each with a different input set.
No need to duplicate test logic for multiple scenarios.
Benefits of Using @DataProvider
1. Enables Data-Driven Testing
DataProviders allow you to run the same test multiple times with different sets of input data.
Instead of writing multiple test methods for each scenario, you can maintain one test method and supply various data sets.
This is especially useful for forms, login scenarios, and any functionality that needs to be tested with multiple input combinations.
2. Reduces Code Duplication
Without DataProviders, you would have to write separate test methods for each data set.
With a DataProvider, one test method can handle all variations of data, making your code cleaner, shorter, and easier to maintain.
3. Improves Test Maintainability
When test data changes (e.g., usernames, passwords, product IDs), you only need to update the data source (Excel, CSV, database) instead of editing test methods.
This separation of data and logic makes tests easier to maintain over time.
4. Enhances Flexibility
DataProviders can supply data from any source:
Excel, CSV, JSON, databases, or even dynamically generated data.
This makes your tests highly flexible, allowing you to scale or adapt without changing the core test logic.
5. Supports Parallel Execution
TestNG can run tests in parallel using DataProviders.
This reduces overall test execution time, which is useful for large data sets or regression testing.
6. Automatic Handling of Multiple Test Cases
Each row of data from the DataProvider automatically triggers a separate test iteration.
TestNG handles the iteration, logging, and reporting for each data set individually, making it easy to see which inputs passed or failed.
7. Encourages Best Practices
Separating data from test logic encourages modularity and reusability.
Makes it easier for teams to collaborate: testers can focus on creating data sets, while developers focus on test logic.
Conclusion
· The TestNG DataProvider is a powerful feature that brings true flexibility to your test automation framework. By combining it with Excel and Apache POI, you can easily build a data-driven framework that scales for real-world projects.
· This approach eliminates redundancy, keeps your tests clean, and makes handling large sets of test data straightforward.
· If you’re building automation with Selenium, mastering DataProviders is a must-have skill—both for productivity and for interview preparation.
Thank you!


