welcome to XRM blog

Keep in touch with latest CRM/ERP articles

To remain competitive your organisation must be efficient across the business process spectrum. To do so you need to take sound decisions based on a balance between the cost and risk. To do so you will be heavily dependent on your content management in itself needs...

image
Blog

Data-Driven Automation with TestNG

By Vishek Nigam on 5/12/2025

In the world of test automation, efficiency and reusability are key. Writing a test for each possible input is not scalable. That’s where Data-Driven Testing (DDT) steps in. When combined with the power of TestNG, DDT becomes incredibly simple and effective. 

In this blog, we’ll walk through what DDT is, how to implement it using TestNG, and how it stacks up against other testing frameworks. 

What is Data-Driven Testing 

This testing approach focuses on running the same test case multiple times with different data inputs to validate various scenarios. We write our test once and execute it multiple times with different input values. This is extremely useful for: 

Login forms 

Registration tests 

Validation scenarios 

Boundary testing 

Instead of writing multiple test cases, we just supply different data sets to one test method. 
 
Imagine we're testing a login form. We need to verify that it works correctly with a bunch of different usernames and passwords – valid ones, invalid ones, edge cases, the whole shebang. Instead of writing a separate test for each combination, data-driven testing let’s feed all those inputs into a single test function. The test runs once for each set of data, making our test suite way more efficient and maintainable. 

Why TestNG? 

TestNG is a highly capable testing tool that brings flexibility and efficiency to Java test automation. It offers a bunch of cool features that make data-driven testing a breeze: 

1. Annotations: TestNG uses annotations to define test methods, set up fixtures, and configure data providers. It simplifies the structure of our tests, making them easier to follow. 

2. Data Providers: In TestNG, data providers are responsible for passing multiple sets of data to a single test method. They can return data from various sources, like CSV files, Excel spreadsheets, databases, or even hardcoded arrays. 

3. Parameterization: TestNG makes it easy to pass data from our data providers to our test methods as parameters. This allows us to write generic test methods that can handle different sets of data. 

4. Reporting: TestNG generates detailed reports that show us which tests passed, which failed, and how long each test took to run. This makes it easy to identify and fix problems in our code. 

Getting Started with Data-Driven Testing in TestNG 

Let’s jump into a simple example to demonstrate how DDT works with TestNG. 

Step 1: Add TestNG to our Project 
 If We're using Maven, include this in our pom.xml: 
 <dependency> 

    <groupId>org.testng</groupId> 

    <artifactId>testng</artifactId> 

    <version>7.8.0</version> 

    <scope>test</scope> 

</dependency> 
 Step 2: Create a Data Provider Method 
 This method returns a 2D array of test data. 
 import org.testng.annotations.DataProvider; 

public class TestData { 

    @DataProvider(name = "loginCredentials") 

    public Object[][] getCredentials() { 

        return new Object[][] { 

            {"admin", "admin123"}, 

            {"user1", "pass1"}, 

            {"user2", "pass2"} 

        }; 

    } 


 Step 3: Link Test Method to DataProvider 
 import org.testng.annotations.Test; 

public class LoginTest { 

    @Test(dataProvider = "loginCredentials", dataProviderClass = TestData.class) 

    public void loginTest(String username, String password) { 

        System.out.println("Attempting login with: " + username + " / " + password); 

        // Your login logic goes here 

        // Assertions can be added here to validate outcome 

    } 


 Advanced Data Sources for Data-Driven Testing with TestNG 
 TestNG's built-in @DataProvider is excellent for static data. But in real-world applications, we often need to pull data from external files like: 

✅ Excel (.xls/.xlsx) 

✅ CSV 

✅ JSON 

✅ Databases 
 
Here we will take example with Excel. 
 Step 1: Create a excel file with name as Loginform.xlsx

Step 2: Add required dependencies (Maven) 
 <dependency> 

    <groupId>org.apache.poi</groupId> 

    <artifactId>poi</artifactId> 

    <version>5.2.3</version> 

</dependency> 

<dependency> 

    <groupId>org.apache.poi</groupId> 

    <artifactId>poi-ooxml</artifactId> 

    <version>5.2.3</version> 

</dependency> 
 Step 3: Excel Reader Utility 
 import org.apache.poi.ss.usermodel.*; 

import java.io.FileInputStream; 

public class ExcelUtils { 

    public static Object[][] getExcelData(String filePath, String sheetName) throws Exception { 

        FileInputStream fis = new FileInputStream(filePath); 

        Workbook workbook = WorkbookFactory.create(fis); 

        Sheet sheet = workbook.getSheet(sheetName); 

        int rowCount = sheet.getPhysicalNumberOfRows(); 

        int colCount = sheet.getRow(0).getLastCellNum(); 

        Object[ ][ ] data = new Object[rowCount - 1] [colCount]; 

        for (int i = 1; i < rowCount; i++) { 

            Row row = sheet.getRow(i); 

            for (int j = 0; j < colCount; j++) { 

                data[i - 1][j] = row.getCell(j).toString(); 

            } 

        } 

        workbook.close(); 

        return data; 

    } 


 Step 4: Data Provider Using Excel 
 @DataProvider(name = "excelData") 

public Object[][] getExcelTestData() throws Exception { 

    return ExcelUtils.getExcelData("src/test/resources/testdata.xlsx", "Loginform"); 


 Conclusion: Using external data sources in TestNG let us build highly maintainable, scalable, and real-world test cases. Whether we're testing 1,000+ inputs from Excel or querying data directly from our staging database, TestNG can handle it with ease.

#Data Provider Using Excel
#Data-Driven
#Data-Driven Automation
#DataProvider
Blog Calendar
Blog Calendar List
2025 Jun  15  4
2025 May  47  9
2025 Apr  34  6
2025 Mar  53  7
2025 Feb  41  6
2024 Nov  11  1
2024 Aug  7  1
2024 Apr  58  4
2024 Mar  156  4
2024 Feb  419  3
2024 Jan  33  7
2023 Dec  39  6
2023 Nov  558  5
2023 Oct  770  12
2023 Sep  1810  9
2023 Aug  555  6
2023 Jul  47  6
2023 Jun  26  4
2023 May  44  5
2023 Apr  85  5
2023 Mar  223  6
2023 Feb  172  5
2023 Jan  80  4
2022 Dec  96  7
2022 Nov  294  2
2022 Sep  13  1
2022 Aug  32  2
2022 Jun  11  2
2022 May  6  2
2022 Apr  12  2
2022 Mar  2  1
2022 Feb  2  1
2022 Jan  1  1
2021 Dec  4  1
2021 Nov  2  1
2021 Oct  2  1
2021 Sep  14  1
2021 Aug  49  5
2021 Jul  51  4
2021 Jun  1857  5
2021 May  42  3
2021 Apr  2257  3
2021 Mar  215  5
2021 Feb  2767  7
2021 Jan  4186  9
2020 Dec  584  7
2020 Sep  80  3
2020 Aug  789  3
2020 Jul  139  1
2020 Jun  98  3
2020 Apr  103  3
2020 Mar  19  2
2020 Feb  34  5
2020 Jan  48  7
2019 Dec  17  4
2019 Nov  40  1
2019 Jan  23  2
2018 Dec  142  4
2018 Nov  68  3
2018 Oct  18  3
2018 Sep  1274  11
2018 Aug  7  2
2018 Jun  21  1
2018 Jan  73  2
2017 Sep  590  5
2017 Aug  17  1
2017 Jul  17  2
2017 Jun  65  2
2017 May  21  1
2017 Apr  39  2
2017 Mar  139  4
2017 Feb  850  4
2016 Dec  208  3
2016 Nov  1055  8
2016 Oct  341  10
2016 Sep  809  6
2016 Aug  39  1
2016 Jun  1893  6
2016 May  114  3
2016 Jan  72  2
2015 Dec  740  6
2015 Nov  4  1
2015 Oct  13  1
2015 Sep  1471  6
2015 Aug  14  1
2015 Jul  129  2
2015 Jun  11  1
2015 May  20  1
2015 Apr  30  3
2015 Mar  80  3
2015 Jan  5350  4
2014 Dec  18  1
2014 Nov  2260  4
2014 Oct  69  1
2014 Sep  107  2
2014 Aug  5337  1
2014 Jul  49  2
2014 Apr  2599  12
2014 Mar  307  17
2014 Feb  223  6
2014 Jan  1510  16
2013 Dec  21  2
2013 Nov  695  2
2013 Oct  256  3
2013 Sep  13  1
2013 Aug  40  3
2013 Jul  214  1
2013 Apr  62  6
2013 Mar  2395  10
2013 Feb  131  3
2013 Jan  352  2
2012 Nov  62  2
2012 Oct  519  10
Tag Cloud
Interested in our services? Still not sure about project details? get a quote