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