Posted in Automation Testing

TestNG Data provider using Read CSV file

While working with TestNG , we deal with multiple set of test data to test various scenarios.

It is difficult to provide test data set using List , Map as these needs maintenance and efforts.

Most popular way of providing test data is using Excel.
Check this article on how to use Excel in Data provider.
Now we will discuss another way using CSV file.

Why CSV ?

CSV is better than Excel in many cases, specially when it comes to test data or any data that we store to use within test framework.
Below are few reasons where CSV is better than Excel:

  1. CSV is lightweight and faster than excel with respect to read / write operation
  2. CSV takes up less space than Excel
  3. CSV is compatible with versioning systems like Git as Excel is not suitable for Git to track changes
  4. CSV can be used by the test automation script while it is open , Excel sheet needs to be closed before running test script.

So now lets look at the code on how to read csv file line by line and provide it to data provider.

Read CSV file and return 2D Object array:

public static Object[][] readCsv(File file) throws JsonProcessingException {
        Object obj[][]=null;
        try {

            // Create an object of filereader
            // class with CSV file as a parameter.
            FileReader filereader = new FileReader(file);
            //Skipping Header
            CSVReader csvReader =
                    new CSVReaderBuilder(filereader).withSkipLines(1).build();

            List<String[]> allData = csvReader.readAll();

            int rowCount = allData.size();
            System.out.println("Row count= " + rowCount);

            String[] headers = allData.get(0);

            int colCount = headers.length;
            System.out.println("Col count=" + colCount);

            obj = new Object[rowCount][colCount];

            for (int i = 0, j = 0; i < rowCount; i++) {
                while (j < rowCount) {
                    String[] rowData = allData.get(i);
                    for (String cell : rowData) {
                        System.out.print("Row: " + i + "Cell= " + j);
                        obj[i][j] = cell;
                        System.out.println(" | " + obj[i][j]);
                        j++;
                    }
                }
                j = 0;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        //To print 2D array
	System.out.println("CSV Data : "+Arrays.deepToString(obj));
        return obj;
    }

Now you can use this Object to create data provider:

        @DataProvider(name = "athleteData")
	public Object[][] getathleteData() throws IOException, InvalidFormatException {
		File filePath = new File("./src/test/resources/ExcelData/TestData.csv");
		Object obj[][] = UtilsReadFile.readCsv(filePath);

		return obj;
	}

To use data provider in Test:

@Test(dataProvider = "athleteData",dataProviderClass = DataProvider.class){
public void testCsvDataProvider(String email, String password){
      System.out.println(email + password)
     // Use this data in Test
   }
}

Leave a comment