Data Driven Testing with testNG

Data Driven Testing is a software testing method in which test data is stored in table or spreadsheet format. Data driven testing allows testers to input a single test script that can execute tests for all test data from a table and expect the test output in the same table. It is also called table-driven testing or parameterized testing.
Let’s take the simple calculator used to addition function. We want to check it with boundary values. We should check with both positive and negative integers.
First we can set the logic as follows.

We can write our test script as follows.

The result would be as follows.

Here our expected result is received. But in a programming point of view this is not a best practice. Only data set is changing. In maintainability aspect this is a burden. If we have a single test method and passing multiple parameters this would be much easier.
We can create a data provider which is two dimensional and pass object array.

Now we can bind this data set with a test method. @DataProvider annotation is used there.
public class CalcLogicTest {
CalcLogic calc;
@BeforeMethod
public void setUp() {
calc = new CalcLogic();
}
@Test(datrovider =”getNum”)
public void testSum(int input1, int input2, int expectedResult) {
Assert.assertEquals(calc.sum(input1, input2), expectedResult, “Failed to add two ints :”); }
}
@DataProvider
public Object[][] getNum(){
return new Object[][]{
{100,50,150},
{-100,-50,-150},
{100,-50,50},
{-100,50,-50}
};
}
}
Note that if a single data set becomes fail (assertion fail), program will not terminate from that particular point. Every row will consider as a new test case.
This is about data driven testing and see you in another article. Happy Learning!!!
Hirudini Udugama