TestNG
- What is dataprovider and why we use them?
Ans : a. A DataProvider is an annotation in TestNG (@DataProvider) that supplies test data to a test method.
b. It returns data in the form of a two-dimensional object array (Object[][]).
c.Each row in the array represents one set of test data, and TestNG executes the test method once per row.
@DataProvider(name = "loginData")
public Object[][] getData() {
return new Object[][] {
{"user1", "password1"},
{"user2", "password2"},
{"user3", "password3"}
};
}
@Test(dataProvider = "loginData")
public void loginTest(String username, String password) {
System.out.println("Testing login with: " + username + " / " + password);
}
2. What is parameter and Data Provider.
Ans :
Parameter :
a. Declared in testng.xml using <parameter> tags.
b. Values are injected into test methods using @Parameters.
c. Static: Each parameter runs the test only once per execution.
d. Best for environment values (e.g., base.url, browser, credentials).
<suite name="AutomationSuite">
<test name="LoginTest">
<parameter name="username" value="admin"/>
<parameter name="password" value="admin123"/>
<classes>
<class name="com.abhi.tests.LoginTest"/>
</classes>
</test>
</suite>
@Parameters({"username", "password"})
@Test
public void loginTest(String user, String pass) {
System.out.println("Login with: " + user + " / " + pass);
}
DataProviders
a. Declared in code using @DataProvider.
b. Returns a 2D Object array (Object[][]).
c. Dynamic: Runs the test multiple times with different data sets.
d. Best for testing multiple inputs (e.g., multiple login credentials, product variations).
@DataProvider(name = "loginData")
public Object[][] getData() {
return new Object[][] {
{"user1", "pass1"},
{"user2", "pass2"},
{"user3", "pass3"}
};
}
@Test(dataProvider = "loginData")
public void loginTest(String user, String pass) {
System.out.println("Login with: " + user + " / " + pass);
}