Thursday 5 July 2012

How to call the test plans dynamically

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. 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
  2. Have your Test Plans with your test methods
  3. Configure your testng.xml file using classes tag and define your main test plan class name
Please find the sample code below.
 
------------------------------------------------------------------------------------------------------
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








 

7 comments:

  1. Can we provide the dynamic name for the testcase ?

    ReplyDelete
  2. it is very helpful blog good explanation compared to other blog keep blogging

    check this too :
    Selenium Training in Chennai

    ReplyDelete
  3. Hello, I read your blog occasionally, and I own a similar one, and I was just wondering if you get a lot of spam remarks? If so how do you stop it, any plugin or anything you can advise? I get so much lately it’s driving me insane, so any assistance is very much appreciated.
    Android Training in Chennai | Best Android Training in Chennai
    Matlab Training in Chennai | Best Matlab Training in Chennai
    Best AWS Training in Chennai | AWS Training in Chennai
    Selenium Training in Chennai | Best Selenium Training in chennai
    Devops Course Training in Chennai | Best Devops Training in Chennai

    ReplyDelete
  4. wonderful article. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article.
    AWS training in chennai | AWS training in anna nagar | AWS training in omr | AWS training in porur | AWS training in tambaram | AWS training in velachery

    ReplyDelete
  5. Hello! This is my first visit to your blog! We are a team of volunteers and starting a new initiative in a community in the same niche..keepp it up
    Ai & Artificial Intelligence Course in Chennai
    PHP Training in Chennai
    Ethical Hacking Course in Chennai Blue Prism Training in Chennai
    UiPath Training in Chennai

    ReplyDelete
  6. Some may stag in Interviews!!! OOPS!! More than 50% of students do this in their career. Instead, do Hadoop Training in Chennai at Infycle. Those students can easily clear this Interview session because more than 5 times at INFYCLE practicing mock-interview sessions, Hence students are Getting out of their interview fear.

    ReplyDelete