`
longgangbai
  • 浏览: 7250700 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

TestNG简单的学习(九)TestNG Method Interceptors 的使用

阅读更多

 

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
===============================================

 

 

分享到:
评论

相关推荐

    TestNG中文手册学习笔记

    TestNG中文手册及学习笔记,包括:环境配置、详细使用说明及实例。

    testng测试报告模板BeautifulReport.7z

    1、testng测试报告模板BeautifulReport 2、testng.xml文件

    TestNG安装包

    让我们调用使用testng.xml文件。创建一个XML文件名称testng.xml C:\ &gt; TestNG_WORKSPACE 执行测试用例(s) &lt;!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" &gt; &lt;/suite&gt; 第7步:检查结果 ...

    TestNG学习笔记

    学习笔记资料来源:http://www.yiibai.com/testng/;从易佰TestNG教程整理,包括概念介绍,代码实例。

    使用TestNG-Abbot实现自动化GUI测试

    TestNG-Abbot源自于两个成功的开发人员测试框架的结合:Abbot和TestNG。Abbot是一种JUnit扩展框架,主要目的...一旦理解了它,您会发现将GUI组件隔离并使用框架所含的极其方便的fixture对象对其进行验证是多么地简单。

    testng-xslt-1.1.2.zip

    testng-xslt-1.1.2.zip

    TestNG官方文档中文版

    TestNG官方文档中文版,详细的介绍了testng的使用指南。

    使用 Selenium 和 TestNG 进行编程式测试

    讲解如何使用Selenium 和 TestNG 进行编程式测试

    testNG6.11.0

    1.下载testNG 离线安装包【eclipse-testng离线包】,并解压。 2.将解压后的文件..\eclipse-testng离线包\features\目录下的文件夹org.testng.eclipse_6.11.0.201703011520放到eclipse安装路径下的features目录下 3....

    testng-6.3.jar

    testNG 自动化测试工具 工具类 testng-6.3.jar

    TestNG的离线安装包

    1. 将解压后的文件..\eclipse-testng离线包\features\org.testng.eclipse_6.9.9.201510270734 文件夹 放到 eclipse--》features目录下 2. 将解压后的文件..\eclipse-testng离线包\plugins\org.testng.eclipse_6.9...

    TestNG学习中遇到的坑.txt

    2017年6月5日记:在学习直接用手工执行testng.xml时,得到以下教训: 1、如果写批处理调用,环境变量的末尾必须以分号结束,否则调试气死你也搞不定啥原因,控制台就一句话,找不到类;

    testng-6.9.4和testng-6.8.8.zip

    testng-6.8.8

    eclipse 安装testNG 离线包

    1. 将解压后的文件..\eclipse-testng离线包\features\org.testng.eclipse_6.9.9.201510270734 文件夹 放到 eclipse--》features目录下 2. 将解压后的文件..\eclipse-testng离线包\plugins\org.testng.eclipse_...

    testng 支持jdk1.6的版本

    testng目前需要jdk1.8,此jar包支持1.6

    [TestNG] TestNG 初学者指南 (英文版)

    [Packt Publishing] TestNG 初学者指南 (英文版) [Packt Publishing] TestNG Beginner's Guide (E-Book) ☆ 图书概要:☆ Write robust unit and functional tests with the power of TestNG Overview Step-by-...

    testng-6.9-10

    testng-6.9-10 jar包

    testng官方文档及使用图解.rar

    TestNG官方文档中文版.pdf TestNG 图解说明.pdf

    eclipse-testng离线包

    接口测试,eclipse testng离线包是eclipse的插件,怎么在Eclipse下配置TestNG环境。 ... ... 3. 配置Java环境变量: JAVA_HOME,PATH,CLASSPATH 4. 安装Eclipse TestNG插件 ...6. 在Java Project中使用TestNG测试

    eclipse-testng离线安装包

    1.下载testNG 离线安装包【eclipse-testng离线包】,并解压。 2.将解压后的文件..\eclipse-testng离线包\features\目录下的文件夹org.testng.eclipse_6.9.8.201510130443放到eclipse安装路径下的features目录下 3.将...

Global site tag (gtag.js) - Google Analytics