Data provider with parallel attribute.

Data provider

How to use Data provider with parallel attribute in testNG?

@Data provider has 2 attributes, 1. Parallel and 2. Name.

Parallel-

This attribute can have true or false value. If this value is set as true, tests executed using this data provider will run in parallel. Default value is false.

<suite name="Suite1" data-provider-thread-count="2">

@DataProvider(name="DataContainer", parallel=true)

Name –

This attribute is for giving the name to dataprovider method. If it's not supplied, then the name of this data provider will automatically be set to the name of the method.

In the below example, "parallel" attribute is set as true and XML file 'data-provider-thread-count' is mentioned as 2.

Java Code-

public class Testing_dataProviderParallel {
	
	@DataProvider(parallel = true, name = "testNG")
	public Object[] dataProvider()
	
	{
		Object[] data =new Object[5];
		data[0]="Selenium";
		data[1]="TestNG";
		data[2]="dataProvider";
		data[3]="Cucumber";
		data[4]="Automation";
		return data;
		
	}
	@Test(dataProvider="testNG")
	public void testNGDataProvider(String text)
	{
		System.out.println(text+"-"+"Threa Number is :-"+Thread.currentThread().getId());
	}
	
}

XML code-

    
<?xml version="1.0" encoding="UTF-8"?>
<suite name="dataProvider" data-provider-thread-count="2">
<test name= "testNG">
<classes>

<class name="org.Testngpro.Testing_dataProviderParallel"></class>
</classes>
</test>

</suite>
    
    

In below outcome, you can see two thread count as 15 and 16. Above data provider method executed on 2 threads.

Outcome-

[RemoteTestNG] detected TestNG version 7.4.0
[TestNGContentHandler] [WARN] It is strongly recommended to add "" at the top of the suite file [F:\eclipse\Practice_2022\TestNGMethod\dataProvider.xml] otherwise TestNG may fail or not work as expected.
TestNG-Threa Number is :-16
Selenium-Threa Number is :-15
Cucumber-Threa Number is :-15
dataProvider-Threa Number is :-16
Automation-Threa Number is :-16

===============================================
dataProvider
Total tests run: 5, Passes: 5, Failures: 0, Skips: 0
===============================================

Related topics-

No comments:

Post a Comment