What is ‘DataProvider’ in TestNG?

What does the name ‘DataProvider’ suggest? Yeah! Something that provides data. Test Data is important in Software Testing, say you want to login to an application – it needs a username and password values – it’s nothing but the Test data.

1. Method1 – Data Provider method – annotated with @DataProvider – Name ‘LoginData’ – returns an array of objects.

public class DataProviderClass {
@DataProvider(name = “LoginData”)
public static Object[][] dataProviderMethod() {
return new Object[][] { {“Username1”, “[email protected]” }, {“Username2”, “[email protected]”} };
}
}

Rows: #times test needs to be repeated.
Columns: #parameters in Test method

2. Method2 – Test method ‘Login’ – Executed using data provided by Method1-LoginData.

public class TestClass {
@Test(dataProvider = “LoginData”, dataProviderClass = DataProviderClass.class)
public void Login(String username, String password) {
System.out.println(“Username: ” + username + “ and Password: “ + password);
}
}

Output –
Username: Username1 and Password: [email protected]
Username: Username2 and Password: [email protected]

Write data-driven tests (same test run multiple times with diff. Test data).

Leave a Reply