Testng 简明教程
TestNG - Basic Annotations - Transformers
在某些情况下,您可能希望根据一些条件或标准执行 TestNG 测试,这些条件或标准在测试执行过程中动态评估。例如:
In some scenarios you might want to execute TestNG tests based on some condition or criteria that is evaluated dynamically while test execution is in progress.Such as :
-
Enable or disable a test
-
Add data provider at run time
为此,您需要使用注解转换器。注解转换器是一个实现了以下接口的类:
In order to achieve this, you need to use an Annotation Transformer. An Annotation Transformer is a class that implements the following interface:
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 指定此类:
You can specify this class either on the command line or with ant as shown below:
java org.testng.TestNG -listener TestTransformer testng.xml
或按如下所示以编程方式指定:
or programmatically as shown below:
TestNG test = new TestNG();
test.setAnnotationTransformer(new TestTransformer());
// ...
此接口 IAnnotationTransformer 修改了运行时中默认的 TestNG 测试行为。使用此监听器,我们可以通过调用其 set 方法修改 @Test 注解中定义的所有属性的值。
This interface IAnnotationTransformer modifies the default TestNG tests behavior at run time. Using this listener, we can modify values of all attributes defined in @Test annotation by calling their setters.
Create a Class
让我们在以下示例中了解注解转换器的用法。我们可以跳过标记到指定组的测试,而不必每次都更改 TestNG 套件。创建一个要测试的 Java 类,例如 ListenerTest.java ,位于 /work/testng/src 。
Let’s see the usage of annotation transformer in below example. Let us skip tests that are tagged to a specified group without changing TestNG suite every time. Create a java class to be tested, say, ListenerTest.java in /work/testng/src.
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");
}
}
Create Tansformer class Case Class
-
Create a java test class, say, TestTransformer.java(that implements IAnnotationTransformer) in /work/testng/src.
-
Override method transform().
-
Add an Annotation @Test to methods test1() and test2() and group them.
-
Add logic to skip tests with group name betaTest.
以下是 TestTransformer.java 的内容。
Following are the TestTransformer.java contents.
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);
}
}
}
Create testng.xml
接下来,让我们在 /work/testng/src 中创建一个 testng.xml 文件来执行测试用例。
Next, let’s create testng.xml file in /work/testng/src, to execute test case(s).
<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 编译测试用例。
Compile the test case using javac.
/work/testng/src$ javac TestTransformer.java ListenerTest.java
现在,运行 testng.xml,这将运行 <test> 标签中定义的测试用例。正如您所看到的,分组在名称 betaTest 下的测试已被跳过。
Now, run the testng.xml, which will run the test case defined in <test> tag. As you can see the tests grouped under name betaTest are skipped.
/work/testng/src$ java org.testng.TestNG testng.xml
验证输出。
Verify the output.
[aplhaTest]
[betaTest, aplhaTest]
found group name
I am test2
===============================================
Suite
Total tests run: 1, Passes: 1, Failures: 0, Skips: 0
===============================================