testNg測試框架搭建
設計背景
爲了方便sdk開發程序包的測試,搭建testNg測試框架,爲了能夠更加方便的編寫sdk的測試用例,以及提升測試的代碼覆蓋率。本期主要是完成sdk測試模塊,後續也會增加接口測試模塊。
設計思路:
主要設計師測試程序和測試用例的分離。
測試用例編寫在項目代碼中,使用testNg對應的註解完成測試用例;測試數據是存放在項目外,通過絕對地址獲取測試用例的數據。本項目主要支持csv,xml,json三種格式的測試用例的數據結構。
TestNg簡介
一、TestNg 介紹:
TestNg 優勢:
1、比Junit 涵蓋的功能更全面的測試框架
2、Junit 更適合隔離性比較強的單元測試
3、TestNg更適合複雜的集成測試
二、註解
BeforeMethod and AfterMethod
每個測試用例之前都會運行
BeforeClass and AfterClass
類之前運行的方法
BeforeSuit and AfterSuit
測試套件 在 BeforeClass 之前運行
運行順序:
BeforeSuit–BeforeClass–BeforeMethod-- case
三、異常測試
什麼時候會用到異常測試?
在我們期望結果爲某一個異常的時候 就要用到異常測試
比如:我們傳入了某些不合法的參數 ,程序會拋出異常,也就是說預期結果就是一個異常
resources : 是一個放配置文件的文件夾
四:忽略測試
某些不需要執行的測試 可以忽略 不執行 可以加屬性:Test(enable = false)
五:超時測試
某些測試如果響應的時間超過多少秒 就拋出超時的異常 可以加屬性 Test(timeout = 3000) 單位是毫秒
Thread.sleep(mills = 3000) 線程休眠
六、依賴測試
某一個方法執行要依賴前一個方法的執行 就叫依賴
Test(dependsOnMethods = {‘被依賴的方法名’})
被依賴的方法報錯了,需要依賴的方法就會被忽略掉
七、參數化測試:
1、xml 文件參數化
2、DateProvider 參數化
testNg搭建項目過程
首先,在pom.xml中添加testng和reportng相關依賴
<dependencies>
<!-- 添加testNG依賴 -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.11</version>
<!-- <scope>test</scope>-->
</dependency>
<!-- 添加reportNG依賴 -->
<dependency>
<groupId>org.uncommons</groupId>
<artifactId>reportng</artifactId>
<version>1.1.2</version>
<scope>test</scope>
<!-- 排除testNG依賴 -->
<exclusions>
<exclusion>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>3.0</version>
<scope>test</scope>
</dependency>
</dependencies>
pom.xml中配置maven-surefire-plugin並加入reportng listener
<properties>
<xmlFileName>testng.xml</xmlFileName>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19</version>
<configuration>
<skipTests>false</skipTests>
<suiteXmlFiles>
<suiteXmlFile>${xmlFileName}</suiteXmlFile>
</suiteXmlFiles>
<properties>
<property>
<name>usedefaultlisteners</name>
<value>false</value>
</property>
<property>
<name>listener</name>
<value>org.uncommons.reportng.HTMLReporter,org.uncommons.reportng.JUnitXMLReporter</value>
</property>
</properties>
<forkMode>always</forkMode>
</configuration>
</plugin>
</plugins>
</build>
在testng.xml中標籤加入listener
<listeners>
<listener class-name="org.uncommons.reportng.HTMLReporter"/>
<listener class-name="org.uncommons.reportng.JUnitXMLReporter"/>
</listeners>
在Idea中打開Run-Edit Configurations…
在Listeners標籤下勾選“Use default reporters”
最後運行testng.xml,自動生成test-output目錄,在html目錄下找到index.html
使用demo
說明:執行具體類適合調試具體的測試程序,執行多個測試程序生成對應的測試報告需要使用xml運行測試程序
一、csv測試用例
部分代碼:
@Test(dataProvider = "dealsQueryData")
public void getDealsQuery(DealsQuery dealsQuery) {
System.out.println("getDealsQuery");
System.out.println(JSONObject.toJSONString(dealsQuery));
}
@DataProvider(name = "dealsQueryData")
public Object[] ProviderData() {
String path = "E:\\csvtest.csv";
List<DealsQueryModel> dealsQueryModels = ToJavaBeanUtil.toJavaBeans(path, DealsQueryModel.class, DataType.CSV);
List<DealsQuery> dealsQueries = dealsQueryModels.stream().map(p -> {
DealsQuery dealsQuery = converter(p);
return dealsQuery;
}).collect(Collectors.toList());
return dealsQueries.toArray();
}
通過絕對路徑獲取測試用例的數據,傳入到具體的測試方法,進入到具體的測試程序執行測試方法。
二、Xml測試用例
@Test(dataProvider = "dealsQueryData")
public void getDealsQuery(DealsQuery dealsQuery) {
System.out.println("getDealsQuery");
System.out.println(JSONObject.toJSONString(dealsQuery));
}
@DataProvider(name = "dealsQueryData")
public Object[] ProviderData() {
String path = "E:\\xmltest2.xml";
DealsQueryXmlModels dealsQueryXmlModels =ToJavaBeanUtil.toJavaBean(path, DealsQueryXmlModels.class,DataType.XML);
List<DealsQuery> dealsQueries = dealsQueryXmlModels.getDealsQueryXmlModel().stream().map(p -> {
DealsQuery dealsQuery = converter(p);
return dealsQuery;
}).collect(Collectors.toList());
return dealsQueries.toArray();
}
三、Json測試用例
@Test(dataProvider = "dealsQueryData")
public void getDealsQuery(DealsQuery dealsQuery) {
System.out.println("getDealsQuery");
System.out.println(JSONObject.toJSONString(dealsQuery));
}
@DataProvider(name = "dealsQueryData")
public Object[] ProviderData() {
String path = "E:\\jsontest.json";
List<DealsQueryJsonModel> dealsQueryXmlModelList = ToJavaBeanUtil.toJavaBeans(path, DealsQueryJsonModel.class, DataType.JSON);
List<DealsQuery> dealsQueries = dealsQueryXmlModelList.stream().map(p -> {
DealsQuery dealsQuery = converter(p);
return dealsQuery;
}).collect(Collectors.toList());
return dealsQueries.toArray();
}
四、測試報告
1.測試單個類
測試報告
2.測試多個類
測試報告
備註
1.解決不同用例之間的依賴關係
通過testNg自帶的註解可一解決用例之間的依賴問題
2.多線程併發的問題
不同dataprovider的併發:
默認情況下,dp並行執行的線程池容量爲10,如果要更改併發的數量,也可以在suite tag下指定參數data-provider-thread-count:
同一個方法的併發:
@Test(enabled=true, dataProvider=“testdp”, threadPoolSize=5, invocationCount=10)
其中threadPoolSize表明用於調用該方法的線程池容量,該例就是同時起5個線程並行執行該方法;invocationCount表示該方法總計需要被執行的次數。該例子中5個線程同時執行,當總計執行次數達到10次時,停止。
項目git地址:https://github.com/why154285/testNgtest.git