Sometimes we may need to run a set of tests with different data values. To achieve this we may define a separate set of tests inside a suite in the testng XML and test the required scenario. The problem with this approach is that, if you get an extra set of data, you will need to redefine the test.@Factory
allows tests to be created at runtime depending on certain data-sets or conditions.
Let's take your example
@Factory
public Object[] testMy()
{
return new Object[]{new Login1()};
}
public class login{
public login(){
syso('Login constructor called');
}
output :
Login constructor called
You can also pass arguments and call the constructor multiple times
@Factory
public Object[] testMy()
{
return new Object[]{new Login1(1),new Login1(2)};
}
public class login{
public login(int num){
syso('The number is '+num);
}
output:
The number is 1
The number is 2
Hope this helps you..Kindly get back if you have any queries