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 TestNGdata providers in testng
We can execute same test using different parameter values using Data Providers in TestNG. In below example, we have a method called as multiplyTest that uses parameter values sent by Data provider to execute the test. Since there are 3 sets of data, same test method is executed 3 times but with different data.
package org.softpost;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class DataProviders {
@DataProvider(name = "dataSets")
public Object[][] provideData() {
return new Object[][] {
{ 11, 20, 220 },
{ 22, 30, 660 },
{ 15, 15, 225 }
};
}
@Test(dataProvider = "dataSets")
public void multiplyTest(int no1, int no2, int multiplicationResult) {
System.out.println("Testing with parameters - " + no1 + " and " + no2 + " and " + multiplicationResult);
Assert.assertEquals(no1*no2, multiplicationResult);
}
}
Here is the output of above code.
[TestNG] Running:
C:UsersSagar.IdeaIC15system emp-testng-customsuite.xml
Current parameters – 11 and 20 and 220
Current parameters – 22 and 30 and 660
Current parameters – 15 and 15 and 225
===============================================
Default Suite
Total tests run: 3, Failures: 0, Skips: 0
===============================================
Web development and Automation testing
solutions delivered!!