TestNG Tutorial
Introduction to TestNG Configuration and set up of TestNG project Simple TestNG project in IntelliJ IDEA TestNG annotations Assertions in TestNG Difference between assertEquals and assertSame Ignoring the tests in TestNG Grouping the tests timeOut tests in TestNG Exception tests in TestNG dependsOnMethods dependsOnGroups Hard and Soft dependencies Passing parameters using XML file Passing parameters using Data providers Dynamic Data Providers group-by-instances attribute in TestNG XML suite file Test priorities in TestNG invocationCount in TestNG Executing Selenium tests using TestNG Executing TestNG tests using main method Executing JUnit tests from within TestNG Running tests in Parallel in TestNG Executing only failed tests in TestNG TestNG integration with Maven TestNG integration with Gradle Reporter class in TestNG TestNG Reports Execution Listener Test Listener Comparison between JUnit and TestNGDynamic data provider in testng
Here is the example that explains how we can pass the data based upon method type. In below example, 2 different test methods use same data provider. At run time, Data provider returns the data based upon method name.
package org.softpost;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import java.lang.reflect.Method;
public class DataProviders1 {
@Test(dataProvider = "dataset")
public void verifyNumbersLength(int number, int expected) {
Assert.assertEquals(String.valueOf(number).length(),expected);
}
@Test(dataProvider = "dataset")
public void verifyStringLength(String sample, int expected) {
Assert.assertEquals(sample.length(), expected);
}
@DataProvider(name = "dataset")
public Object[][] provideData(Method method) {
Object[][] result = null;
if (method.getName().equalsIgnoreCase("verifyNumbersLength")) {
result = new Object[][] {
{ 120, 3 },
{ 11, 2 }
};
} else if (method.getName().equalsIgnoreCase("verifyStringLength")) {
result = new Object[][] {
{ "softpost", 8 },
{ "watson", 6 }
};
}
return result;
}
}
Web development and Automation testing
solutions delivered!!