TestNG之Factory篇

簡介

@Factory注解從字面意思上來講就是采用工廠的方法來創(chuàng)建測試數(shù)據(jù)并配合完成測試,其主要應(yīng)對的場景是:對于某一個測試用例或方法,我們需要輸入多個測試數(shù)據(jù)進行測試,并且這些測試數(shù)據(jù)可以是有一定關(guān)系(可以通過代碼控制),此時,我們就可以把自動化或者手動測試時的遇到的只因測試數(shù)據(jù)不同的多個測試用例合并成一個測試用例,來進行更方便和快捷的測試。

對編寫自己主動化測試代碼人員節(jié)省了非常多時間

策略:一般我們會在標(biāo)有@Factory注解的方法中對測試類進行調(diào)用,這時TestNg會自動調(diào)用測試類中帶有@Test注解的方法

配置文件:只需要配置帶有@Factory注解的類即可

@Factory必須放在一個返回對象數(shù)組的頂部,所有的這些對象都包含測試類的實例,testng會確保@Factory只被調(diào)用一次。

@Factory方法是首先被調(diào)用的,在@Test方法和配置方法之前,只有當(dāng)所有的@Factory方法被調(diào)用之后,testng才開始執(zhí)行配置和測試方法。

@Factory允許在運行時動態(tài)測試。

?上邊說了這么多是不是把大家說的云里霧里,暈頭轉(zhuǎn)向的,接下來通過具體的例子給小伙伴和同學(xué)們分享一下。

實例:

被測試類Person:

publicclass Person {

? ? String name;

? ? int age;


? ? @Parameters({"name","age"})

? ? publicPerson(String name,int age) {

? ? ? ? super();

? ? ? ? this.name = name;

? ? ? ? this.age = age;

? ? }

? ? @Test()

? ? publicvoid say() {

? ? ? ? System.out.print("我是"+name+" ");

? ? ? ? if(age<18){

? ? ? ? ? ? System.out.println("我未成年");

? ? ? ? }elseif(age>=18&&age<=45){

? ? ? ? ? ? System.out.println("我是青年人");

? ? ? ? }elseif(age>45&&age<=60){

? ? ? ? ? ? System.out.println("我是中年人");

? ? ? ? }elseif(age>60){

? ? ? ? ? ? System.out.println("我是老年人");

? ? ? ? }

? ? }

}

該類的say()方法中有四個判斷分支,為了測試充分,必須執(zhí)行四次這個方法,如果不使用@Factory注解,在TestNG配置文件中必須這樣配置:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">

<suite name="Suite">

? <test name="Test1">

? ? ? ? <parameter name="name" value="小明" />

? ? ? ? <parameter name="age" value="10" />

? ? ? ? <classes>

? ? ? ? ? ? <class name="hongge.Person" />

? ? ? ? </classes>

? ? </test>

? ? <test name="Test2">

? ? ? ? <parameter name="name" value="宏哥" />

? ? ? ? <parameter name="age" value="20" />

? ? ? ? <classes>

? ? ? ? ? ? <class name="hongge.Person" />

? ? ? ? </classes>

? ? </test>

? ? <test name="Test3">

? ? ? ? <parameter name="name" value="劉創(chuàng)" />

? ? ? ? <parameter name="age" value="50" />

? ? ? ? <classes>

? ? ? ? ? ? <class name="hongge.Person" />

? ? ? ? </classes>

? ? </test>

? ? <test name="Test4">

? ? ? ? <parameter name="name" value="爺爺" />

? ? ? ? <parameter name="age" value="70" />

? ? ? ? <classes>

? ? ? ? ? ? <class name="hongge.Person" />

? ? ? ? </classes>

? ? </test><!-- Test -->

</suite>

從上邊我們可以清楚地看出來:參數(shù)一旦多起來,就難以管理了,所以應(yīng)該使用工廠來做

工廠

Factory注解

如果使用@Factory注解,就比較簡單,而且方便擴展,示例如下。

不需改動原有類,添加一個新類PersonFactory

publicclass PersonFactory {

? ? @Factory

? ? public Object[] factory() {

? ? ? ? ArrayList testList =newArrayList<>();

? ? ? ? Person tp =newPerson("明明",10);

? ? ? ? testList.add(tp);

? ? ? ? Person tp2 =newPerson("宏哥",20);

? ? ? ? testList.add(tp2);

? ? ? ? Person tp3 =newPerson("劉創(chuàng)",50);

? ? ? ? testList.add(tp3);

? ? ? ? Person tp4 =newPerson("朱爺爺",70);

? ? ? ? testList.add(tp4);

? ? ? ? return testList.toArray();

? ? }

}

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">

<suite name="Suite">

? <test name="Test1">

? ? ? ? <classes>

? ? ? ? ? ? <class name="hongge.PersonFactory" />

? ? ? ? </classes>

? ? </test><!-- Test -->

</suite> <!-- Suite -->

使用@Factory的運行原理

1、如果不使用@Factory,運行普通的被@Test標(biāo)注的方法時,實際上是TestNG框架調(diào)用了該類的構(gòu)造函數(shù)構(gòu)造出一個對象,然后再執(zhí)行對象的這個方法。

2、使用了@Factory后,可以看到被@Factory標(biāo)注的方法返回了一個Object數(shù)組,數(shù)組中每一個元素是一個被測試類的對象。也就是說@Factory構(gòu)造了多個被測試類對象,然后把每一個對象都傳遞給了TestNG框架,然后TestNG框架在分別執(zhí)行這些對象中被@Test標(biāo)注的方法。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容