TestNG簡単な勉強(八)TestNG Annotation Trans formersの使用


もっと読む
TestNG公式サイト:
http://testng.org/doc/documentation-main.html
 
 
5.15-Annotation Transformers TestNG allows you to modify the contest of all the annotatititime.Thisisisisisespecialallyuseeful the annotatitititins the source code arrightmost of the time、but there thethetherererererererererererereaaaafefefefefeeeeeeeeaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaformer.
Annotation Trans former is a clast that implements the follwing interface:
view sourceprint?public interface IAnnotation Trans former{ 
  
  /** 
   * This method will be invoked by TestNG to give you a change 
   * to modify a TestNG annotation read from your test clases. 
   * You can change the values you need by caling 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 otherswise) 
   * @param testConstructor If the annotation was found on a constructor、 
   * this parameter represents this constructor(null otherswise) 
   * @param testMethod If the annotation was found on a method、 
   * this parameter represents this method(null otherswise) 
   */
  public void transform(ITest annotation、Class testClass、 
      コンストラクター、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.testing.TestNG-listener MyTrans former testing.xml or programmatially:
view sourceprint?TestNG tng=new TestNG(); 
tng.set AnnotationTrans former(new MyTrans former); 
//When the method transform()is invoked,you can can cal any of the setters on the ITest test parameter to alter before TestNG proceders.For example,here is how You world overrithe trunite the tonit
view sourceprint?public class MyTrans former implements IAnnotation Trans former{ 
  public void transform(ITest annotation、Class testClass、 
      コンストラクター、Method testMethod) 
  { 
    if("invoke".equals(testMethod.get Name)){ 
      annotations.set InvocationCount(5) 
    } 
  } 
}IAnnotation Trans former only lets you modify a@Test annotation.If you need to modify another TestNG annotation(a configration annotation、@Factory or@DataProvider)、use an IAnnotation Transformer 2.
 
 
 
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();

	}
}