Thursday, 5 July 2012

How to execute the testng test methods in order we specified?
We can execute the testng test methods in order we specified using Priority IMethodInterceptor

1. Declare a class(PriorityInterceptor) implementing the interface IMethodInterceptor
2. TestNG passes the list of methods it's about to run so you get a chance to reorder them.
3. We can declare three kinds of priorities:
    1. Priorities at the method level.
    2. A priority at the class level.
    3. No priority. When no priority is defined on a method, we want to look for a priority on the class first, and if none can be found, the priority will receive a default value of 0.
4. In your testplan(PriorityTestPlan) define the Priority as shown below


@Priority(1)
 @Test
 public void method1()  {
 System.out.println("method 1 invoked");
 }
 @Priority(-1)
 @Test
 public void method2()  {
 System.out.println("method 2 invoked");
 }

5. Next, we need to implement the @Priority annotation
6. Now we need to declare our interceptor in the testng.xml file using listner tags.

Please find below the sample code for calling the test methods in order we specified using Priority IMethodInterceptor

Sample Code Below:
--------------------------------------------------------------------------------------------------------

1. PriorityInterceptor.java

package com.test;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

import org.testng.IMethodInstance;
import org.testng.IMethodInterceptor;
import org.testng.ITestContext;

import com.test.Priority;

public class PriorityInterceptor implements IMethodInterceptor {

      public List<IMethodInstance> intercept(List<IMethodInstance> methods, ITestContext context) {
        Comparator<IMethodInstance> comparator = new Comparator<IMethodInstance>() {
         
          private int getPriority(IMethodInstance mi) {
            int result = 0;
            Method method = mi.getMethod().getMethod();
            Priority a1 = method.getAnnotation(Priority.class);
            if (a1 != null) {
              result = a1.value();
            } else {
              Class cls = method.getDeclaringClass();
              Priority classPriority = (Priority) cls.getAnnotation(Priority.class);
              if (classPriority != null) {
                result = classPriority.value();
              }
            }
            return result;
          }

          public int compare(IMethodInstance m1, IMethodInstance m2) {
            return getPriority(m1) - getPriority(m2);
          }
         
        };
        IMethodInstance[] array = methods.toArray(new IMethodInstance[methods.size()]);
        Arrays.sort(array, comparator);

        return Arrays.asList(array);
      }

    }

 2. Priority.java
package com.test;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

@Retention(java.lang.annotation.RetentionPolicy.RUNTIME)
@Target(value={ElementType.METHOD, ElementType.TYPE})
public @interface Priority {
  int value() default 0;
}


3. PriorityTestPlan.java

package com.test;

import org.testng.annotations.Test;

public class PriorityTestPlan   {
  @Test
  public void method1() { System.out.println("M1 Default priority");}

  @Priority(1)
  @Test
  public void method2()  { System.out.println("M2 Priority 1");}
 
  @Priority(3)
  @Test
  public void method3()  { System.out.println("M3 Priority 3");}
 
  @Priority(-1)
  @Test
  public void method4()  { System.out.println("M4 Priority -1");}

  @Priority(2)
  @Test
  public void method5()  { System.out.println("M5 Priority 2");}

  @Priority(2)
  @Test
  public void method6()  { System.out.println("M6 Priority 2");}

}

4. testng.xml

<!DOCTYPE suite SYSTEM "http://beust.com/testng/testng-1.0.dtd" >
<suite name="PriorityTestSuite" verbose="1" >
    <listeners>
        <listener class-name="com.test.PriorityInterceptor" />
    </listeners>

   
    <test name="PriorityTestPlan" preserve-order="true">
    <classes>
        <class name="com.test.PriorityTestPlan"></class>
    </classes>
    </test>
</suite>


Output:

M4 Priority -1
M2 Priority 1
M5 Priority 2
M6 Priority 2
M3 Priority 3
M1 Default priority

5 comments: