TestNG简单的学习(十)TestNG @Listeners 的使用
TestNG官方网站:
http://testng.org/doc/documentation-main.html
?
官方文档:
/** * */package com.easyway.testng.junit;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;/** * @author longgangbai 2013-11-29 下午4:31:40 * */public class MyMethodInterceptor implements IMethodInterceptor {/* * (non-Javadoc) * * @see org.testng.IMethodInterceptor#intercept(java.util.List, * org.testng.ITestContext) */@Overridepublic List<IMethodInstance> intercept(List<IMethodInstance> methods,ITestContext context) {List<IMethodInstance> result = new ArrayList<IMethodInstance>();for (IMethodInstance m : methods) {Test test = m.getMethod().getConstructorOrMethod().getMethod().getAnnotation(Test.class);Set<String> groups = new HashSet<String>();for (String group : test.groups()) {groups.add(group);}System.out.println("groups +"+groups );if (groups.contains("fast")) {result.add(0, m);} else {result.add(m);}}return result;}}
?
/** * */package com.easyway.testng.junit;import org.testng.annotations.Listeners;import org.testng.annotations.Test;/** * 5.17.1 - Specifying listeners with testng.xml or in JavaHere is how you can define listeners in your testng.xml file: view sourceprint?<suite> <listeners> <listener class-name="com.example.MyListener" /> <listener class-name="com.example.MyMethodInterceptor" /> </listeners> ... Or if you prefer to define these listeners in Java: view sourceprint?@Listeners({ com.example.MyListener.class, com.example.MyMethodInterceptor.class }) public class MyTest { // ... } The @Listeners annotation can contain any class that extends org.testng.ITestNGListener except IAnnotationTransformer and IAnnotationTransformer2. The reason is that these listeners need to be known very early in the process so that TestNG can use them to rewrite your annotations, therefore you need to specify these listeners in your testng.xml file. Note that the @Listeners annotation will apply to your entire suite file, just as if you had specified it in a testng.xml file. If you want to restrict its scope (for example, only running on the current class), the code in your listener could first check the test method that's about to run and decide what to do then. * @author longgangbai * 2013-11-29 下午4:12:17 * */@Listeners({ MyMethodInterceptor.class }) public class MyListenerTest {@Testpublic void test1() { System.out.println("=================test1=============");} @Testpublic void test2() { System.out.println("=================test2=============");} }
?
?