Java接口自動化之TestNG單元測試框架(一)


本文3704字
閱讀約需10分鐘
上一篇Java接口自動化系列文章:Java接口自動化之log4j日志框架,主要介紹log4j日志介紹、日志三大組成部分及日志實戰(zhàn)。
以下主要介紹TestNG的簡介、@Test注解及其屬性。
1 TestNG介紹
2 TestNG特點
支持注解;
靈活的運行配置;
支持多線程、忽略、異常、參數(shù)化等測試。
3 加入TestNG依賴
在maven項目的pom.xml中,添加內(nèi)容如下:
????org.testng
????testng
????7.0.0
@Test是TestNG 最基本的注解,用來將方法標注為測試方法。接下來會介紹@Test注解的常用屬性。
1 enable 測試方法是否執(zhí)行
enable默認是?true?, 表示執(zhí)行這個方法,如果設置為?false?,則在運行時不會執(zhí)行這個測試方法。
import?org.testng.annotations.Test;
public?class?TestDemo?{
????@Test(enabled?=?true)
????public??void?testDemo1(){
????????System.out.println("這是testDemo1");
????}
????@Test(enabled?=?false)
????public??void?testDemo2(){
????????System.out.println("這是testDemo2");
????}
}
運行結果為:

從上圖可以看出,enabled為true的方法執(zhí)行了,enabled為false的方法未執(zhí)行。
2 dependsOnMethods 依賴方法
在依賴的方法運行完成之后運行當前方法,如果依賴方法測試不通過,那么當前方法也不會繼續(xù)運行了。
依賴的方法可以有多個,格式為:@Test(dependsOnMethods = { "method1" , “method2” })。
修改testDemo1代碼,運行時拋出異常,代碼如下:
import?org.testng.annotations.Test;
public?class?TestDemo?{
????@Test()
????public??void?testDemo1(){
????????int?i=10;
????????System.out.println(i/0);
????????System.out.println("這是testDemo1");
????}
????@Test(dependsOnMethods?=?{"testDemo1"})
????public??void?testDemo2(){
????????System.out.println("這是testDemo2");
????}
}
運行結果為:

從上圖可以看出testDemo1方法運行失敗后,testDemo2方法被忽略運行了。
3 groups 分組
在運行時,一個組的方法會一起運行,然后再運行下一個組的方法;
格式為:@Test(groups = "groupName")。
import?org.testng.annotations.Test;
import?org.testng.annotations.BeforeGroups;
import?org.testng.annotations.AfterGroups;
public?class?TestDemo?{
????@BeforeGroups(groups?=?"server")
????public?void?beforeGroupsOnServer()?{
????????System.out.println("服務端組運行前執(zhí)行的方法。。。");
????}
????@BeforeGroups(groups?=?"client")
????public?void?beforeGroupsOnClient()?{
????????System.out.println("客戶端組運行前執(zhí)行的方法。。。");
????}
????@Test(groups?=?"server")
????public?void?test1()?{
????????System.out.println("server?test1?run?....");
????}
????@Test(groups?=?"server")
????public?void?test2()?{
????????System.out.println("server?test2?run?....");
????}
????@Test(groups?=?"client")
????public?void?test3()?{
????????System.out.println("client?test3?run?....");
????}
????@Test(groups?=?"client")
????public?void?test4()?{
????????System.out.println("client?test4?run?....");
????}
????@AfterGroups(groups?=?"server")
????public?void?afterGroupsOnServer()?{
????????System.out.println("服務端組運行之后執(zhí)行的方法。。。");
????}
????@AfterGroups(groups?=?"client")
????public?void?afterGroupsOnClient()?{
????????System.out.println("客戶端組運行之后執(zhí)行的方法。。。");
????}
}
運行結果為:
服務端組運行前執(zhí)行的方法。。。
server?test1?run?....
server?test2?run?....
服務端組運行之后執(zhí)行的方法。。。
客戶端組運行前執(zhí)行的方法。。。
client?test3?run?....
client?test4?run?....
客戶端組運行之后執(zhí)行的方法。。。
===============================================
Default?Suite
Total?tests?run:?4,?Failures:?0,?Skips:?0
4?timeOut 超時屬性
格式:@Test(timeOut = 3000) 設置超時時間,單位為毫秒。
import?org.testng.annotations.Test;
public?class?TestDemo?{
????//?單位為毫秒值,3秒內(nèi)沒有響應,就運行失敗,反之成功
????@Test(timeOut?=?3000)
????public?void?testSuccess()?throws?InterruptedException?{
????????Thread.sleep(2000);
????}
????//?單位為毫秒值,2秒內(nèi)沒有響應,就運行失敗,反之成功
????@Test(timeOut?=?2000)
????public?void?testFail()?throws?InterruptedException?{
????????Thread.sleep(3000);
????}
}
運行結果為:

從上圖可以看出,testFail方法因為超時運行失敗。
5 多線程測試
格式:@Test(invocationCount = 10,threadPoolSize = 3)。
參數(shù)說明:
invocationCount:線程調(diào)用的次數(shù),默認1次。
threadPoolSize:線程池,需與invocationCount組合使用。
接下來編寫一個方法,定義3個線程,執(zhí)行方法10次。
import?org.testng.annotations.Test;
public?class?TestDemo?{
????@Test(invocationCount?=?10,threadPoolSize?=?3)
????public?void?testDemo(){
????????System.out.println("---------");
????????System.out.printf("Thread?Id?:?%s%n",Thread.currentThread().getId());
????}
}
運行結果為:
[TestNG]?Running:
?
---------
---------
Thread?Id?:?13
---------
Thread?Id?:?12
Thread?Id?:?14
---------
Thread?Id?:?13
---------
Thread?Id?:?14
---------
Thread?Id?:?12
????
---------
Thread?Id?:?12
---------
Thread?Id?:?13
---------
Thread?Id?:?14
---------
Thread?Id?:?12
===============================================
Default?Suite
Total?tests?run:?10,?Failures:?0,?Skips:?0
===============================================
(完)

喜歡記得星標置頂,讓我們一起守護成長
