Data Provider with double Array.

Parameterization using DataProvider in TestNG

This annotation is used to pass the data to method. This annotation method will return an Object[ ][ ], where each Object[ ] can be assigned the parameter list of the test method. The @Test method that wants to receive data from this DataProvider needs to use a dataProvider name equals to the name of this annotation.

@DataProvider Annotation with Double Array-

Java Code-

@DataProvider(name="browserName")
public Object[][] dataProviderMethod()
{
return new Object[][] {{ "Chrome" },{ "Firefox" }};		
}
@Test(dataProvider="browserName")
public void testNGDataProvider(String data)
{
System.out.println(data);
}

Output-

[RemoteTestNG] detected TestNG version 7.4.0
Chrome
Firefox
PASSED: testNGDataProvider("Firefox")
PASSED: testNGDataProvider("Chrome")

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


===============================================
Default suite
Total tests run: 2, Passes: 2, Failures: 0, Skips: 0
===============================================


Related topics-
















@DataProviderSingleArray-

Here we have used single array for passing the parameter. Here we have created size of array and passed the parameter in array and same value passed to @Test method

Java Code-

public class Testing_dataProviderSingleArray {
@DataProvider(name="skill")
public Object[] dataProviderMethod()
{
Object[] data =new Object[5];
data[0]="Selenium";
data[1]="TestNG";
data[2]="dataProvider";
data[3]="Cucumber";
data[4]="Automation";
return data;	
}
@Test(dataProvider="skill")
public void testNGDataProvider(String text)
{
System.out.println(text);
}
}

Output-

[RemoteTestNG] detected TestNG version 7.4.0
Selenium
TestNG
dataProvider
Cucumber
Automation
PASSED: testNGDataProvider("Automation")
PASSED: testNGDataProvider("dataProvider")
PASSED: testNGDataProvider("Cucumber")
PASSED: testNGDataProvider("TestNG")
PASSED: testNGDataProvider("Selenium")

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


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


DataproviderClass Annotation-

In all cases, it’s not necessary that you will keep data provider method in same class.

So here, if you are keeping data provider in other class and want to call in @Test method class then you have to use the attribute as data provider class in @Test Method.

Java Code-

    
public class Testing_dataProviderSingleArray {
@DataProvider(name="skill")
public Object[] dataProviderMethod()
{
Object[] data =new Object[5];
data[0]="Selenium";
data[1]="TestNG";
data[2]="dataProvider";
data[3]="Cucumber";
data[4]="Automation";	
return data;	
}

public class Testing_dataProviderClass {
@Test(dataProvider="skill", dataProviderClass = Testing_dataProviderSingleArray.class)
public void testNGDataProvider(String text)
{
System.out.println(text);
}
}

Outcome-

[RemoteTestNG] detected TestNG version 7.4.0
Selenium
TestNG
dataProvider
Cucumber
Automation
PASSED: testNGDataProvider("Cucumber")
PASSED: testNGDataProvider("Automation")
PASSED: testNGDataProvider("dataProvider")
PASSED: testNGDataProvider("TestNG")
PASSED: testNGDataProvider("Selenium")

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


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

No comments:

Post a Comment