如果遇到什么問題建議觀看下面視頻:
【敢稱全站第一】B站最全的Python自動化測試深度學(xué)習(xí)教程!學(xué)完即就業(yè),小白也能信手拈來!幫你少走99%的彎路~
一、原理及特點
參數(shù)放在XML文件中進行管理
用httpClient簡單封裝一個httpUtils工具類
測試用例管理使用了testNg管理,使用了TestNG參數(shù)化測試,通過xml文件來執(zhí)行case。
測試報告這里用到第三方的包ReportNG 項目組織用Maven
二、準(zhǔn)備
使用工具:eclipse,maven
用到的第三方j(luò)ar包:dom4j、reportng、testng
理解難點:httpUtils和xmlUtil工具類的封裝;dom4j使用;CookieStore的應(yīng)用
三、框架構(gòu)思
1、項目結(jié)構(gòu)
2、用例執(zhí)行流程
?
3、接口調(diào)用流程
?
4、調(diào)度腳本流程
?
四、框架實現(xiàn)
1、輸入?yún)?shù)
1.1 參數(shù)放在XML文件中進行管理
例:這里測試獲取角色的接口輸入?yún)?shù)為,page和rows,mapRole.xml內(nèi)容如下
<?xml version="1.0" encoding="UTF-8"?>
<map>
<bean beanName="GetRole">
<!--Locator lists -->
<locator name="page" value="1"></locator>
<locator name="rows" value="10"></locator>
</bean>
</map>
1.2 封裝一個xmlUtil工具類負(fù)責(zé)讀取XML,使用第三方的jar包dom4j
1.2.1 xmlUtil中readXMLDocument方法返回值為HashMap<String, String>
public static HashMap<String, String> readXMLDocument(String beanName,String xmlName){
}
參數(shù)xmlName(xml文件的名字); 參數(shù)beanName(xml文件中節(jié)點的名稱);
1.3 封裝一個CookieUtil工具類,通過CookieStore儲存cookie
1.3.1 CookieUtil類中setCookieStore方法返回值為CookieStore
public CookieStore setCookieStore(HttpResponse httpResponse) {
}
1.4 用httpClient簡單封裝一個httpUtils工具類有g(shù)et.post,put,delete方法
1.4.1 httpUtils中post封裝方法如下:
public CloseableHttpResponse post(String url, Map<String, String> params,CloseableHttpClient httpclient,CookieStore cookieStore){
}
2、返回參數(shù)
2.1 創(chuàng)建一個接口返回對象ResponseBean,
對象ResponseBean,包括status、statusCode、contentType、body、url、method、cookies
2.2 在工具類中在創(chuàng)建一個ReponseUtil工具類
ReponseUtil工具類負(fù)責(zé)將請求的返回數(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
/*
* 登錄進入系統(tǒng)獲取JSESSIONID放入到CookieStore中
* */
public void TestLoginIn(String url ,String objBean, String statusCode,String xmlName) {
Map<String,String> 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<String,String> 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();
}
}
}
【注】?因為API接口測試時每次都要校驗Cookie,所有我們每次都先執(zhí)行登錄操作去獲取Cookie
3.2 xml文件的編寫
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="TestGetRole" parallel="classes" thread-count="5">
<parameter name="url" value="/sys/login" />
<parameter name="objBean" value="loginIn" />
<parameter name="status" value="OK" />
<parameter name="statusCode" value="200" />
<parameter name="xmlName" value="mapRole" />
<test name="TestGetRole" preserve-order="true">
<parameter name="url" value="/json/getRoleInfo" />
<parameter name="objBean" value="GetRole" />
<parameter name="status" value="OK" />
<parameter name="statusCode" value="200" />
<parameter name="body" value="roleName" />
<classes>
<class name="com.lc.testScript.GetRoleTest">
<methods>
<include name="TestGetRole" />
<!--<include name="TestGetRole2" />-->
</methods>
</class>
</classes>
</test>
</suite>
右鍵->run as ->TestNG Suite,這個場景的的測試用例就可以運行了
4、測試報告和項目組織
測試報告這里用到第三方的包ReportNG 項目組織用Maven
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
..........................................
..........................................
..........................................
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<xmlFileName1>TestGetRole.xml</xmlFileName>
.................這里寫testNG對應(yīng)的XML名稱----------------------
<xmlFileName10>TestGetUser.xml</xmlFileName>
</properties>
<dependencies>
..........................
</dependencies>
<build>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>src/test/java/testSuites/${xmlFileName}</suiteXmlFile>
.................略............
..............這里的和properties中的xmlFileName想對應(yīng)............
<suiteXmlFile>src/test/java/testSuites/${xmlFileName10}</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
<!-- 添加插件,添加ReportNg的監(jiān)聽器,修改最后的TestNg的報告 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.5</version>
<configuration>
<properties>
<property>
<name>usedefaultlisteners</name>
<value>false</value>
</property>
<property>
<name>listener</name>
<value>org.uncommons.reportng.HTMLReporter</value>
</property>
</properties>
<workingDirectory>target/</workingDirectory>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
【注】因為是maven的項目所以要將testSuite的xml文件放在maven的test目錄下,這樣右鍵pom.xml文件maven test,所有的測試用例就開始執(zhí)行了
測試報告
框架目前存在的不足
1、數(shù)據(jù)庫數(shù)據(jù)校驗這一塊的功能還沒有完善,計劃用MyBatis
2、參數(shù)使用了xml文件配置雖然靈活但有些繁瑣,目前還沒想到好的解決方案,testlink是否可以嘗試一下呢
最后允許我對你們說一段話:
如果你也在往自動化測試開發(fā)方向發(fā)展
在適當(dāng)?shù)哪挲g,選擇適當(dāng)?shù)膷徫?,盡量去發(fā)揮好自己的優(yōu)勢文章來源:http://www.zghlxwxcb.cn/news/detail-732335.html
我的自動化測試之路,一路走來都離不每個階段的計劃,因為自己喜歡規(guī)劃和總結(jié),所以,我和朋友特意花了一段時間整理編寫了以上的《自動化測試工程師進階路線》,也整理了不少【網(wǎng)盤資源】,需要的朋友可以掃描下方小卡片獲取網(wǎng)盤鏈接。希望會給你帶來幫助和方向。文章來源地址http://www.zghlxwxcb.cn/news/detail-732335.html
到了這里,關(guān)于接口自動化測試框架搭建【附詳細(xì)搭建視頻】的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!