1.6. Example of 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 which returns the altered list of methods.
It has just one method to implement which returns the altered list of methods.
Lets being with our test class. has two test methods. One of the test methods 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 a listener that can filter out the other tests and return only SANITY based tests.
Suppose we want to only run the SANITY based tests and not the other tests, we will have to provide a listener that can filter out the other tests and return only SANITY based tests.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
=============================================== |
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.