TestNG測試分組
配置@Test
可以通過@Test給多個測試類的測試用例分組,比如某個測試用例既屬於smoke又屬於uat,那麼就可以配置如下:
@Test(groups = {"smoke", "uat"})
public void testDemo1() throws InterruptedException{
System.out.print("testDemo1\n");
}
@Test(groups = {"smoke", "func"})
public void testDemo2() throws InterruptedException{
System.out.print("testDemo2\n");
}
通過testng.xml進行測試用例的運行,運行結果如下:
- 運行
smoke
類型的測試用例
testDemo1
testDemo2
===============================================
Suite1
Total tests run: 2, Failures: 0, Skips: 0
===============================================
從上面的結果可以看出,實際這裏testDemo1和testDemo2都運行了。
- 修改testng.xml,運行uat類型的測試用例
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite1" verbose="1" >
<test name="Regression1">
<groups>
<run>
<include name="uat" /> <!-- 修改這裏-->
</run>
</groups>
<!--<classes>-->
<!--<class name="com.wilmar.loms.testcase.ManualOrderTest"/>-->
<!--</classes>-->
<classes>
<class name="com.nitb.demo.Demo1"/>
</classes>
</test>
</suite>
運行結果:
testDemo1
===============================================
Suite1
Total tests run: 1, Failures: 0, Skips: 0
===============================================
從上面的結果可以看出,本次只運行了配置成uat的testDemo。
- 執行多個類型的測試用例,包括uat和func
<groups>
<run>
<include name="uat" />
<include name="func" />
</run>
</groups>
把上訴testng.xml中的內容修改成如下,然後執行,運行結果:
testDemo1
testDemo2
===============================================
Suite1
Total tests run: 2, Failures: 0, Skips: 0
===============================================
從輸出結果看,運行了分別屬於uat組的testDemo1和屬於func組的testDemo2。
TestNG捕獲異常
如果測試用例期望的結果是拋出異常,可以通過@Test把異常指派給某個方法,此時測試用例對應拋出異常,結果爲PASS;否則,判定爲FAIL。
@Test(expectedExceptions = { ArithmeticException.class })
TestNG設置參數
在testng.xml中設置參數有兩種方式:
- 一種是通過@Parameters設置,但是它只能傳字符串數據。
- 一種是通過@DataProvider設置,它可以傳複雜類型的數據。
@Parameter設置
添加測試用例:
@Parameters({"username"})
@Test(groups = {"smoke", "func"})
public void testDemo3(String username) throws InterruptedException{
System.out.print("testDemo3, " + "username=" + username);
}
配置testng.xml,增加參數:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite1" verbose="1" >
<test name="Regression1">
<parameter name="username" value="testname" /> <!-- 添加參數 -->
<groups>
<run>
<include name="uat" />
<include name="func" />
</run>
</groups>
<!--<classes>-->
<!--<class name="com.wilmar.loms.testcase.ManualOrderTest"/>-->
<!--</classes>-->
<classes>
<class name="com.nitb.demo.Demo1"/>
</classes>
</test>
</suite>
運行結果:
testDemo1
testDemo2
testDemo3, username=testname
===============================================
Suite1
Total tests run: 3, Failures: 0, Skips: 0
===============================================
Process finished with exit code 0
從輸出結果看出,這裏多了username=testname的輸出,正好是我們期望的。
設置@DataProvider
添加測試代碼:
@DataProvider(name = "dataProvider")
public Object[][] dataProvider() {
return new Object[][] {{"data1"}, {"data2"}, {"data3"}};
}
@Test(groups = {"func"}, dataProvider = "dataProvider")
public void testDemo4(String data) {
System.out.print("testDemo4, " + "data=" + "\n");
}
這裏需要注意,如果@DataProvider中不設置name,那麼默認會使用註釋的方法名。因爲這裏設置的name和方法名都叫dataProvider,所以其實設置不設置name都無所謂。
執行結果:
testDemo1
testDemo2
testDemo3, username=testname
testDemo4, data=data1
testDemo4, data=data2
testDemo4, data=data3
===============================================
Suite1
Total tests run: 6, Failures: 0, Skips: 0
===============================================
從上面可以看到,這裏的testDemo被執行了三次,所以當同一個測試用例需要執行多組數據的時候,使用@DataProvider配置參數就會很簡便。