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

 public void method1()  {
 System.out.println("method 1 invoked");
 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:


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);


package com.test;

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

@Target(value={ElementType.METHOD, ElementType.TYPE})
public @interface Priority {
  int value() default 0;


package com.test;

import org.testng.annotations.Test;

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

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

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

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


4. testng.xml

<!DOCTYPE suite SYSTEM "" >
<suite name="PriorityTestSuite" verbose="1" >
        <listener class-name="com.test.PriorityInterceptor" />

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


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