接口测试客户端的搭建
一. 引言
隨著公司項(xiàng)目整體架構(gòu)由原來(lái)的多渠道各自為戰(zhàn),向著建立統(tǒng)一的服務(wù)端應(yīng)用,從而為各渠道提供服務(wù)調(diào)用的轉(zhuǎn)變,服務(wù)端應(yīng)用接口測(cè)試成了我們?nèi)粘9ぷ髦械闹匾蝿?wù)之一。經(jīng)過(guò)半年的摸索和項(xiàng)目實(shí)戰(zhàn),我們已經(jīng)掌握了一套接口測(cè)試的流程。這里,對(duì)我們的接口測(cè)試流程進(jìn)行下梳理,一來(lái)是對(duì)之前的工作進(jìn)行總結(jié),二來(lái)可以發(fā)現(xiàn)其中的不足和需要改進(jìn)之處。
接口測(cè)試客戶端的搭建,主要可以分為以下幾個(gè)步驟:
1. 客戶端項(xiàng)目搭建
2. 服務(wù)端地址配置
3. 接口請(qǐng)求封裝
4. 測(cè)試用例編寫
二. 正文
1. 客戶端項(xiàng)目搭建
a.新建項(xiàng)目
首先,在eclipse中創(chuàng)建一個(gè)簡(jiǎn)單的maven項(xiàng)目,目錄結(jié)構(gòu)可以為默認(rèn)的,這里我們用不到test/resources這個(gè)路徑,可以把它給去了。jdk版本可以根據(jù)服務(wù)端應(yīng)用的版本來(lái)選擇,這里我們選的是1.6的。
b.導(dǎo)入jar包
接著,編寫pom.xml文件引入相應(yīng)的jar包。相關(guān)的jar可以分為三類:框架/工具類、服務(wù)端應(yīng)用接口包、測(cè)試用例框架包。框架一般會(huì)用到spring(配置服務(wù)端地址)、hibernate(連接數(shù)據(jù)庫(kù)),工具類的有commons-lang,jackson,log4j等等;服務(wù)端應(yīng)用相關(guān)的包,例如:我們的服務(wù)調(diào)用用的是dubbo協(xié)議,則需要引入dubbo相關(guān)的jar以及服務(wù)提供的相應(yīng)接口api包, 如果我們想用cxf-jaxws來(lái)調(diào)用服務(wù),則需要引入cxf相關(guān)的jar包;測(cè)試用例框架包,這里我們用的是junit;另外,如果還想要對(duì)接口進(jìn)行性能測(cè)試的話,就需要引入對(duì)應(yīng)測(cè)試工具相關(guān)的jar包,如:ApacheJMeter-core/ApacheJMeter-java。以下是一個(gè)簡(jiǎn)單的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目錄下新建包
接下來(lái),我們?cè)趕rc/main/java源碼目錄下新建相關(guān)的包路徑,根據(jù)包中類的功能大致可以三類,
1. 工具輔組類,例如:常量類,以及常用工具類的封裝類
2. 請(qǐng)求參數(shù)封裝類
3.服務(wù)端接口封裝及調(diào)用類
2.服務(wù)端地址配置
首先,在resources目錄下新建xml配置文件,采用spring的IOC來(lái)管理各服務(wù)提供的接口地址。
例一:dubbo協(xié)議的服務(wù)地址配置文件
<?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.*"/> <!-- 消費(fèi)方應(yīng)用名,用于計(jì)算依賴關(guān)系,不是匹配條件,不要與提供方一樣 --><dubbo:application name="app_opentravel" /><!-- 使用zookeeper注冊(cè)中心暴露服務(wù)地址 --><!-- <dubbo:registry address="redis://172.0.0.1:6380" /> --><!-- 生成遠(yuǎn)程服務(wù)代理,可以像使用本地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形式的遠(yuǎn)程調(diào)用接口地址配置
<?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>配置完接口地址后,接下來(lái),我們?cè)赾om.lglan.services路徑下新建一個(gè)抽象類AbstractBaseService.java 用來(lái)默認(rèn)加載配置文件,并且定義一個(gè)抽象方法,其它的service類可以通過(guò)繼承該抽象類并實(shí)現(xiàn)抽象方法來(lái)獲取特定的服務(wù)接口:
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類可以用來(lái)獲得OneService服務(wù)
package com.lglan.services;public class OneService extends AbstractBaseService{@Overridepublic OneService getRemoteService() {OneService iOneService = (OneService) getApplicationContext().getBean("iOneService");return iOneService;}}這樣,如果在測(cè)試用例中要調(diào)用OneService接口,則直接創(chuàng)建一個(gè)OneService類就可以調(diào)用其具體的服務(wù)了。
3.接口請(qǐng)求封裝
服務(wù)端提供的接口請(qǐng)求類,其結(jié)構(gòu)根據(jù)業(yè)務(wù)需要往往是比較復(fù)雜的,一個(gè)請(qǐng)求包含幾十個(gè)類幾百個(gè)字段都是很正常的事。所以,在測(cè)試用例中,不可能把請(qǐng)求中的所有字段都填上,通常只是輸入一些跟具體業(yè)務(wù)邏輯和常見(jiàn)相關(guān)的參數(shù),其它字段要么不填要么寫死(如果是必填的話)。為了測(cè)試用例代碼的簡(jiǎn)潔,我們要對(duì)請(qǐng)求先進(jìn)行封裝,只暴露幾個(gè)和業(yè)務(wù)相關(guān)的必填參數(shù),這樣,測(cè)試類中只要通過(guò)輸入幾個(gè)不同的參數(shù)組合,就可以得到不同的測(cè)試用例,來(lái)測(cè)試不同的業(yè)務(wù)邏輯和場(chǎng)景了。
例如:一個(gè)機(jī)票預(yù)定的請(qǐng)求,其數(shù)據(jù)結(jié)構(gòu)如下圖,如果每個(gè)測(cè)試用例都要去手動(dòng)填寫如下這些參數(shù)的話,那肯定得瘋了。所以通過(guò)請(qǐng)求封裝后,我們只暴露了出發(fā)到達(dá)城市和日期以及乘機(jī)人數(shù),每個(gè)測(cè)試用例只要填寫以上幾個(gè)簡(jiǎn)單的參數(shù)就可以執(zhí)行了。這也更符合實(shí)際的業(yè)務(wù)場(chǎng)景,對(duì)于用戶來(lái)說(shuō),也只是需要輸入這么幾個(gè)參數(shù)就可以預(yù)定機(jī)票了。
?
4.測(cè)試用例編寫
通過(guò)以上幾個(gè)步驟的工作,我們的接口測(cè)試客戶端就基本上算是搭建完成了。最后就是測(cè)試用例的設(shè)計(jì)和編寫。一般我們采用junit框架來(lái)編寫測(cè)試用例,在src/test/java下面按接口名新建一個(gè)包,并在包下面創(chuàng)建測(cè)試用例類,如:
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 = "請(qǐng)求參數(shù)1";String agr2 = "請(qǐng)求參數(shù)2";OneServiceRQ oneServiceRQ = OneServiceRQUtil.assembleRQ(arg1,arg2); OneServiceRS oneServiceRS = oneServiceRQ.getResponse(oneServiceRQ);System.out.println(oneServiceRS);}@Afterpublic void tearDown() throws Exception {} }三.總結(jié)
1.新建項(xiàng)目,引入相關(guān)jar包
2.配置接口地址的xml文件
3.用一個(gè)抽象類來(lái)讀取配置文件,服務(wù)類繼承該抽象類來(lái)獲得相應(yīng)的接口實(shí)例化對(duì)象(工廠模式)
4.對(duì)請(qǐng)求參數(shù)進(jìn)行封裝
5.編寫測(cè)試用例,組裝請(qǐng)求并通過(guò)服務(wù)類進(jìn)行服務(wù)調(diào)用,打印響應(yīng)結(jié)果
?
全文完…
轉(zhuǎn)載于:https://www.cnblogs.com/beetle-shu/p/4199755.html
總結(jié)
以上是生活随笔為你收集整理的接口测试客户端的搭建的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Windows如何开启虚拟化,以安装虚拟
- 下一篇: (剑指Offer)面试题5:从尾到头打印