接口自動化測試框架搭建


一、原理及特點

  1. 參數放在XML文件中進行管理
  2. 用httpClient簡單封裝一個httpUtils工具類
  3. 測試用例管理使用了testNg管理,使用了TestNG參數化測試,通過xml文件來執行case。
  4. 測試報告這里用到第三方的包ReportNG 項目組織用Maven

二、准備

    使用工具:eclipse,maven
    用到的第三方jar包:dom4j、reportng、testng
    理解難點:httpUtils和xmlUtil工具類的封裝;dom4j使用;CookieStore的應用

三、框架構思

1、項目結構

2、用例執行流程

3、接口調用流程

4、調度腳本流程

 

四、框架實現

1、輸入參數

1.1 參數放在XML文件中進行管理

例:這里測試獲取角色的接口輸入參數為,page和rows,mapRole.xml內容如下

<?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工具類負責讀取XML,使用第三方的jar包dom4j

1.2.1 xmlUtil中readXMLDocument方法返回值為HashMap<String, String>

public static HashMap<String, String> readXMLDocument(String beanName,String xmlName){ }

參數xmlName(xml文件的名字); 參數beanName(xml文件中節點的名稱);

1.3 封裝一個CookieUtil工具類,通過CookieStore儲存cookie

1.3.1 CookieUtil類中setCookieStore方法返回值為CookieStore

public CookieStore setCookieStore(HttpResponse httpResponse) { }

1.4 用httpClient簡單封裝一個httpUtils工具類有get.post,put,delete方法

1.4.1 httpUtils中post封裝方法如下:

public CloseableHttpResponse post(String url, Map<String, String> params,CloseableHttpClient httpclient,CookieStore cookieStore){ }

2、返回參數

2.1 創建一個接口返回對象ResponseBean

對象ResponseBean,包括status、statusCode、contentType、body、url、method、cookies

2.2 在工具類中在創建一個ReponseUtil工具類

ReponseUtil工具類負責將請求的返回數據CloseableHttpResponse 轉換成ResponseBean

public ResponseBean setResponseBean(CloseableHttpResponse httpResponse) { }

3、測試用例

測試用例管理使用了testNg管理 ,使用了TestNG參數化測試,通過xml文件來執行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 /* * 登錄進入系統獲取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 { // 關閉流並釋放資源 client.close(); } catch (IOException e) { e.printStackTrace(); } } }
  • [注] 因為API接口測試時每次都要校驗Cookie,所有我們每次都先執行登錄操作去獲取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對應的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想對應............ <suiteXmlFile>src/test/java/testSuites/${xmlFileName10}</suiteXmlFile> </suiteXmlFiles> </configuration> </plugin> <!-- 添加插件,添加ReportNg的監聽器,修改最后的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,所有的測試用例就開始執行了

測試報告

框架目前存在的不足

  • 1、數據庫數據校驗這一塊的功能還沒有完善,計划用MyBatis
  • 2、參數使用了xml文件配置雖然靈活但有些繁瑣,目前還沒想到好的解決方案,testlink是否可以嘗試一下呢

 

項目源碼地址:https://git.oschina.net/hellotester/iaf.git

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM