Open In App

Data Driven Testing With TestNG

Last Updated : 20 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Data-Driven Testing with TestNG is a powerful approach that allows you to run the same test case with multiple sets of data. This methodology helps in achieving comprehensive test coverage and ensures that your application works correctly with various input values. By using external data sources like Excel or CSV files, you can easily manage and maintain your test data, making your testing process more efficient and reliable.

What is Data-Driven Testing?

Data-Driven Testing is a testing methodology where test logic is separated from test data. It means that you can execute a single test case with multiple data sets. This approach enhances test coverage, simplifies maintenance, and reduces the number of test scripts required.

How to Perform Data-Driven Testing?

To perform Data-Driven Testing, follow these general steps:

  1. Prepare Test Data: Store your test data in an external source (e.g., Excel, CSV, database).
  2. Configure Your Testing Framework: Use a testing framework that supports DDT, such as TestNG in Java.
  3. Read the Test Data: Implement code to read the test data from the external source.
  4. Execute Tests with Multiple Data Sets: Run your tests by iterating over the data sets.

Example Step-by-Step Guide

Let's implement Data-Driven Testing using TestNG in Java with an example. Suppose we want to test a login functionality with multiple sets of credentials.

Step 1: Prepare Test Data

Create an Excel file named LoginData.xlsx with the following content:

Login-xl
Login Excel File

Step 2: Add Required Dependencies

Ensure you have the necessary dependencies in your pom.xml if you are using Maven:

XML
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>7.4.0</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi</artifactId>
        <version>5.0.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
        <version>5.0.0</version>
    </dependency>
</dependencies>

Step 3: Create a Utility to Read Excel Data

Create a class ExcelUtils.java to read data from the Excel file:

Java
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileInputStream;
import java.io.IOException;

public class ExcelUtils {
    private static XSSFWorkbook workbook;
    private static XSSFSheet sheet;

    public ExcelUtils(String excelPath, String sheetName) {
        try {
            FileInputStream fis = new FileInputStream(excelPath);
            workbook = new XSSFWorkbook(fis);
            sheet = workbook.getSheet(sheetName);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public int getRowCount() {
        return sheet.getPhysicalNumberOfRows();
    }

    public int getColCount() {
        return sheet.getRow(0).getPhysicalNumberOfCells();
    }

    public String getCellData(int rowNum, int colNum) {
        DataFormatter formatter = new DataFormatter();
        return formatter.formatCellValue(sheet.getRow(rowNum).getCell(colNum));
    }
}

Step 4: Implement Test Case Using TestNG

Create a test class LoginTest.java:

Java
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import java.lang.reflect.Method;

public class LoginTest {

    @DataProvider(name = "loginData")
    public Object[][] getData(Method method) {
        String excelPath = "./LoginData.xlsx";
        ExcelUtils excel = new ExcelUtils(excelPath, "Sheet1");

        int rowCount = excel.getRowCount();
        int colCount = excel.getColCount();

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

        for (int i = 1; i < rowCount; i++) {
            for (int j = 0; j < colCount; j++) {
                data[i - 1][j] = excel.getCellData(i, j);
            }
        }
        return data;
    }

    @Test(dataProvider = "loginData")
    public void testLogin(String username, String password) {
        System.out.println("Testing login with: " + username + " / " + password);
        // Add your login test logic here
        // Example: Assert.assertTrue(loginPage.login(username, password));
    }
}


Step 5: Run the Test

Run your LoginTest class. You should see output for each set of data provided:

output-of-data-provider-testng
output of data provider testng

Conclusion

In conclusion, Data-Driven Testing with TestNG simplifies the testing process by separating test logic from test data. This method enhances test coverage, reduces redundancy, and makes your tests more maintainable and scalable. By following the steps outlined, you can efficiently implement data-driven testing in your projects, ensuring thorough and reliable testing of your application with various input data sets. This approach not only improves test quality but also boosts the overall productivity of your testing efforts.

Frequently Questions on Data Driven Testing With TestNG

1. What is Data-Driven Testing?

Data-Driven Testing (DDT) is a testing methodology where test data is driven by external sources, such as Excel or CSV files, allowing the same test case to be executed with multiple sets of data.

2. Why use TestNG for Data-Driven Testing?

TestNG provides powerful annotations and data provider features that make it easy to implement Data-Driven Testing, ensuring better test coverage and simplified test maintenance.

3. How do you read test data from an Excel file in TestNG?

You can use Apache POI library to read data from Excel files and integrate it with TestNG's @DataProvider annotation to supply data to your test methods.

4. What are the benefits of Data-Driven Testing?

Data-Driven Testing enhances test coverage, reduces redundancy, makes tests easier to maintain, and allows for more flexible and reusable test scripts.

5. Can Data-Driven Testing be used for any type of application?

Yes, Data-Driven Testing can be applied to various types of applications, including web, mobile, and desktop applications, to test different input scenarios effectively.


Next Article

Similar Reads