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

TestNG简单的学习(八)TestNG Annotation Transformers 的使用

阅读更多

TestNG官方网站:

http://testng.org/doc/documentation-main.html

 

 

5.15 - Annotation Transformers
TestNG allows you to modify the content of all the annotations at runtime. This is especially useful if the annotations in the source code are right most of the time, but there are a few situations where you'd like to override their value.
In order to achieve this, you need to use an Annotation Transformer.

An Annotation Transformer is a class that implements the following interface:

view sourceprint?public interface IAnnotationTransformer { 

  

  /** 

   * This method will be invoked by TestNG to give you a chance 

   * to modify a TestNG annotation read from your test classes. 

   * You can change the values you need by calling any of the 

   * setters on the ITest interface. 

   *  

   * Note that only one of the three parameters testClass, 

   * testConstructor and testMethod will be non-null. 

   *  

   * @param annotation The annotation that was read from your 

   * test class. 

   * @param testClass If the annotation was found on a class, this 

   * parameter represents this class (null otherwise). 

   * @param testConstructor If the annotation was found on a constructor, 

   * this parameter represents this constructor (null otherwise). 

   * @param testMethod If the annotation was found on a method, 

   * this parameter represents this method (null otherwise). 

   */

  public void transform(ITest annotation, Class testClass, 

      Constructor testConstructor, Method testMethod); 

}
Like all the other TestNG listeners, you can specify this class either on the command line or with ant:

view sourceprint?java org.testng.TestNG -listener MyTransformer testng.xml
or programmatically:

view sourceprint?TestNG tng = new TestNG(); 

tng.setAnnotationTransformer(new MyTransformer()); 

// ...
When the method transform() is invoked, you can call any of the setters on the ITest test parameter to alter its value before TestNG proceeds further.
For example, here is how you would override the attribute invocationCount but only on the test method invoke() of one of your test classes:

view sourceprint?public class MyTransformer implements IAnnotationTransformer { 

  public void transform(ITest annotation, Class testClass, 

      Constructor testConstructor, Method testMethod) 

  { 

    if ("invoke".equals(testMethod.getName())) { 

      annotation.setInvocationCount(5); 

    } 

  } 

}
IAnnotationTransformer only lets you modify a @Test annotation. If you need to modify another TestNG annotation (a configuration annotation, @Factory or @DataProvider), use an IAnnotationTransformer2.

 

 

 
package com.easyway.testng.junit;

import org.testng.annotations.Test;


/**
 * @author longgangbai
 * 2013-11-29  下午4:12:17
 *
 */
public class MyTransferTest {
	@Test
	public void test1() { 
		System.out.println("=================test1=============");
	}  

	   
    
	@Test
	public void test2() {  
		System.out.println("=================test2=============");
	} 

}
package com.easyway.testng.junit;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;

import org.testng.IAnnotationTransformer;
import org.testng.annotations.ITestAnnotation;

/**
 * @author longgangbai 2013-11-29 下午4:18:34
 * 
 */
public class MyTransformer implements IAnnotationTransformer {

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.testng.IAnnotationTransformer#transform(org.testng.annotations.
	 * ITestAnnotation, java.lang.Class, java.lang.reflect.Constructor,
	 * java.lang.reflect.Method)
	 */
	@SuppressWarnings("rawtypes")
	@Override
	public void transform(ITestAnnotation annotation, Class testClass,
			Constructor testConstructor, Method testMethod) {
		System.out.println(" MyTransformer  "+testMethod);
		if ("test1".equals(testMethod.getName())) {
			annotation.setInvocationCount(5);
		}

	}

}

  

 

 

package com.easyway.testng.junit;

import org.testng.TestNG;

/**
 * @author longgangbai
 * 2013-11-29  下午4:20:51
 *
 */
public class MyTransformerTestMain {

	public static void main(String[] args) {
		TestNG tng = new TestNG();  
		tng.setAnnotationTransformer(new MyTransformer());  
		tng.setTestClasses(new Class[]{MyTransferTest.class});
		tng.run();

	}
}

 

分享到:
评论

相关推荐

    TestNG中文手册学习笔记

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

    testng测试报告模板BeautifulReport.7z

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

    TestNG安装包

    让我们调用使用testng.xml文件。创建一个XML文件名称testng.xml C:\ > TestNG_WORKSPACE 执行测试用例(s) <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" > </suite> 第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...

    Java测试新技术TestNG和高级概念.part2

    无论您使用TestNG,JUnit或其他测试框架,本书提供的测试设计模式都会告诉您如何改进您的测试,对如何让代码和设计变得更可测试提供具体的建议。 目录: 第1章 起步 1.1 超越JUnit 3 1.2 JUnit 4 1.3 针对可测试性而...

    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测试

Global site tag (gtag.js) - Google Analytics