bellum
bellum

Reputation: 3710

Must IMethodInterceptor be called once before running test methods or what?

As I understood, method interceptor can be used to build list of running methods by their priorities. But in my project method interceptor runs for test methods from each test class.

For example, there are two classes that were put to method setTestClasses. In the first class there are 3 test methods (with similar priorities). In the second there is one (with the highest priority). In this situation the method with highest priority will be run the latest because method interceptor will be run firstly for 3 methods from first class and secondly for the method from second class. Is it correct?

Upvotes: 0

Views: 1972

Answers (1)

Dharshana
Dharshana

Reputation: 1232

What method interceptor does it it gives out list of methods by List. Inside the method interceptor's intercept method you can reorder the list and return List object. So in the execution the Testng will use your modified list. In staid of the original list inputs to intercept method. That is the use of method interceptor in testng. And it doesn't deal with priorities in this level. Of cause you can use methodinterceptor to reorder test list as priorities by implementing method interceptor.

See the below sample code for clarification.

public class methodSortingListner implements IMethodInterceptor {
public List<IMethodInstance> intercept(List<IMethodInstance> methods, ITestContext context) {
    List<IMethodInstance> result = new ArrayList<IMethodInstance>();
    for (IMethodInstance method : methods) {
          if(<your logic here>)
          {
             result.add(method);
          }
    }

 return result;

}

This will gives out a list as suits your logic and that will be executed by testng

Thank you, Dharshana.

Upvotes: 2

Related Questions