Data 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:\Users\Sagar\.IdeaIC15\system\temp-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
===============================================

You may also like...