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();

	}
}

 

你可能感兴趣的:(Junit4,TestNG,Annotation,Transformers,的使用)