目錄
(一)TestNG學(xué)習(xí)之路—HelloWorld入門
(二)TestNG學(xué)習(xí)之路—注解及屬性概覽
(三)TestNG學(xué)習(xí)之路—TestNG.xml/YAML
(四)TestNG學(xué)習(xí)之路—注解詳述之@Test
(五)TestNG學(xué)習(xí)之路—注解詳述之參數(shù)化
(六)TestNG學(xué)習(xí)之路—注解詳述之@Factory
(七)TestNG學(xué)習(xí)之路—注解詳述之忽略測(cè)試
(八)TestNG學(xué)習(xí)之路—注解詳述之并發(fā)
(九)TestNG學(xué)習(xí)之路—失敗測(cè)試重跑
(十)TestNG學(xué)習(xí)之路—編碼執(zhí)行TestNG
(十一)TestNG學(xué)習(xí)之路—BeanShell高級(jí)用法
(十二)TestNG學(xué)習(xí)之路—注解轉(zhuǎn)換器
(十三)TestNG學(xué)習(xí)之路—方法攔截器
(十四)TestNG學(xué)習(xí)之路—TestNG監(jiān)聽器
(十五)TestNG學(xué)習(xí)之路—依賴注入
(十六)TestNG學(xué)習(xí)之路—測(cè)試報(bào)告
(十七)基于TestNG+Rest Assured+Allure的接口自動(dòng)化測(cè)試框架
前言
TestNG允許您在測(cè)試執(zhí)行時(shí)修改所有注解(@Test,@DataProvider,@Factory等)的內(nèi)容亡笑,可以通過重寫IAnnotationTransformer膳凝,IAnnotationTransformer2 的方法來實(shí)現(xiàn)。IAnnotationTransformer 只能用來修改 @Test 注解缺脉,如果需要修改其他 TestNG 的注解(比如@DataProvider, @Factory 以及 @Configuration),需要使用 IAnnotationTransformer2 監(jiān)聽器。
示例
IAnnotationTransformer
通過javadoc可發(fā)現(xiàn),IAnnotationTransformer繼承了ITestNGListener接口喇辽,其要求實(shí)現(xiàn)transform方法。
public interface IAnnotationTransformer extends ITestNGListener{
/**
* 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);
}
通過重寫transform方法雨席,可改寫@Test注解的屬性菩咨,示例如下:
編寫測(cè)試類如下:
import org.testng.annotations.Test;
public class TestTransform {
@Test
public void test(){
System.out.println("Test annotationTransformer!");
}
}
IAnnotationTransformer實(shí)現(xiàn)類如下:
import org.testng.IAnnotationTransformer;
import org.testng.annotations.ITestAnnotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
public class Transform implements IAnnotationTransformer {
public void transform(ITestAnnotation iTestAnnotation, Class aClass, Constructor constructor, Method method) {
iTestAnnotation.setInvocationCount(2); //執(zhí)行2次
}
}
testng.xml配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="All Test Suite" >
<listeners>
<listener class-name="Transform"/>
</listeners>
<test verbose="2" preserve-order="true" name="Test">
<classes>
<class name="TestTransform">
</class>
</classes>
</test>
</suite>
執(zhí)行結(jié)果如下:
Test annotationTransformer!
Test annotationTransformer!
===============================================
All Test Suite
Total tests run: 2, Failures: 0, Skips: 0
===============================================
如上結(jié)果所示,可發(fā)現(xiàn)@Test注解的方法執(zhí)行了2次,說明Transform監(jiān)聽器起了作用抽米。
IAnnotationTransformer2
public interface IAnnotationTransformer2 extends IAnnotationTransformer {
void transform(IConfigurationAnnotation var1, Class var2, Constructor var3, Method var4);
void transform(IDataProviderAnnotation var1, Method var2);
void transform(IFactoryAnnotation var1, Method var2);
}
IAnnotationTransformer2繼承IAnnotationTransformer接口特占,其可以用于修改@DataProvider,@Factory,@Configuration注解,但@Configuration在新版本已經(jīng)被@BeforeSuite缨硝,@AfterSuite 等代替摩钙,所以此處不再探討。
編寫測(cè)試類:
import org.testng.annotations.Test;
public class TestTransform {
private String str;
public TestTransform(String str){
this.str = str;
}
@Test()
public void test(){
System.out.println("Test annotationTransformer!");
System.out.println("DataProviderName:"+str);
}
}
編寫工廠類查辩,該工廠類有兩個(gè)dataProvider(tom,data)胖笛,默認(rèn)使用tom。
import org.testng.annotations.DataProvider;
import org.testng.annotations.Factory;
public class TransformFactory {
@Factory(dataProvider = "tom")
public Object[] transformFac(String str){
Object[] objects = new Object[1];
for(int i=0;i<1;i++){
TestTransform testTransform = new TestTransform(str);
objects[i] = testTransform;
}
return objects;
}
@DataProvider(name = "tom")
public Object[][] tom(){
return new Object[][]{new Object[]{"tom"}};
}
@DataProvider(name = "data")
public Object[][] data(){
return new Object[][]{new Object[]{"data"}};
}
}
編寫IAnnotationTransformer2實(shí)現(xiàn)類如下宜岛,用于修改Factory的dataProvider為data长踊。
import org.testng.IAnnotationTransformer2;
import org.testng.annotations.IConfigurationAnnotation;
import org.testng.annotations.IDataProviderAnnotation;
import org.testng.annotations.IFactoryAnnotation;
import org.testng.annotations.ITestAnnotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
public class Transform2 implements IAnnotationTransformer2 {
public void transform(IConfigurationAnnotation iConfigurationAnnotation, Class aClass, Constructor constructor, Method method) {
}
public void transform(IDataProviderAnnotation iDataProviderAnnotation, Method method) {
if (iDataProviderAnnotation.getName().equals("tom")) //匹配名為data的DataProvider
iDataProviderAnnotation.setParallel(true); //設(shè)置并行
}
public void transform(IFactoryAnnotation iFactoryAnnotation, Method method) {
iFactoryAnnotation.setDataProvider("data");
}
public void transform(ITestAnnotation iTestAnnotation, Class aClass, Constructor constructor, Method method) {
}
}
編寫testng.xml(不使用監(jiān)聽器)如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="All Test Suite" >
<test verbose="2" preserve-order="true" name="Test">
<classes>
<class name="TransformFactory">
</class>
</classes>
</test>
</suite>
執(zhí)行結(jié)果如下:
Test annotationTransformer!
DataProviderName:tom
===============================================
Default Suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================
修改testng.xml(使用監(jiān)聽器)如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="All Test Suite" >
<listeners>
<listener class-name="Transform2"/>
</listeners>
<test verbose="2" preserve-order="true" name="Test">
<classes>
<class name="TransformFactory">
</class>
</classes>
</test>
</suite>
執(zhí)行結(jié)果如下:
Test annotationTransformer!
DataProviderName:data
===============================================
All Test Suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================
對(duì)比以上兩個(gè)執(zhí)行結(jié)果,可發(fā)現(xiàn)@Factory的dataProvider確實(shí)被修改了萍倡。
注意事項(xiàng)
@listener注解不能包含IAnnotationTransformer和IAnnotationTransformer2的實(shí)現(xiàn)類身弊,原因是這兩種監(jiān)聽器必須在更早的階段添加到 TestNG 中才能實(shí)施修改注釋的操作,所以它們只能在 testng.xml 添加列敲。還是最前面的例子阱佛,通過以下方式添加監(jiān)聽器不會(huì)起作用。
@Listeners(Transform.class)
public class TestTransform {
@Test
public void test(){
System.out.println("Test annotationTransformer!");
}
}