Spread the love

Learn How to Work With DataProvider in TestNG Effectively

Spread the love

DataProvider in TestNG

data providers are useful when implementing data-driven tests. Data providers separate test methods and test data. This is applicable while we are doing complex framework and the data is coming from other source – objects, objects read from a property file, database, file system etc. There can be the parameter of the parameter.

This can be achieved in two different ways

  • Iterate over the input datasets(from a single test method.)
  • For each test method, create a test data set.

How to use DataProvider annotation in TestNG?

A data provider method is having annotations as @DataProvide. Data Provider as one String attribute- the name. In case, we have not provided Data Provider a name, the TestNG will pick up the default value as the method’s name as the Data Provide’s name.

The Data Provide method must return an array of objects. DataProvider is an important concept to remember in Parameterization in TestNG.

TestNG Data Provider returns one of the following types:

  1. An array of array objects(Object[][]). The first dimension is the size that represents the number of times the test methods will be invoked. The second dimension talks about the array of objects that will be consumed by the test method.
  2. An iterator of objects like Iterator<Object[]>. This allows us to create our own test data. Once invoked this will supply the test data in a sequential manner.
public class DataProvider{
@Test(dataProvider="dp1")
  public void myTest(int n,String s){
   system.out.println("int-"+n+"String-"+s);
}
@BeforeMethod
  public void beforeMethod(){}
     @DataProvider(name="dp1")
      public Object][] dp(){
       return new object[][]{
        new object[]{1,"one"},
        new object[]{2,"two"},
};
}
@DataProvider(name="dp2")
    public Object][] dp(){
      return new object[][]{
        new object[]{"Name",new String},
        new object[]{RollNo,new Integer(20)},
};
}
}

A data provider can return:

  • An iterator of parameter types.

public iterator<Object[]> crateTestData(){
return new MyIterator(...);
}
  • An argument of the type
public object[][] crateTestData(method m){
System.out.println("The method name"+m.getName());
return new Object[][]{new Object[]{"Name"}};
}

Let’s create an actual class to test. This class accepts an Integer value and determines if the value is odd or even. The actual class will be as follows:

package com.test;
import org.testng.annotations.Test;
public class OddEvenCalculator {
  public boolean validate(final Integer oddEven)
  {
	  if(oddEven/2==0) {
		  return true;
	  }
	return false;
	  
  }
}

The test class will look like:


package com.test;

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

public class TestOddEvenCalculator {
    private OddEvenCalculator oddEven;
    @BeforeMethod
    public void setup() {
    	oddEven=new OddEvenCalculator();
    }
    @DataProvider(name="dp")
    public static Object[] oddEvenNumbers() {
    	return new Object [ ][ ] { { 2,true}, {5,false}, {9, false}, {10, true}};
    }
    @Test(dataProvider="dp")
    public void testOddEvenChecker(Integer input, boolean flag) {
    System.out.println("input "+input +" flag "+ flag);
    Assert.assertEquals(flag, oddEven.validate(input));
    }
}

the testng.xml looks like below:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="TestOddEvenCalculator" verbose="1">
  <test name="TestOddEvenCalculator">
     <classes>
      <class name="com.test.TestOddEvenCalculator">
       </class>
    </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->
DataProviders use in TestNG
DataProviders use in TestNG

The console output is as follows:

READ  Learn How To Install TestNG for IDEs Quickly
READ  Learn How to Work on Groups in TestNG Effectively

[RemoteTestNG] detected TestNG version 7.0.0
input 2 flag true
input 5 flag false
input 9 flag false
input 10 flag true

===============================================
TestOddEvenCalculator
Total tests run: 4, Passes: 2, Failures: 2, Skips: 0
========




Spread the love
Animesh Chatterjeehttps://techtravelhub.com/
I am the founder and owner of the blog - TechTravelHub.com, always love to share knowledge on test automation,tools,techniques and tips.I am a passionate coder of Java and VBScript.I also publish articles on Travel ideas and great honeymoon destinations.Apart from these, I am a gear-head,love to drive across India. I have shared lots of articles here on How to travel several parts of India.Customization of cars aka car modification is my another hobby.Get in touch with me on ani01104@gamil.com

Related Articles

Learn How to Work on Groups in TestNG Effectively

What are the groups in TestNG? Groups concept in TestNG is a...

Comments

  1. […] DataProvider in TestNG data providers are useful when implementing data-driven tests. Data providers separate test methods and test data. This is applicable while we are doing complex framework and the data is coming from other source – objects, objects read from a property file, database, file system etc. There can be the parameter of the […] Source: https://www.techtravelhub.com/dataprovider-in-testng/ […]

  2. […] fromhttps://www.techtravelhub.com/dataprovider-in-testng/ […]

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Recent Posts

Learn Quickly About DevOps Tools in a Fast and Efficient Manner

Introduction to DevOps tools DevOps tools are a collection of...

How to Collaborate between Development and Operations Team Fast and Effectively

Collaborate between Development and Operations Teams are the key...

Learn How to Use Jacoco Java code Coverage

Introduction to Jacoco Jacoco provides a wide range of coverage(analysis...

EDITOR PICKS


Spread the love