Example of TestNG DataProvider

0

A testNG test method is like any other java method and you can even pass parameters to it. In this article, I will show you how one can write data-driven test using DataProvider. A data-driven test will run once for each set of data passed-in and the data will be provided by the method annotated with @DataProvider.

1. Data-Driven test using DataProvider

Let’s start with a simple example. In the below test case singleString, it accepts a String parameter and the data is injected using a @DataProvider method dp(). Note that a DataProvider method has to be annotated with @DataProvider. You can even provide a name to it else by default one can refer to it by its method name. If a test is referring to a DataProvider for its parameter values then it has to specify the dataProvider attribute in its Test annotation.

TestNGDataProviderExample:

package com.javarticles.testng;

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class TestNGDataProviderExample {
    @Test(dataProvider="dp")
    public void singleString(String a) {
        System.out.println(a);
    }
    
    @DataProvider
    public Object[][] dp() {
        return new Object[][]{{"TestNG"}, {"DataProvider"}};
    }
}

Output:

TestNG
DataProvider
PASSED: singleString("TestNG")
PASSED: singleString("DataProvider")

2. Passing Multiple Parameters with DataProvider

A DataProvider method must always return a two dimensional array. The first array represents data set thus takes care of the number of time the tests has to run. The second array represents number of parameters.
In the below example, I have introduced one more DataProvider, called dpMultiParam, it returns multiple sets of values. Each set contains different java types like String, int, long and a boolean.
Here I have also specified a name in @DataProvider‘s name attribute.

TestNGDataProviderExample:

package com.javarticles.testng;

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class TestNGDataProviderExample {
    @Test(dataProvider="dp")
    public void singleString(String a) {
        System.out.println(a);
    }
    
    @DataProvider
    public Object[][] dp() {
        return new Object[][]{{"TestNG"}, {"DataProvider"}};
    }
        
    @Test(dataProvider="multi-param")
    public void multiParameter(String a, int i, long l, boolean b) {
        System.out.println("String: " + a + ", int: " + i + ", long: " + l + ", boolean: " + b);
    }
    
    @DataProvider(name="multi-param")
    public Object[][] dpMultiParam() {
        return new Object[][]{
                {"a", 1, 10L, true},
                {"b", 2, 20L, false},
                {"c", 3, 30L, true},
                {"d", 4, 40L, false}};
    }
}

Output:

String: a, int: 1, long: 10, boolean: true
String: b, int: 2, long: 20, boolean: false
String: c, int: 3, long: 30, boolean: true
String: d, int: 4, long: 40, boolean: false
PASSED: multiParameter("a", 1, 10, true)
PASSED: multiParameter("b", 2, 20, false)
PASSED: multiParameter("c", 3, 30, true)
PASSED: multiParameter("d", 4, 40, false)

3. Inherited DataProvider

A DataProvider can belong to a super class and can be reused by several test methods either defined in the same class or one of its sub-classes. A test method from the super class can also refer to a DataProvider defined in one of its sub-classes.

BaseTestNG:

package com.javarticles.testng;

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class BaseTestNG {
    @DataProvider
    public Object[][] inheritedDp() {
        return new Object[][]{{"one"}, {"two"}};
    }
    
    @Test(dataProvider="dp")
    public void superSingleString(String a) {
        System.out.println(a);
    }
}

TestNGDataProviderExample:

package com.javarticles.testng;

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class TestNGDataProviderExample 
extends BaseTestNG {
    @Test(dataProvider="dp")
    public void singleString(String a) {
        System.out.println(a);
    }
    
    @DataProvider
    public Object[][] dp() {
        return new Object[][]{{"TestNG"}, {"DataProvider"}};
    }
        
    @Test(dataProvider="multi-param")
    public void multiParameter(String a, int i, long l, boolean b) {
        System.out.println("String: " + a + ", int: " + i + ", long: " + l + ", boolean: " + b);
    }
    
    @DataProvider(name="multi-param")
    public Object[][] dpMultiParam() {
        return new Object[][]{
                {"a", 1, 10L, true},
                {"b", 2, 20L, false},
                {"c", 3, 30L, true},
                {"d", 4, 40L, false}};
    }
    
    @Test(dataProvider="inheritedDp")
    public void numericalInWords(String numerical) {
        System.out.println(numerical);
    }
}

Output:

one
two
PASSED: numericalInWords("one")
PASSED: numericalInWords("two")

TestNG
DataProvider
PASSED: singleString("TestNG")
PASSED: singleString("DataProvider")

4. DataPoviders with parameters

Data Providers can accept two types of parameters, Method and ITestContext. In this example, I am going to show you a DataProvider that receives the Method. TestNG will pass the test method that is about to be invoked. This is useful if the DataProvider has to provide different data set based on the test method being called.

In the below example, we have test methods client1, client2 and commonClient. DataProvider dpWithMethod, receives the test method in form of Method. It returns different data sets based on the method name.

TestNGDataProviderExample:

package com.javarticles.testng;

import java.lang.reflect.Method;

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class TestNGDataProviderExample 
extends BaseTestNG {
    @Test(dataProvider="dp")
    public void singleString(String a) {
        System.out.println(a);
    }
    
    @DataProvider
    public Object[][] dp() {
        return new Object[][]{{"TestNG"}, {"DataProvider"}};
    }
        
    @Test(dataProvider="multi-param")
    public void multiParameter(String a, int i, long l, boolean b) {
        System.out.println("String: " + a + ", int: " + i + ", long: " + l + ", boolean: " + b);
    }
    
    @DataProvider(name="multi-param")
    public Object[][] dpMultiParam() {
        return new Object[][]{
                {"a", 1, 10L, true},
                {"b", 2, 20L, false},
                {"c", 3, 30L, true},
                {"d", 4, 40L, false}};
    }
    
    @Test(dataProvider="inheritedDp")
    public void numericalInWords(String numerical) {
        System.out.println(numerical);
    }
    
    @Test(dataProvider="dpWithMethod")
    public void client1(String p) {
        System.out.println(p);
    }
    
    @Test(dataProvider="dpWithMethod")
    public void client2(String p) {
        System.out.println(p);
    }
    
    @Test(dataProvider="dpWithMethod")
    public void commonClient(String p) {
        System.out.println(p);
    }
    
    @DataProvider
    public Object[][] dpWithMethod(Method m) {
        switch (m.getName()) {
        case "client1":
            return new Object[][]{{"one"}, {"two"}};
        case "client2":
            return new Object[][]{{"1"}, {"2"}};
        default:
            return new Object[][]{{"i"}, {"ii"}};
        }
    }
}

Output:

one
two
PASSED: client1("one")
PASSED: client1("two")

1
2
PASSED: client2("1")
PASSED: client2("2")

i
ii
PASSED: commonClient("i")
PASSED: commonClient("ii")

5. Download Eclipse Project

This was an example about TestNG DataProvider. You can download the source code here: testNgDataProviderExample.zip

Share.

Leave A Reply