亚洲好骚综合-亚洲黄色录像-亚洲黄色网址-亚洲黄色网址大全-99久久99久久-99久久99久久精品国产

您的位置:軟件測試 > 開源軟件測試 > 開源單元測試工具 > TestNG
TestNG的@Factory及其與@DataProvider的區別
作者:網絡轉載 發布時間:[ 2016/6/28 11:45:22 ] 推薦標簽:單元測試工具 軟件測試

  Factory,顧名思意是工廠,也是工廠方法,在軟件開發中一般結合多態使用,用來根據不同的條件創建不同的類對象。
  在這里,Factory一般用來創建一個測試類的多個實例,每個實例屬性不同,以執行不同的測試,Factory構造實例的方法必須返回Object[],也是一組測試類的實例。
  以testng官網的例子來說明,測試類如下,在測試用例testServer中,訪問m_numberOfTimes次web頁面,這里打印出了訪問的次數和執行該用例的實例地址。
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 }
  構造測試類WebTest實例的工廠如下,該工程創建了5個WebTest實例:
  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文件中指定測試類為WebTestFactory,即可運行測試,注意:只需指定工廠類即可,無需再指定測試類。
  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>
  運行結果如下,可見總共執行了5個用例,每個用例訪問web頁面的次數不同。
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的使用也比較簡單,思考一下,在上面的例子中一個用例運行了5次,每次訪問頁面的次數不同,那么用DataProvider能否實現呢?
  采用DataProvider實現的代碼如下,這里通過DataProvider來提供每個用例訪問web頁面的次數,一共5個參數,用例會執行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 }
  執行結果如下:
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)
  可以看到,訪問web頁面的次數來說,這里結果與Factory方法沒有什么不同,那這兩者有什么區別呢?
  DataProvider:為測試用例提供參數,有多少組參數會執行多少次用例,因此它是讓一個測試類實例的某個方法執行多次,但每次執行都是采用的同一個實例(從上面結果的實例地址可以看出)。
  Factory:創建一個測試類的多個實例,每個實例中的所有測試用例都會被執行,因此它是讓一個測試類被執行多次,每次執行采用的是不同實例。

上一頁12下一頁
軟件測試工具 | 聯系我們 | 投訴建議 | 誠聘英才 | 申請使用列表 | 網站地圖
滬ICP備07036474 2003-2017 版權所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd
主站蜘蛛池模板: 国产综合视频 | 成人动漫在线视频 | 成人精品视频一区二区三区尤物 | 久久久久免费精品国产 | 免费精品视频在线 | a在线观看欧美在线观看 | 性欧美高清另类videoso | 2015小明永久领域日韩 | 欧美成人免费sss | 日本成日本片人免费 | 欧美又黄又嫩大片a级 | 小明明看看视频永久免费网 | 日韩视频高清免费看 | 成人欧美精品一区二区不卡 | 亚洲成人网在线 | 黄毛片网站 | 国产一级视频免费 | 男女做污污无遮挡激烈免费 | 狠狠色婷婷丁香六月 | 成人午夜免费视频毛片 | 黄色一级片免费看 | 欧美日韩亚洲m码色帝国 | 日韩国产欧美精品综合二区 | 草草视频在线免费观看 | 亚洲图片欧洲图片aⅴ | 嫩草影院永久一二三入口 | 国产黄色福利 | 欧美成人综合在线 | 免费黄色小视频在线观看 | 亚洲精品高清国产一线久久97 | 欧美日本一二三区 | 九九视频在线 | 中文字幕欧美一区 | 欧美手机在线视频 | 日韩日日操| 亚洲欧美在线综合一区二区三区 | 大片毛片女女女女女女女 | 久久久青草青青国产亚洲免观 | 在线精品国产第一页 | 欧美亚洲桃花综合 | 免费人成在线观看 |