@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
===============================================