@Factory - Can be used to execute all the test methods present inside a test class, using separate instance of the same class. That means running the class in parametrized way by providing different inputs to class constructor.
@DataProvider - A test method that uses DataProvider will be executed a multiple number of times based on the data provided by the DataProvider. The test method will be executed using the same instance of the test class to which the test method belongs.
Below code segment will illustrate usage of @Factory with @DataProvider annotation.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.testng.annotations.BeforeClass; | |
import org.testng.annotations.DataProvider; | |
import org.testng.annotations.Factory; | |
import org.testng.annotations.Test; | |
public class FactoryExample { | |
String userMode; | |
@Factory(dataProvider = "userModeProvider") | |
public FactoryExample(String userMode) { | |
this.userMode = userMode; | |
} | |
@BeforeClass(alwaysRun = true) | |
public void init() throws Exception { | |
System.out.println("Before Running the class userMode is " + userMode); | |
} | |
@Test(groups = "testFactory", description = "test method1") | |
public void testMethod1() throws Exception { | |
System.out.println("Inside method 1 " + userMode ); | |
} | |
@Test(groups = "testFactory", description = "test method2") | |
public void testMethod2() throws Exception { | |
System.out.println("Inside method 2 " + userMode); | |
} | |
@DataProvider | |
private static String [][] userModeProvider() { | |
return new String [][]{ | |
new String []{"Admin"}, | |
new String []{"Tenant"}, | |
new String []{"AdminUser"}, | |
new String []{"TenantUser"}, | |
}; | |
} | |
} |
Execution result of the class as follows. You can see that 8 test methods have been executed with different user mode parameters.
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running TestSuite
Before Running the class userMode is Admin
Inside method 1 Admin
Inside method 2 Admin
Before Running the class userMode is Tenant
Inside method 1 Tenant
Inside method 2 Tenant
Before Running the class userMode is AdminUser
Inside method 1 AdminUser
Inside method 2 AdminUser
Before Running the class userMode is TenantUser
Inside method 1 TenantUser
Inside method 2 TenantUser
Tests run: 8, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.625 sec
Results :
Tests run: 8, Failures: 0, Errors: 0, Skipped: 0
5 comments:
Before Running the class userMode is Tenant
Inside method 1 Tenant
Before Running the class userMode is TenantUser
Inside method 1 TenantUser
Before Running the class userMode is Admin
Inside method 1 Admin
Before Running the class userMode is AdminUser
Inside method 1 AdminUser
Inside method 2 Tenant
Inside method 2 TenantUser
Inside method 2 Admin
Inside method 2 AdminUser
Hi Krishana,
I executed the same program But got Output in below format
Before Running the class userMode is Tenant
Inside method 1 Tenant
Before Running the class userMode is TenantUser
Inside method 1 TenantUser
Before Running the class userMode is Admin
Inside method 1 Admin
Before Running the class userMode is AdminUser
Inside method 1 AdminUser
Inside method 2 Tenant
Inside method 2 TenantUser
Inside method 2 Admin
Inside method 2 AdminUser
Hi Sachin,
How you execute the tests. Is it though your IDE ?
I got the same above results. Executed it in Eclipse IDE. Its not giving the results you mentioned.
@Anil - Can you run the maven pom file and see.. whether it gives the same result.
Post a Comment