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")

===============================================
Default test
Tests run: 5, Failures: 0, Skips: 0
===============================================

===============================================
Default suite
Total tests run: 5, Failures: 0, Skips: 0
===============================================

[/java]

You can see console output is not in sequence of data provided by DataProvider method. It means it was run parallely.

Parallel data providers running from an XML file share the same pool of threads, which has a size of 10 by default. You can modify this value in the tag of your XML file:

[xml]
<suite name="Suite1" data-provider-thread-count="20" >
[/xml]

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.
#ThanksForReading
#HappySelenium

3 thoughts on “TestNG Tutorials 59: DataProvider in TestNG – Running DataProvider Method in Parallel – Parallel DataProvider Method

  1. My tests are executing in only single browser session while running it with parallel=true in dataprovider. i m running it with 2 set of data it is opening two browser window but executing both tests in single browser only. Please suggest the way so that both tests run in separate browsers.

  2. So if I have 10 set of data for a test method
    Should i set the data-provider-thread-count=”10″
    Is there any relation between data-provider-threadcount and number of data returned by data provider method???

    1. Hi,
      It’s not like that to run 10 set of data parallely, you need to set thread-count as 10. thread-count allows you to specify how many threads should be allocated for this execution. Parallel data providers running from an XML file share the same pool of threads, which has a size of 10 by default. thread-count make more sense when we run in parallel.

Leave a Reply to Austin Cancel reply

Your email address will not be published. Required fields are marked *