@Factory注解從字面意思上來講就是采用工廠的方法來創建測試數據並配合完成測試,其主要應對的場景是:對於某一個測試用例或方法,我們需要輸入多個測試數據進行測試,並且這些測試數據可以是有一定關系(可以通過代碼控制),此時,我們就可以把自動化或者手動測試時的遇到的只因測試數據不同的多個測試用例合並成一個測試用例,來進行更方便和快捷的測試。
策略:一般我們會在標有@Factory注解的方法中對測試類進行調用,這時TestNg會自動調用測試類中帶有@Test注解的方法
配置文件:只需要配置帶有@Factory注解的類即可
@Factory必須放在一個返回對象數組的頂部,所有的這些對象都包含測試類的實例,testng會確保@Factory只被調用一次。
@Factory方法是首先被調用的,在@Test方法和配置方法之前,只有當所有的@Factory方法被調用之后,testng才開始執行配置和測試方法。
@Factory允許在運行時動態測試。
簡單的使用:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
public class SimpleTest {
@Test
public void simpleTest1(){
System.out.println("simple test one");
}
@Test
public void simpleTest2(){
System.out.println("simple test two");
}
}
public class SimpleTestFactory
{
@Factory
public Object[] factoryMethod() {
return new Object[] { new SimpleTest(), new SimpleTest() };
}
}
|
SimpleTestFactory工廠類,在帶有@Factory注解的方法中調用被執行的測試類,TestNg會自動調用被執行類中帶有@Test注解的方法被執行的測試類為:SimpleTestFactory。
輸出結果會:
simple test one
simple test one
simple test two
simple test two
PASSED: simpleTest1
PASSED: simpleTest1
PASSED: simpleTest2
PASSED: simpleTest2
===============================================
Default test
Tests run: 4, Failures: 0, Skips: 0
===============================================
由以上可知所有的test方法都被調用了。
使用@Factory最大的好處就是可以在初始化的時候將參數傳給測試類:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
public class SimpleTest {
private int para;
public SimpleTest(int para) {
this.para = para;
}
@Test
public void testMethodOne(){
int value = para + 1;
System.out.println("Test method one output: " + value);
}
@Test
public void testMethodTwo(){
int value = para + 2;
System.out.println("Test method two output: " + value);
}
}
public class SimpleTestFactory {
@Factory
public Object[] factoryMethod(){
return new Object[] { new SimpleTest(0), new SimpleTest(10)};
}
}
|
運行SimpleTestFactory,可以得到以下輸出:
Test method one output: 1
Test method one output: 11
Test method two output: 2
Test method two output: 12
PASSED: testMethodOne
PASSED: testMethodOne
PASSED: testMethodTwo
PASSED: testMethodTwo
可以知道測試中的每個方法都執行了兩遍。
@Factory更適合於同一類型的參數變化性的測試,那么如果參數值沒有特定的規律時,我們可以采用@Factory和@DataProvider相結合的方式進行測試
注意要點:測試方法將被一共執行的次數,因為@Factory本身就屬於循環測試的類型,@DataProvider也是屬於測試整體循環的類型
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
public class DataProviderTest
{
private int param;
@Factory(dataProvider = "dataMethod")
public DataProviderTest(int param) {
this.param = param;
}
@DataProvider
public static Object[][] dataMethod() {
return new Object[][] { new Object[]{ 0 }, new Object[]{ 10 } };
}
@Test
public void testMethodOne() {
int opValue = param + 1;
System.out.println("Test method one output: " + opValue);
}
@Test
public void testMethodTwo() {
int opValue = param + 2;
System.out.println("Test method two output: " + opValue);
}
}
|
dataMethod會返回一個二維數組,維數表示迭代的次數,第二個值表示傳入的參數。
使用@Factory的依賴測試,在會先執行所有的依賴方法,然后在執行測試方法。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
public class DependencyTest
{
private int param;
public DependencyTest(int param) {
this.param = param;
}
@Test(dependsOnMethods = { "testMethodTwo" })
public void testMethodOne() {
System.out.println("Test method one with param values: " + this.param);
}
@Test
public void testMethodTwo() {
System.out.println("Test method two with param values: " + this.param);
}
}
public class SimpleTestFactory
{
@Factory
public Object[] factoryMethod()
{
return new Object[] { new DependencyTest(1), new DependencyTest(2) };
}
}
|
結果:
Test method two with param values: 2
Test method two with param values: 1
Test method one with param values: 2
Test method one with param values: 1
PASSED: testMethodTwo
PASSED: testMethodTwo
PASSED: testMethodOne
PASSED: testMethodOne
