- TestNG 教程
- TestNG - 首页
- TestNG - 概述
- TestNG - 环境
- TestNG -编写测试
- TestNG - 基本注解
- TestNG - 执行过程
- TestNG - 执行测试
- TestNG - 套件测试
- TestNG - 忽略测试
- TestNG - 组测试
- TestNG - 异常测试
- TestNG - 依赖测试
- TestNG - 参数化测试
- TestNG - 运行JUnit测试
- TestNG - 测试结果
- TestNG - 注解变换器
- TestNG - 断言
- TestNG - 并行执行
- TestNG - 与ANT集成
- TestNG - 与Eclipse集成
- TestNG - TestNG vs JUnit
- TestNG 有用资源
- TestNG - 快速指南
- TestNG - 有用资源
- TestNG - 讨论
TestNG - 基本注解 - 变换器
在某些情况下,您可能希望根据在测试执行过程中动态评估的某些条件或标准来执行 TestNG 测试。例如:
启用或禁用测试
在运行时添加数据提供者
为了实现这一点,您需要使用注解变换器。注解变换器是一个实现以下接口的类:
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 测试行为。使用此监听器,我们可以通过调用其setter方法来修改@Test注解中定义的所有属性的值。
创建类
让我们在下面的示例中看看注解变换器的用法。让我们跳过标记到特定组的测试,而无需每次都更改 TestNG 套件。创建一个要测试的 Java 类,例如,在 **/work/testng/src** 中的 **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");
}
}
创建变换器类案例类
创建一个 Java 测试类,例如,在 **/work/testng/src** 中的 **TestTransformer.java**(实现 IAnnotationTransformer)。
重写方法 transform()。
向方法 test1() 和 test2() 添加注解 @Test 并对其进行分组。
添加逻辑以跳过具有组名 *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 ===============================================
广告