TestNG Tutorials 59: DataProvider in TestNG – Running DataProvider Method in Parallel – Parallel DataProvider Method

A Test method is run for all data set provided by a DataProvider method which is by default one after another. Note here that test method is run on data in same sequence in which DataProvider pass it. The reason behind this is that a DataProvider annotated method has an attribute named “parallel” whose default value is set to “false”.

We can run it parallel as well by setting attribute “parallel” value as true. It will save a lot of time.

Example:

package DataProvider;

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
 
public class DataProviderWithIndices {
   
        
        
        // You must need to mention data provider method name in Test method
        @Test(dataProvider="DataContainer")
    public void studentRegistration(String First_name, String Last_Name, String Email_ID) {
        System.out.println("Registered student with details: "+First_name+" "+Last_Name+" "+Email_ID);    
      
    }
     
        
        /*
         * Since we have set parallel as true, test method will be run in parallel for set of data provided by data provider. 
         */
    @DataProvider(name="DataContainer", parallel=true)
    public Object[] myDataProvider() {
        
        
        Object data[][]=  new Object[5][3];
        // First student details
        data[0][0]= "Mukesh"; 
        data[0][1]= "Otwani";
        data[0][2]= "Motwani@gmail.com";
        
        // Second student details
        data[1][0]= "Amod";
        data[1][1]= "Mahajan";
        data[1][2]= "amahajan@hotmail.com";
        
        // Third student details
        data[2][0]= "Animesh";
        data[2][1]= "Prashant";
        data[2][2]= "aprashant@gmail.com";
        
        // Fourth student details
        data[3][0]= "Ankur";
        data[3][1]= "Singh";
        data[3][2]= "asingh@gmail.com";
        
        // Fifth student details
        data[4][0]= "Amritansh";
        data[4][1]= "Kumar";
        data[4][2]= "akumar@gmail.com";
        
        return data;
        
        
    }
}

Output:

[java] [RemoteTestNG] detected TestNG version 6.14.2 Registered student with details: Mukesh Otwani Motwani@gmail.com Registered student with details: Amod Mahajan amahajan@hotmail.com Registered student with details: Amritansh Kumar akumar@gmail.com Registered student with details: Ankur Singh asingh@gmail.com Registered student with details: Animesh Prashant aprashant@gmail.com PASSED: studentRegistration(“Mukesh”, “Otwani”, “Motwani@gmail.com”) PASSED: studentRegistration(“Ankur”, “Singh”, “asingh@gmail.com”) PASSED: studentRegistration(“Amod”, “Mahajan”, “amahajan@hotmail.com”) PASSED: studentRegistration(“Amritansh”, “Kumar”, “akumar@gmail.com”)

PASSED: studentRegistration(“Animesh”, “Prashant”, “aprashant@gmail.com”)