您的位置:軟件測(cè)試 > 開(kāi)源軟件測(cè)試 > 開(kāi)源單元測(cè)試工具 > TestNG
TestNG的@Factory及其與@DataProvider的區(qū)別
作者:網(wǎng)絡(luò)轉(zhuǎn)載 發(fā)布時(shí)間:[ 2016/6/28 11:45:22 ] 推薦標(biāo)簽:單元測(cè)試工具 軟件測(cè)試

  Factory,顧名思意是工廠,也是工廠方法,在軟件開(kāi)發(fā)中一般結(jié)合多態(tài)使用,用來(lái)根據(jù)不同的條件創(chuàng)建不同的類對(duì)象。
  在這里,F(xiàn)actory一般用來(lái)創(chuàng)建一個(gè)測(cè)試類的多個(gè)實(shí)例,每個(gè)實(shí)例屬性不同,以執(zhí)行不同的測(cè)試,F(xiàn)actory構(gòu)造實(shí)例的方法必須返回Object[],也是一組測(cè)試類的實(shí)例。
  以testng官網(wǎng)的例子來(lái)說(shuō)明,測(cè)試類如下,在測(cè)試用例testServer中,訪問(wèn)m_numberOfTimes次web頁(yè)面,這里打印出了訪問(wèn)的次數(shù)和執(zhí)行該用例的實(shí)例地址。
1 public class WebTest {
2   private int m_numberOfTimes;
3   public WebTest(int numberOfTimes) {
4     m_numberOfTimes = numberOfTimes;
5   }
6
7   @Test
8   public void testServer() {
9    for (int i = 0; i < m_numberOfTimes; i++) {
10      // access the web page
11      System.out.println("Access the web page, times " + i + ", the instance is " + this);
12     }
13   }
14 }
  構(gòu)造測(cè)試類WebTest實(shí)例的工廠如下,該工程創(chuàng)建了5個(gè)WebTest實(shí)例:
  1 public class WebTestFactory {
  2   @Factory
  3   public Object[] createInstances() {
  4       Object[] result = new Object[5];
  5       for (int i = 0; i < 5; i++) {
  6           result[i] = new WebTest(i+1);
  7     }
  8     return result;
  9   }
  10 }
  然后在XML文件中指定測(cè)試類為WebTestFactory,即可運(yùn)行測(cè)試,注意:只需指定工廠類即可,無(wú)需再指定測(cè)試類。
  1 <suite name="suite1">
  2
  3     <test name="test1" verbose="2">
  4         <classes>
  5             <class name="sea.WebTestFactory" />
  6           </classes>
  7     </test>
  8
  9 </suite>
  運(yùn)行結(jié)果如下,可見(jiàn)總共執(zhí)行了5個(gè)用例,每個(gè)用例訪問(wèn)web頁(yè)面的次數(shù)不同。
1 Access the web page, times 0, the instance is sea.WebTest@1593948d
2 Access the web page, times 1, the instance is sea.WebTest@1593948d
3 Access the web page, times 2, the instance is sea.WebTest@1593948d
4
5 Access the web page, times 0, the instance is sea.WebTest@1b604f19
6 Access the web page, times 1, the instance is sea.WebTest@1b604f19
7 Access the web page, times 2, the instance is sea.WebTest@1b604f19
8 Access the web page, times 3, the instance is sea.WebTest@1b604f19
9
10 Access the web page, times 0, the instance is sea.WebTest@482f8f11
11 Access the web page, times 1, the instance is sea.WebTest@482f8f11
12
13 Access the web page, times 0, the instance is sea.WebTest@51565ec2
14
15 Access the web page, times 0, the instance is sea.WebTest@7823a2f9
16 Access the web page, times 1, the instance is sea.WebTest@7823a2f9
17 Access the web page, times 2, the instance is sea.WebTest@7823a2f9
18 Access the web page, times 3, the instance is sea.WebTest@7823a2f9
19 Access the web page, times 4, the instance is sea.WebTest@7823a2f9
20
21 PASSED: testServer
22 PASSED: testServer
23 PASSED: testServer
24 PASSED: testServer
25 PASSED: testServer
  可以看到Factory的使用也比較簡(jiǎn)單,思考一下,在上面的例子中一個(gè)用例運(yùn)行了5次,每次訪問(wèn)頁(yè)面的次數(shù)不同,那么用DataProvider能否實(shí)現(xiàn)呢?
  采用DataProvider實(shí)現(xiàn)的代碼如下,這里通過(guò)DataProvider來(lái)提供每個(gè)用例訪問(wèn)web頁(yè)面的次數(shù),一共5個(gè)參數(shù),用例會(huì)執(zhí)行5次:
1 public class Test1 {
2
3     @DataProvider(name = "data1")
4     public Object[][] createdata() {
5         return new Object[][] {
6             {1},
7             {2},
8             {3},
9             {4},
10             {5}
11         };
12     }
13
14     @Test(dataProvider = "data1")
15     public void testServer(int m_numberOfTimes) {
16         for (int i = 0; i < m_numberOfTimes; i++) {
17         // access the web page
18             System.out.println("Access the web page, times " + i + ", the instance is " + this);
19         }
20     }
21
22 }
  執(zhí)行結(jié)果如下:
Access the web page, times 0, the instance is sea.Test1@58651fd0
Access the web page, times 0, the instance is sea.Test1@58651fd0
Access the web page, times 1, the instance is sea.Test1@58651fd0
Access the web page, times 0, the instance is sea.Test1@58651fd0
Access the web page, times 1, the instance is sea.Test1@58651fd0
Access the web page, times 2, the instance is sea.Test1@58651fd0
Access the web page, times 0, the instance is sea.Test1@58651fd0
Access the web page, times 1, the instance is sea.Test1@58651fd0
Access the web page, times 2, the instance is sea.Test1@58651fd0
Access the web page, times 3, the instance is sea.Test1@58651fd0
Access the web page, times 0, the instance is sea.Test1@58651fd0
Access the web page, times 1, the instance is sea.Test1@58651fd0
Access the web page, times 2, the instance is sea.Test1@58651fd0
Access the web page, times 3, the instance is sea.Test1@58651fd0
Access the web page, times 4, the instance is sea.Test1@58651fd0
PASSED: testServer(1)
PASSED: testServer(2)
PASSED: testServer(3)
PASSED: testServer(4)
PASSED: testServer(5)
  可以看到,訪問(wèn)web頁(yè)面的次數(shù)來(lái)說(shuō),這里結(jié)果與Factory方法沒(méi)有什么不同,那這兩者有什么區(qū)別呢?
  DataProvider:為測(cè)試用例提供參數(shù),有多少組參數(shù)會(huì)執(zhí)行多少次用例,因此它是讓一個(gè)測(cè)試類實(shí)例的某個(gè)方法執(zhí)行多次,但每次執(zhí)行都是采用的同一個(gè)實(shí)例(從上面結(jié)果的實(shí)例地址可以看出)。
  Factory:創(chuàng)建一個(gè)測(cè)試類的多個(gè)實(shí)例,每個(gè)實(shí)例中的所有測(cè)試用例都會(huì)被執(zhí)行,因此它是讓一個(gè)測(cè)試類被執(zhí)行多次,每次執(zhí)行采用的是不同實(shí)例。

上一頁(yè)12下一頁(yè)
軟件測(cè)試工具 | 聯(lián)系我們 | 投訴建議 | 誠(chéng)聘英才 | 申請(qǐng)使用列表 | 網(wǎng)站地圖
滬ICP備07036474 2003-2017 版權(quán)所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd