TestingDocs.com
Software Testing website
  • Automation
    • Selenium
    • JBehave Framework
  • Tutorials
    • MySQL Tutorials
    • Testlink
    • Maven
    • Git
  • IDEs
    • IntelliJ IDEA
    • Eclipse
  • Flowcharts
    • Flowgorithm
    • Raptor
  • About

TestNG

@DataProvider Annotation TestNG Example

Overview

In earlier post we have seen a basic example of TestNG DataProvider. ( http://www.testingdocs.com/testng-dataprovider-example/ )

In the above example both the DataProvider and @Test method reside in the same test class. Now we will see how to use data provider specified in another test class.

We can specify the data provider class using the attribute “dataProviderClass” . We will see a code example by tweaking the code mentioned in earlier post. We will move the data provider to another class .

DataProvider class

package com.testingdocs.testng.sample;

import org.testng.annotations.DataProvider;

public class DataProviderClass 
{
 @DataProvider(name="org")
 
 public static Object[][] employeeData(){ 
 Object[][] object = new Object[2][3]; 
 
 object[0][0]="Manager"; 
 object[0][1]="Regan"; 
 object[0][2]=12000; 
 
 object[1][0]="Employee"; 
 object[1][1]="John"; 
 object[1][2]=8000; 
 
 
 return object; 
 } 

}

 

Test method in another class which uses the above data provider.

package com.testingdocs.testng.sample;

import org.testng.annotations.Test;

public class DataProviderExample 
{
 
 @Test(dataProviderClass=DataProviderClass.class,dataProvider="org") 
 public void runforEachEmployee(String role, String empname, int salary)
 { 
 System.out.println("******Emp Record***********");
 System.out.println(empname); 
 System.out.println(role); 
 System.out.println(salary); 
 } 

}

 

We can see in the above example how we specified the data provider in another class.( dataProviderClass=DataProviderClass.class )

The test method run twice as shown in the below run result:

PASSED: runforEachEmployee(“Manager”, “Regan”, 12000)
PASSED: runforEachEmployee(“Employee”, “John”, 8000)

===============================================
Default test
Tests run: 2, Failures: 0, Skips: 0
===============================================

Note that ONLY  the test method which uses the data provider runs twice as . Its a common misconception that all the methods run multiple times like the test method which uses the DataProvider.We will now place a configuration method like @BeforeClass and check the behavior.

package com.testingdocs.testng.sample;

import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class DataProviderExample 
{
 @BeforeClass 
 public void beforeClass(){ 
 System.out.println("This runs only once unlike the @Test method which uses DataProvider"); 
 } 
 
 @Test(dataProviderClass=DataProviderClass.class,dataProvider="org") 
 public void runforEachEmployee(String role, String empname, int salary)
 { 
 System.out.println("******Test Method Run ******");
 System.out.println(empname); 
 System.out.println(role); 
 System.out.println(salary); 
 } 

}

 

@BeforeClass would run only once as shown in the below run result.

Run Result

This runs only once unlike the @Test method which uses DataProvider
******Test Method Run ******
Regan
Manager
12000
******Test Method Run ******
John
Employee
8000

PASSED: runforEachEmployee(“Manager”, “Regan”, 12000)
PASSED: runforEachEmployee(“Employee”, “John”, 8000)

===============================================
Default test
Tests run: 2, Failures: 0, Skips: 0
===============================================

Related Posts

Tests TestNG Suite testng.xml

TestNG /

Run tests from TestNG Suite testng.xml file

Add TestNG library

TestNG /

Add the TestNG library to the project

new TestNG class

TestNG /

Create TestNG test class in Eclipse IDE.

TestNG Plugin IntelliJ

TestNG /

Enable TestNG in IntelliJ IDE

Installing Eclipse from Update Site

TestNG /

Install TestNG latest version from the update site

‹ TestNG Framework Questions› Install TestNG plugin in Eclipse

Recent Posts

  • MS Access Data Types
  • Install RAPTOR Avalonia on CentOS
  • Download RAPTOR Avalonia Edition on Windows
  • npm doctor command
  • Build & Run CLion Project
  • Create New CLion C Project on Windows
  • Configure CLion Toolchains on Windows
  • Launch CLion IDE on Windows
  • Activate CLion IDE
  • CLion IDE for C/C++ Development

Back to Top

Links

  • Contact
  • Privacy Policy
  • Cookie Policy

www.TestingDocs.com

Go to mobile version