0% found this document useful (0 votes)
13 views3 pages

TestNG DataProviders

DataProvider is a container that allows tests to be run with different data sets, enabling parameterization and avoiding hardcoded values. It is essential to define a name for the @DataProvider to prevent runtime errors, and various return types such as Object[], Object[][], and Iterators can be used for data provision. The document also explains how to define a DataProvider method and use it in test methods with appropriate parameters.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views3 pages

TestNG DataProviders

DataProvider is a container that allows tests to be run with different data sets, enabling parameterization and avoiding hardcoded values. It is essential to define a name for the @DataProvider to prevent runtime errors, and various return types such as Object[], Object[][], and Iterators can be used for data provision. The document also explains how to define a DataProvider method and use it in test methods with appropriate parameters.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

1.

What is DataProvider:
DataProvider is a container which passes a data to our test method, so that same
test can be run with different data

2. Why we need to use dataproviders


mainly for parameterizing our test and when we dont want to hardcode data in our
test method.

3. What happens if we dont define name to @DataProvider?


it will give run time Error as our @Test expects a dataprovider name

if dont define name in @DataProvider annotation to method which generates a data


....better to pass name of method which generate data to dataProvider in @Test
Annotation

4. How to use Data providers

### define data provider method with name

@DataProvider(name="loginTestData")
public Object[][] loginData(){
Object[][] data = Object[2][2]
data = { {"Admin","admin123"},
{"Admin", "test123"},
}
return data
}
### use data provider method name in @Test Annotation, create appropriate no. of
variables in method signature
@Test(dataProvider="loginTestData")
public void login(String name, String password){
Reporter.log("login name = " + name + " login password " + password +"\n")
}

we want to run same test with multiple sets of data.


Parameters of Test method and return type of data provider

1)is it mandatory to return Object[][] from the dataprovider


no ...we can return other datatype as well
But
As Object is superclass/parent of all object in java, Object is used as return
type
Object represent all datatype(Pre defined / user defined datatypes)
Pre defined datatype : String, Interger, Float
user defined datatype : User created class

when we are returning single type of data (e.g. String, Employee) ...then instead
of Object we can use that datatype as return
But, For mixed datatype of data better to use Object ( as Object is superclass)

2)what are the return types of a dataprovider


we can return following:

- Object[] (single dimentional array)


- Object[][] (multi dimentional array)
- Iterator<Object>
- Iterator<Object[]>

- Object[] (single dimentional array)


@DataProvider()
public Object[] dp2(){
Object[] data= new Object[]{
"Yadgir",
"Ramesh",
};
return data;

@Test(dataProvider="dp2")
public void arrayconsumer(Object a){
Reporter.log(a);
}

- Object[][] (multi dimentional array)

@DataProvider()
public Object[][] dp1(){
Object[][] data= {
new Object[] {"Yadgir", "Reddy"},
new Object[] {"Ramesh", "Edureka"},
};
return data;
}

@Test(dataProvider="dp1")
public void arrayofarray(String a, String b){ <=== as we know Object
array contains two Strings so we used String parameter ( we can use Object here as
well)
Reporter.log(a + " " + b);
}

- Iterator<Object>
when you use collections to store a data then we have to return it using
iterator

@DataProvider()
public Iterator<Object> dp3(){
List<Object> data = new ArrayList<Object>();
data.add("Kausar");
data.add(40);
data.add("Satara");
data.add("Ibrahim");
data.add(41);
data.add("Kolhapur");
return data.iterator;
}

@Test(dataProvider=dp3)
public Object consumer(Object a){
system.out.println(a)
}
- Iterator<Object[]>
when you use collections to store a array of data then we have to return it
using iterator

@DataProvider()
public Iterator<Object[]> dp7(){
List<Object[]> data = new ArrayList<Object[]>();
data.add(new Object[]{"Ibrahimpasha",41});
data.add(new Object[] {"Kausar", 40});
return data.iterator();
}

@Test(dataProvider="dp7")
public void iteratorarrayConsumer(Object[] a) {
for (int x=0;x<a.length;x++) {
System.out.println(a[x]);
}

3) How do we specify the parameters in the method signature or the test method
based on data

You might also like