Data-Driven Testing with TestNG
Azher Iqbal
Sr. SQAE | Automation Specialist | Java | .NET | Selenium WebDriver | Cypress | BDD | PlayWritght | API Testing | Azure DevOps | Appium
Data-driven testing is a powerful methodology used to run the same set of tests with multiple sets of data. In the world of automated testing, this approach maximizes coverage and efficiency, especially when leveraged with TestNG, a versatile testing framework for Java.
TestNG stands out for its ability to handle complex test structures and parameterize tests easily. Coupled with Selenium for browser automation, it becomes a key asset in ensuring software quality.
Step 1: Set Up Your Environment
Ensure you have Java, TestNG, Selenium WebDriver, and Eclipse IDE or another IDE of your choice installed and configured. Add the necessary dependencies in your pom.xml if you're using Maven:
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>Your.Selenium.Version</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>Your.TestNG.Version</version>
<scope>test</scope>
</dependency>
</dependencies>
Step 2: Prepare Test Data
Typically, data is stored externally in CSV files, Excel sheets, or databases. For simplicity, let's use an Excel sheet with columns for username and password.
Step 3: Write the Test Code
Utilize TestNG's @DataProvider annotation to feed data into your tests. Here's a basic test that navigates to a login page and inputs credentials:
领英推荐
TestNG DataProvider:
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class TestDataDrivenExample {
@DataProvider(name = "loginData")
public Object[][] getLoginData() {
return new Object[][] {
{"username1", "password1"},
{"username2", "password2"},
// Add more test data as needed
};
}
@Test(dataProvider = "loginData")
public void testLogin(String username, String password) {
// Selenium code to perform login with username and password
}
}
External Data Sources:
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.stream.Stream;
public class ExternalDataProviderExample {
@DataProvider(name = "loginData")
public Object[][] getLoginData() throws IOException {
// Read data from a CSV file
try (Stream<String> stream = Files.lines(Paths.get("login_data.csv"))) {
return stream.map(line -> line.split(",")).toArray(String[][]::new);
}
}
@Test(dataProvider = "loginData")
public void testLogin(String username, String password) {
// Selenium code to perform login with username and password
}
}
Parameterized Tests:
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class ParameterizedTestNGExample {
@Test
@Parameters({"username", "password"})
public void testLogin(String username, String password) {
// Selenium code to perform login with username and password
}
}
Step 4: Run the Test
You can execute the test through the IDE or using Maven/Gradle commands:
mvn test
Data-driven testing with TestNG, Java, and Selenium is a robust way to enhance your testing capabilities. This article provided a snapshot into making this methodology work for you, but the depth of TestNG allows for much more, including parallel execution, group testing, and more sophisticated data providers.