How to call the test plans dynamically using testng?
We can call the test plans dynamically using @Factory Annotation in testng.
Factories - Factories allow you to create tests dynamically
@Factory - Marks a method as a factory that returns objects that will be used by TestNG as Test classes. The method must return Object[].
Please find the below steps
------------------------------------------------------------------------------------------------------
1. TestPlan1.java
package com.test.sample;
import org.testng.annotations.Test;
public class TestPlan1{
@Test
public void testPlan1Method1()throws Exception {
System.out.println("testPlan1Method1- invoked");
}
@Test
public void testPlan1Method2()throws Exception {
System.out.println("testPlan1Method2 - invoked");
}
@Test
public void testPlan1Method3()throws Exception {
System.out.println("testPlan1Method3- invoked");
}
@Test
public void testPlan1Method4()throws Exception {
System.out.println("testPlan1Method4 - invoked");
}
}
2. TestPlan2.java
package com.test.sample;
import org.testng.annotations.Test;
public class TestPlan2{
@Test
public void testPlan2Method1()throws Exception {
System.out.println("testPlan2Method1- invoked");
}
@Test
public void testPlan2Method2()throws Exception {
System.out.println("testPlan2Method2 - invoked");
}
}
3. DynamicTestPlanTest.java
package com.test.sample;
import java.util.ArrayList;
import java.util.List;
import org.testng.annotations.Factory;
public class DynamicTestPlanTest {
TestPlan1 obj1;
TestPlan2 obj2;
@Factory
public Object[] setUp() throws Exception {
List list=new ArrayList();
int a=30,b=20,c;
if(a>b){
obj1=new TestPlan1();
list.add(obj1);
}else{
obj2=new TestPlan2();
list.add(obj2);
}
Object[] data = list.toArray();
return data;
}
}
4. testng.xml
<!DOCTYPE suite SYSTEM "http://beust.com/testng/testng-1.0.dtd" >
<suite name="DynamicTestPlanTestSuite" verbose="1" >
<test name="DynamicTestPlanTest" preserve-order="true">
<classes>
<class name="com.test.sample.DynamicTestPlanTest"></class>
</classes>
</test>
</suite>
Output:
testPlan1Method1- invoked
testPlan1Method2- invoked
testPlan1Method3- invoked
testPlan1Method4- invoked
We can call the test plans dynamically using @Factory Annotation in testng.
Factories - Factories allow you to create tests dynamically
@Factory - Marks a method as a factory that returns objects that will be used by TestNG as Test classes. The method must return Object[].
Please find the below steps
- In your MainTestPlan have a method with @Factory annotation which determines the type of test plan to be called dynamically and return the Object array
- Have your Test Plans with your test methods
- Configure your testng.xml file using classes tag and define your main test plan class name
------------------------------------------------------------------------------------------------------
1. TestPlan1.java
package com.test.sample;
import org.testng.annotations.Test;
public class TestPlan1{
@Test
public void testPlan1Method1()throws Exception {
System.out.println("testPlan1Method1- invoked");
}
@Test
public void testPlan1Method2()throws Exception {
System.out.println("testPlan1Method2 - invoked");
}
@Test
public void testPlan1Method3()throws Exception {
System.out.println("testPlan1Method3- invoked");
}
@Test
public void testPlan1Method4()throws Exception {
System.out.println("testPlan1Method4 - invoked");
}
}
2. TestPlan2.java
package com.test.sample;
import org.testng.annotations.Test;
public class TestPlan2{
@Test
public void testPlan2Method1()throws Exception {
System.out.println("testPlan2Method1- invoked");
}
@Test
public void testPlan2Method2()throws Exception {
System.out.println("testPlan2Method2 - invoked");
}
}
3. DynamicTestPlanTest.java
package com.test.sample;
import java.util.ArrayList;
import java.util.List;
import org.testng.annotations.Factory;
public class DynamicTestPlanTest {
TestPlan1 obj1;
TestPlan2 obj2;
@Factory
public Object[] setUp() throws Exception {
List list=new ArrayList();
int a=30,b=20,c;
if(a>b){
obj1=new TestPlan1();
list.add(obj1);
}else{
obj2=new TestPlan2();
list.add(obj2);
}
Object[] data = list.toArray();
return data;
}
}
4. testng.xml
<!DOCTYPE suite SYSTEM "http://beust.com/testng/testng-1.0.dtd" >
<suite name="DynamicTestPlanTestSuite" verbose="1" >
<test name="DynamicTestPlanTest" preserve-order="true">
<classes>
<class name="com.test.sample.DynamicTestPlanTest"></class>
</classes>
</test>
</suite>
Output:
testPlan1Method1- invoked
testPlan1Method2- invoked
testPlan1Method3- invoked
testPlan1Method4- invoked