1.6. Example of IMethodInterceptor

1.6. Example of IMethodInterceptor


IMethodInterceptor interface is used to modify the list of test methods that we want TestNG to run. It will be invoked right before TestNG starts invoking test methods.
It has just one method to implement intercept which returns the altered list of methods.
Lets being with our test class. MethodInterceptorListenerExample has two test methods. One of the test methods testcase1 is to test performance so we grouped it in “sanity”.
Suppose we want to only run the SANITY based tests and not the other tests, we will have to provide aIMethodInterceptor listener that can filter out the other tests and return only SANITY based tests.



MethodInterceptorListenerExample:
package com.IMethodInterceptor;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;
@Listeners({com.IMethodInterceptor.MethodInterceptorListener.class})
public class MethodInterceptorListenerExample {
@Test(groups="sanity")
public void testcase1() {
System.out.println("am in test method: t1 and i will executed since belog to Sanity group");
}
@Test
public void testcase2() {
System.out.println("am in test method: t2");
}
}
MethodInterceptorListener is our listener class. You can see we are returning an altered method list, filtering methods other than methods belonging to “sanity” group.
package com.IMethodInterceptor;
import java.lang.reflect.Executable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.testng.IMethodInstance;
import org.testng.IMethodInterceptor;
import org.testng.ITestContext;
import org.testng.annotations.Test;
public class MethodInterceptorListener implements IMethodInterceptor {
@Override
public List<IMethodInstance> intercept(List<IMethodInstance> methods,
ITestContext context) {
List<IMethodInstance> result = new ArrayList<IMethodInstance>();
for (IMethodInstance m : methods) {
Test test = m.getMethod().getMethod().getAnnotation(Test.class);
Set<String> groups = new HashSet<String>();
for (String group : test.groups()) {
groups.add(group);
}
if (groups.contains("sanity")) {
result.add(m);
} else {
String testMethod = m.getMethod().getMethodName();
System.out.println(testMethod
+ " not a SANITY test so not included");
}
}
return result;
}
}
methodInterceptorListenerTestng.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="false">
<listeners>
<listener class-name="com.IMethodInterceptor.MethodInterceptorListener" />
</listeners>
<test name="Test">
<classes>
<class name="com.IMethodInterceptor.MethodInterceptorListenerExample" />
</classes>
</test>
</suite>
view raw 3.testng.xml hosted with ❤ by GitHub
Output :
From the output, we see only tesecase1 has run.
[TestNG] Running:
testcase2 not a SANITY test so not included
am in test method: t1
===============================================
Suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================
view raw 4.Output.txt hosted with ❤ by GitHub

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.