接口测试客户端的搭建
一. 引言
隨著公司項目整體架構由原來的多渠道各自為戰,向著建立統一的服務端應用,從而為各渠道提供服務調用的轉變,服務端應用接口測試成了我們日常工作中的重要任務之一。經過半年的摸索和項目實戰,我們已經掌握了一套接口測試的流程。這里,對我們的接口測試流程進行下梳理,一來是對之前的工作進行總結,二來可以發現其中的不足和需要改進之處。
接口測試客戶端的搭建,主要可以分為以下幾個步驟:
1. 客戶端項目搭建
2. 服務端地址配置
3. 接口請求封裝
4. 測試用例編寫
二. 正文
1. 客戶端項目搭建
a.新建項目
首先,在eclipse中創建一個簡單的maven項目,目錄結構可以為默認的,這里我們用不到test/resources這個路徑,可以把它給去了。jdk版本可以根據服務端應用的版本來選擇,這里我們選的是1.6的。
b.導入jar包
接著,編寫pom.xml文件引入相應的jar包。相關的jar可以分為三類:框架/工具類、服務端應用接口包、測試用例框架包。框架一般會用到spring(配置服務端地址)、hibernate(連接數據庫),工具類的有commons-lang,jackson,log4j等等;服務端應用相關的包,例如:我們的服務調用用的是dubbo協議,則需要引入dubbo相關的jar以及服務提供的相應接口api包, 如果我們想用cxf-jaxws來調用服務,則需要引入cxf相關的jar包;測試用例框架包,這里我們用的是junit;另外,如果還想要對接口進行性能測試的話,就需要引入對應測試工具相關的jar包,如:ApacheJMeter-core/ApacheJMeter-java。以下是一個簡單的pom配置:
<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><groupId>com.lglan</groupId><artifactId>InterfaceTestDemo</artifactId><version>0.0.1-SNAPSHOT</version><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><spring.version>3.1.2.RELEASE</spring.version><junit.version>4.9</junit.version><dubbo.version>2.5.3</dubbo.version><zkclient.version>0.1</zkclient.version><zookeeper.version>3.4.5</zookeeper.version></properties><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>${junit.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-beans</artifactId><version>${spring.version}</version></dependency><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.14</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>dubbo</artifactId><version>${dubbo.version}</version><exclusions><exclusion><groupId>org.springframework</groupId><artifactId>spring</artifactId></exclusion></exclusions></dependency><dependency><groupId>com.github.sgroschupf</groupId><artifactId>zkclient</artifactId><version>${zkclient.version}</version></dependency><dependency><groupId>org.apache.zookeeper</groupId><artifactId>zookeeper</artifactId><version>${zookeeper.version}</version><exclusions><exclusion><artifactId>jmxtools</artifactId><groupId>com.sun.jdmk</groupId></exclusion><exclusion><artifactId>jmxri</artifactId><groupId>com.sun.jmx</groupId></exclusion><exclusion><artifactId>jms</artifactId><groupId>javax.jms</groupId></exclusion></exclusions></dependency><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-frontend-simple</artifactId><version>2.6.1</version></dependency><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-transports-http</artifactId><version>2.6.1</version></dependency><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-frontend-jaxws</artifactId><version>2.6.1</version><exclusions><exclusion><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-ws-addr</artifactId></exclusion></exclusions></dependency></dependencies><build><plugins><plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.16</version> <configuration> <skipTests>true</skipTests> </configuration> </plugin> </plugins></build> </project>C. src/main/java目錄下新建包
接下來,我們在src/main/java源碼目錄下新建相關的包路徑,根據包中類的功能大致可以三類,
1. 工具輔組類,例如:常量類,以及常用工具類的封裝類
2. 請求參數封裝類
3.服務端接口封裝及調用類
2.服務端地址配置
首先,在resources目錄下新建xml配置文件,采用spring的IOC來管理各服務提供的接口地址。
例一:dubbo協議的服務地址配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"xmlns:context="http://www.springframework.org/schema/context"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"><context:annotation-config/><context:component-scan base-package="com.*"/> <!-- 消費方應用名,用于計算依賴關系,不是匹配條件,不要與提供方一樣 --><dubbo:application name="app_opentravel" /><!-- 使用zookeeper注冊中心暴露服務地址 --><!-- <dubbo:registry address="redis://172.0.0.1:6380" /> --><!-- 生成遠程服務代理,可以像使用本地bean一樣使用demoService --><dubbo:reference id="iOneService" interface="com.lglan.demo.service.IOneService" timeout="5000" url="dubbo://172.0.0.1:20880"/> <dubbo:reference id="iTwoService" interface="com.lglan.demo.service.ITwoService" timeout="5000" url="dubbo://172.0.0.1:20880"/> </beans>例二:jaxws形式的遠程調用接口地址配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:jaxws="http://cxf.apache.org/jaxws"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsdhttp://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"><context:annotation-config/><context:component-scan base-package="com.ceair.travel.handlers"/><jaxws:client id="OneService" serviceClass="com.lglan.demo.service.IOneService" address="http://172.0.0.1/lglan/services/IOneService?wsdl"/></beans>配置完接口地址后,接下來,我們在com.lglan.services路徑下新建一個抽象類AbstractBaseService.java 用來默認加載配置文件,并且定義一個抽象方法,其它的service類可以通過繼承該抽象類并實現抽象方法來獲取特定的服務接口:
package com.lglan.services;import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.context.support.ClassPathXmlApplicationContext;public abstract class AbstractBaseService implements ApplicationContextAware{private ApplicationContext applicationContext;@Overridepublic void setApplicationContext(ApplicationContext applicationContext)throws BeansException {this.applicationContext = applicationContext;}public ApplicationContext getApplicationContext(){if (null == this.applicationContext) {return new ClassPathXmlApplicationContext(new String[]{"classpath:dubboService.xml","classpath:wsdlService.xml"} );}else {return this.applicationContext;}}public abstract Object getRemoteService();}例如:OneService.java類可以用來獲得OneService服務
package com.lglan.services;public class OneService extends AbstractBaseService{@Overridepublic OneService getRemoteService() {OneService iOneService = (OneService) getApplicationContext().getBean("iOneService");return iOneService;}}這樣,如果在測試用例中要調用OneService接口,則直接創建一個OneService類就可以調用其具體的服務了。
3.接口請求封裝
服務端提供的接口請求類,其結構根據業務需要往往是比較復雜的,一個請求包含幾十個類幾百個字段都是很正常的事。所以,在測試用例中,不可能把請求中的所有字段都填上,通常只是輸入一些跟具體業務邏輯和常見相關的參數,其它字段要么不填要么寫死(如果是必填的話)。為了測試用例代碼的簡潔,我們要對請求先進行封裝,只暴露幾個和業務相關的必填參數,這樣,測試類中只要通過輸入幾個不同的參數組合,就可以得到不同的測試用例,來測試不同的業務邏輯和場景了。
例如:一個機票預定的請求,其數據結構如下圖,如果每個測試用例都要去手動填寫如下這些參數的話,那肯定得瘋了。所以通過請求封裝后,我們只暴露了出發到達城市和日期以及乘機人數,每個測試用例只要填寫以上幾個簡單的參數就可以執行了。這也更符合實際的業務場景,對于用戶來說,也只是需要輸入這么幾個參數就可以預定機票了。
?
4.測試用例編寫
通過以上幾個步驟的工作,我們的接口測試客戶端就基本上算是搭建完成了。最后就是測試用例的設計和編寫。一般我們采用junit框架來編寫測試用例,在src/test/java下面按接口名新建一個包,并在包下面創建測試用例類,如:
package com.lglan.oneservice.testcase;import static org.junit.Assert.*;import org.junit.After; import org.junit.Before; import org.junit.Test;import com.lglan.services.OneService;public class OneServiceTest {OneService oneService;@Beforepublic void setUp() throws Exception {oneService = new OneService();}@Testpublic void testOneService() {String agr1 = "請求參數1";String agr2 = "請求參數2";OneServiceRQ oneServiceRQ = OneServiceRQUtil.assembleRQ(arg1,arg2); OneServiceRS oneServiceRS = oneServiceRQ.getResponse(oneServiceRQ);System.out.println(oneServiceRS);}@Afterpublic void tearDown() throws Exception {} }三.總結
1.新建項目,引入相關jar包
2.配置接口地址的xml文件
3.用一個抽象類來讀取配置文件,服務類繼承該抽象類來獲得相應的接口實例化對象(工廠模式)
4.對請求參數進行封裝
5.編寫測試用例,組裝請求并通過服務類進行服務調用,打印響應結果
?
全文完…
轉載于:https://www.cnblogs.com/beetle-shu/p/4199755.html
總結
以上是生活随笔為你收集整理的接口测试客户端的搭建的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Windows如何开启虚拟化,以安装虚拟
- 下一篇: (剑指Offer)面试题5:从尾到头打印