TestNG官方网站:
http://testng.org/doc/documentation-main.html
官方文档:
5.16 - Method Interceptors
Once TestNG has calculated in what order the test methods will be invoked, these methods are split in two groups:
Methods run sequentially. These are all the test methods that have dependencies or dependents. These methods will be run in a specific order.
Methods run in no particular order. These are all the methods that don't belong in the first category. The order in which these test methods are run is random and can vary from one run to the next (although by default, TestNG will try to group test methods by class).
In order to give you more control on the methods that belong to the second category, TestNG defines the following interface: public interface IMethodInterceptor {
List<IMethodInstance> intercept(List<IMethodInstance> methods, ITestContext context);
}
The list of methods passed in parameters are all the methods that can be run in any order. Your intercept method is expected to return a similar list of IMethodInstance, which can be either of the following:
The same list you received in parameter but in a different order.
A smaller list of IMethodInstance objects.
A bigger list of IMethodInstance objects.
Once you have defined your interceptor, you pass it to TestNG as a listener. For example:
Shelljava -classpath "testng-jdk15.jar:test/build" org.testng.TestNG -listener test.methodinterceptors.NullMethodInterceptor
-testclass test.methodinterceptors.FooTest
For the equivalent ant syntax, see the listeners attribute in the ant documentation.
For example, here is a Method Interceptor that will reorder the methods so that test methods that belong to the group "fast" are always run first:
/** * */ 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) */ @Override public 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.Test; /** * @author longgangbai * 2013-11-29 下午4:36:54 * */ public class MyMethodInterceptorTest { @Test(groups={"fast"}) public void test1() { System.out.println("=================test1============="); } @Test public void test2() { System.out.println("=================test2============="); } @Test(groups="init") public void test3() { System.out.println("=================test3============="); } }
package com.easyway.testng.junit; import org.testng.TestNG; /** * @author longgangbai * 2013-11-29 下午4:20:51 * */ public class MyMethodInterceptorTestMain { public static void main(String[] args) { TestNG tng = new TestNG(); tng.addListener( new MyMethodInterceptor()); tng.setTestClasses(new Class[]{MyMethodInterceptorTest.class}); tng.run(); } }
测试结果:
[TestNG] Running: Command line suite groups +[fast] groups +[] groups +[init] groups +[fast] groups +[] groups +[init] =================test1============= =================test2============= =================test3============= =============================================== Command line suite Total tests run: 3, Failures: 0, Skips: 0 ===============================================
相关推荐
Spring AOP还允许你定义自定义的方法拦截器(Method Interceptors),这些拦截器可以在方法调用前后执行特定操作。通过`@Aspect`和`@Around`注解,你可以创建一个拦截器来替换或增强方法的行为。 9. **测试配置类*...
这一章可能包含如何使用Struts2 TestNG或JUnit插件进行Action类的测试。 9. **最佳实践与性能优化**:最后,你可能会学到一些Struts2的最佳实践,如何避免常见陷阱,以及如何优化Struts2应用程序以提升性能。 以上...
这部分可能会介绍如何使用JUnit或TestNG进行测试,以及如何利用Struts2的调试工具进行问题定位。 9. **性能优化(Performance Optimization)**:可能讨论如何配置Struts2以提高应用程序的性能,如缓存策略、减少...
- Struts2有内置的测试工具,如Struts2 TestNG插件,便于进行单元测试和集成测试。 综上,这个“struts2 API帮助文档”对于开发人员深入理解和使用Struts2框架至关重要,涵盖了框架的各个方面,从基础组件到高级...
使用 JUnit 或 TestNG 进行单元测试,集成 Maven 或 Gradle 进行构建自动化,可以轻松运行测试、检查代码风格和执行其他构建任务。 ### 6. 配置与扩展 VRaptor 4 允许自定义配置,包括拦截器、事件处理器、异常...