- 测试NG教程
 - 测试NG - 主页
 - TestNG - 概述
 - TestNG - 环境
 - TestNG - 编写测试
 - TestNG - 基本注释
 - TestNG - 执行过程
 - TestNG - 执行测试
 - TestNG - 套件测试
 - TestNG - 忽略测试
 - TestNG - 小组测试
 - TestNG - 异常测试
 - TestNG - 依赖性测试
 - TestNG - 参数化测试
 - TestNG - 运行 JUnit 测试
 - TestNG - 测试结果
 - TestNG - 注释变压器
 - TestNG - 断言
 - TestNG - 并行执行
 - TestNG - 带 ANT 的插头
 - TestNG - 使用 Eclipse 进行插件
 - TestNG - TestNG - 与 JUnit
 
- TestNG 有用资源
 - TestNG - 快速指南
 - TestNG - 有用的资源
 - TestNG - 讨论
 
TestNG - 基本注释 - 变压器
在某些情况下,您可能希望根据在测试执行过程中动态评估的某些条件或标准来执行 TestNG 测试。例如:
启用或禁用测试
在运行时添加数据提供者
为了实现这一点,您需要使用注释转换器。Annotation Transformer 是一个实现以下接口的类:
  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);
  }
您可以在命令行或使用 ant 指定此类,如下所示:
java org.testng.TestNG -listener TestTransformer testng.xml
或以编程方式如下所示:
TestNG test = new TestNG(); test.setAnnotationTransformer(new TestTransformer()); // ...
此接口IAnnotationTransformer修改运行时的默认 TestNG 测试Behave。使用这个监听器,我们可以通过调用它们的setter来修改@Test注释中定义的所有属性的值。
创建一个类
让我们在下面的示例中看看注释转换器的用法。让我们跳过标记到指定组的测试,而无需每次都更改 TestNG 套件。在/work/testng/src中创建一个要测试的 java 类,例如ListenerTest.java。
  import org.testng.annotations.Test;
  public class ListenerTest {
  	@Test(groups={"betaTest","aplhaTest"})
  	public void test1() {
  		System.out.println("I am test1");
  	}
  	@Test(groups={"aplhaTest"})
  	public void test2() {
  		System.out.println("I am test2");
  	}
  }
创建 Transformer 类 Case 类
在/work/testng/src中创建一个 java 测试类,例如TestTransformer.java(实现 IAnnotationTransformer)。
重写方法transform()。
将注释 @Test 添加到方法 test1() 和 test2() 并将它们分组。
添加逻辑以跳过组名称betaTest 的测试。
以下是TestTransformer.java内容。
  import java.lang.reflect.Constructor;
  import java.lang.reflect.Method;
  import java.util.Arrays;
  import java.util.List;
  import org.testng.IAnnotationTransformer;
  import org.testng.annotations.ITestAnnotation;
  public class TestTransformer implements IAnnotationTransformer{
  	@Override
  	public void transform(ITestAnnotation annotation, Class testClass,
  			Constructor testConstructor, Method testMethod) {
  		List groupNames = Arrays.asList(annotation.getGroups());
  		System.out.println(groupNames.toString());
  		//Value 'betaTest' can be read from many places like properties file, run time parameter etc...
  		//For Simplicity, group is hardcoded in this program
  		String groupNameToSkip = "betaTest";
  		if(groupNames.contains(groupNameToSkip)){
  			System.out.println("found group name");
  			annotation.setEnabled(false);
  		}
  	}
  }
 
创建testng.xml
接下来,让我们在/work/testng/src中创建 testng.xml 文件,以执行测试用例。
  <suite name="Suite" parallel="classes" thread-count="2">
  <listeners>
    <listener class-name="TestTransformer"></listener>
  </listeners>
  <test name="Test">
    <classes>
      <class name="ListenerTest"/>
    </classes>
  </test>
</suite>
使用 javac 编译测试用例。
/work/testng/src$ javac TestTransformer.java ListenerTest.java
现在,运行 testng.xml,它将运行 <test> 标记中定义的测试用例。正如您所看到的,以名称betaTest分组的测试被跳过。
/work/testng/src$ java org.testng.TestNG testng.xml
验证输出。
[aplhaTest] [betaTest, aplhaTest] found group name I am test2 =============================================== Suite Total tests run: 1, Passes: 1, Failures: 0, Skips: 0 ===============================================