(十二)TestNG學(xué)習(xí)之路—注解轉(zhuǎn)換器

目錄

(一)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!");
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末戴而,一起剝皮案震驚了整個(gè)濱河市凑术,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌所意,老刑警劉巖淮逊,帶你破解...
    沈念sama閱讀 217,084評(píng)論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異扶踊,居然都是意外死亡泄鹏,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,623評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門秧耗,熙熙樓的掌柜王于貴愁眉苦臉地迎上來备籽,“玉大人,你說我怎么就攤上這事分井〗禾ǎ” “怎么了?”我有些...
    開封第一講書人閱讀 163,450評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵杂抽,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我韩脏,道長(zhǎng)缩麸,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,322評(píng)論 1 293
  • 正文 為了忘掉前任赡矢,我火速辦了婚禮杭朱,結(jié)果婚禮上阅仔,老公的妹妹穿的比我還像新娘。我一直安慰自己弧械,他們只是感情好八酒,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,370評(píng)論 6 390
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著刃唐,像睡著了一般羞迷。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上画饥,一...
    開封第一講書人閱讀 51,274評(píng)論 1 300
  • 那天衔瓮,我揣著相機(jī)與錄音,去河邊找鬼抖甘。 笑死热鞍,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的衔彻。 我是一名探鬼主播薇宠,決...
    沈念sama閱讀 40,126評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼艰额!你這毒婦竟也來了澄港?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,980評(píng)論 0 275
  • 序言:老撾萬榮一對(duì)情侶失蹤悴晰,失蹤者是張志新(化名)和其女友劉穎慢睡,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體铡溪,經(jīng)...
    沈念sama閱讀 45,414評(píng)論 1 313
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡漂辐,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,599評(píng)論 3 334
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了棕硫。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片髓涯。...
    茶點(diǎn)故事閱讀 39,773評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖哈扮,靈堂內(nèi)的尸體忽然破棺而出纬纪,到底是詐尸還是另有隱情,我是刑警寧澤滑肉,帶...
    沈念sama閱讀 35,470評(píng)論 5 344
  • 正文 年R本政府宣布包各,位于F島的核電站,受9級(jí)特大地震影響靶庙,放射性物質(zhì)發(fā)生泄漏问畅。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,080評(píng)論 3 327
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望护姆。 院中可真熱鬧矾端,春花似錦、人聲如沸卵皂。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,713評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽灯变。三九已至殴玛,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間柒凉,已是汗流浹背族阅。 一陣腳步聲響...
    開封第一講書人閱讀 32,852評(píng)論 1 269
  • 我被黑心中介騙來泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留膝捞,地道東北人坦刀。 一個(gè)月前我還...
    沈念sama閱讀 47,865評(píng)論 2 370
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像蔬咬,于是被迫代替她去往敵國(guó)和親鲤遥。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,689評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容