java接口测试框架搭建_接口自动化测试框架搭建
一、原理及特點(diǎn)
參數(shù)放在XML文件中進(jìn)行管理
用httpClient簡單封裝一個(gè)httpUtils工具類
測試用例管理使用了testNg管理,使用了TestNG參數(shù)化測試,通過xml文件來執(zhí)行case。
測試報(bào)告這里用到第三方的包ReportNG 項(xiàng)目組織用Maven
二、準(zhǔn)備
使用工具:eclipse,maven
用到的第三方j(luò)ar包:dom4j、reportng、testng
理解難點(diǎn):httpUtils和xmlUtil工具類的封裝;dom4j使用;CookieStore的應(yīng)用
三、框架構(gòu)思
1、項(xiàng)目結(jié)構(gòu)
2、用例執(zhí)行流程
3、接口調(diào)用流程
4、調(diào)度腳本流程
四、框架實(shí)現(xiàn)
1、輸入?yún)?shù)
1.1 參數(shù)放在XML文件中進(jìn)行管理
例:這里測試獲取角色的接口輸入?yún)?shù)為,page和rows,mapRole.xml內(nèi)容如下
1.2 封裝一個(gè)xmlUtil工具類負(fù)責(zé)讀取XML,使用第三方的jar包dom4j
1.2.1 xmlUtil中readXMLDocument方法返回值為HashMap
public static HashMap ?readXMLDocument(String beanName,String xmlName){
}
參數(shù)xmlName(xml文件的名字); 參數(shù)beanName(xml文件中節(jié)點(diǎn)的名稱);
1.3 封裝一個(gè)CookieUtil工具類,通過CookieStore儲存cookie
1.3.1 CookieUtil類中setCookieStore方法返回值為CookieStore
public ?CookieStore setCookieStore(HttpResponse httpResponse) {
}
1.4 用httpClient簡單封裝一個(gè)httpUtils工具類有g(shù)et.post,put,delete方法
1.4.1 httpUtils中post封裝方法如下:
public CloseableHttpResponse post(String url, Map params,CloseableHttpClient httpclient,CookieStore cookieStore){
}
2、返回參數(shù)
2.1 創(chuàng)建一個(gè)接口返回對象ResponseBean,
對象ResponseBean,包括status、statusCode、contentType、body、url、method、cookies
2.2 在工具類中在創(chuàng)建一個(gè)ReponseUtil工具類
ReponseUtil工具類負(fù)責(zé)將請求的返回?cái)?shù)據(jù)CloseableHttpResponse 轉(zhuǎn)換成ResponseBean
public ResponseBean setResponseBean(CloseableHttpResponse httpResponse) {
}
3、測試用例
測試用例管理使用了testNg管理 ,使用了TestNG參數(shù)化測試,通過xml文件來執(zhí)行case
3.1 測試case腳本
public class GetRoleTest {
static CookieStore cookieStore ;
static CookieUtil cookieUtil=new CookieUtil() ;
CloseableHttpClient client;
HttpUtils httpUtils=HttpUtils.getInstance();
@Parameters({ "url", "objBean" ,"statusCode","xmlName"})
@BeforeSuite
/*
* 登錄進(jìn)入系統(tǒng)獲取JSESSIONID放入到CookieStore中
* */
public ?void TestLoginIn(String url ,String objBean, String statusCode,String xmlName) {
Map params=xmlUtil.readXMLDocument(objBean,xmlName);
client = HttpClients.createDefault();
CloseableHttpResponse httpResponse= httpUtils.post(url, params, client, cookieStore);
//cookieUtil.printResponse(httpResponse);
cookieStore=cookieUtil.setCookieStore(httpResponse);
}
@Parameters({ "url", "objBean" ,"statusCode","body","xmlName"})
@Test(priority = 2)
public ?void TestGetRole(String url ,String objBean, String statusCode,String body,String xmlName) {
Map params=xmlUtil.readXMLDocument(objBean,xmlName);
client = HttpClients.custom().setDefaultCookieStore(cookieStore).build();
CloseableHttpResponse httpResponse= httpUtils.post(url, params, client, cookieStore);
ResponseBean rb=new ReponseUtil().setResponseBean(httpResponse);
// ? ? ? ?add Assert
Assert.assertEquals("OK", rb.getStatus());
Assert.assertEquals(statusCode, rb.getStatusCode());
Assert.assertEquals(true, rb.getBody().contains(body));
}
@AfterSuite
public void closeClient(){
try {
// 關(guān)閉流并釋放資源
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
[注] 因?yàn)锳PI接口測試時(shí)每次都要校驗(yàn)Cookie,所有我們每次都先執(zhí)行登錄操作去獲取Cookie
3.2 xml文件的編寫
右鍵->run as ->TestNG Suite,這個(gè)場景的的測試用例就可以運(yùn)行了
4、測試報(bào)告和項(xiàng)目組織
測試報(bào)告這里用到第三方的包ReportNG 項(xiàng)目組織用Maven
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
..........................................
..........................................
..........................................
UTF-8
TestGetRole.xml
.................這里寫testNG對應(yīng)的XML名稱----------------------
TestGetUser.xml
..........................
org.apache.maven.plugins
maven-surefire-plugin
2.19
src/test/java/testSuites/${xmlFileName}
.................略............
..............這里的和properties中的xmlFileName想對應(yīng)............
src/test/java/testSuites/${xmlFileName10}
org.apache.maven.plugins
maven-surefire-plugin
2.5
usedefaultlisteners
false
listener
org.uncommons.reportng.HTMLReporter
target/
maven-compiler-plugin
3.5.1
1.8
1.8
[注] 因?yàn)槭莔aven的項(xiàng)目所以要將testSuite的xml文件放在maven的test目錄下,這樣右鍵pom.xml文件maven test,所有的測試用例就開始執(zhí)行了
測試報(bào)告
框架目前存在的不足
1、數(shù)據(jù)庫數(shù)據(jù)校驗(yàn)這一塊的功能還沒有完善,計(jì)劃用MyBatis
2、參數(shù)使用了xml文件配置雖然靈活但有些繁瑣,目前還沒想到好的解決方案,testlink是否可以嘗試一下呢
總結(jié)
以上是生活随笔為你收集整理的java接口测试框架搭建_接口自动化测试框架搭建的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 我用手机喇叭吹灭了一根蜡烛
- 下一篇: 小米手环7 Pro上架:小米史上最强