网站建设岗位能力,在线之家官网,介绍好的免费网站模板下载地址,吉林 网站备案 照相一. 引言 随着公司项目整体架构由原来的多渠道各自为战#xff0c;向着建立统一的服务端应用#xff0c;从而为各渠道提供服务调用的转变#xff0c;服务端应用接口测试成了我们日常工作中的重要任务之一。经过半年的摸索和项目实战#xff0c;我们已经掌握了一套接口测试的…一. 引言 随着公司项目整体架构由原来的多渠道各自为战向着建立统一的服务端应用从而为各渠道提供服务调用的转变服务端应用接口测试成了我们日常工作中的重要任务之一。经过半年的摸索和项目实战我们已经掌握了一套接口测试的流程。这里对我们的接口测试流程进行下梳理一来是对之前的工作进行总结二来可以发现其中的不足和需要改进之处。 接口测试客户端的搭建主要可以分为以下几个步骤 1. 客户端项目搭建 2. 服务端地址配置 3. 接口请求封装 4. 测试用例编写 二. 正文 1. 客户端项目搭建 a.新建项目 首先在eclipse中创建一个简单的maven项目目录结构可以为默认的这里我们用不到test/resources这个路径可以把它给去了。jdk版本可以根据服务端应用的版本来选择这里我们选的是1.6的。 b.导入jar包 接着编写pom.xml文件引入相应的jar包。相关的jar可以分为三类框架/工具类、服务端应用接口包、测试用例框架包。框架一般会用到spring配置服务端地址、hibernate连接数据库工具类的有commons-langjacksonlog4j等等服务端应用相关的包例如我们的服务调用用的是dubbo协议则需要引入dubbo相关的jar以及服务提供的相应接口api包, 如果我们想用cxf-jaxws来调用服务则需要引入cxf相关的jar包测试用例框架包这里我们用的是junit另外如果还想要对接口进行性能测试的话就需要引入对应测试工具相关的jar包如ApacheJMeter-core/ApacheJMeter-java。以下是一个简单的pom配置 project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersiongroupIdcom.lglan/groupIdartifactIdInterfaceTestDemo/artifactIdversion0.0.1-SNAPSHOT/versionpropertiesproject.build.sourceEncodingUTF-8/project.build.sourceEncodingproject.reporting.outputEncodingUTF-8/project.reporting.outputEncodingspring.version3.1.2.RELEASE/spring.versionjunit.version4.9/junit.versiondubbo.version2.5.3/dubbo.versionzkclient.version0.1/zkclient.versionzookeeper.version3.4.5/zookeeper.version/propertiesdependenciesdependencygroupIdjunit/groupIdartifactIdjunit/artifactIdversion${junit.version}/version/dependencydependencygroupIdorg.springframework/groupIdartifactIdspring-context/artifactIdversion${spring.version}/version/dependencydependencygroupIdorg.springframework/groupIdartifactIdspring-core/artifactIdversion${spring.version}/version/dependencydependencygroupIdorg.springframework/groupIdartifactIdspring-beans/artifactIdversion${spring.version}/version/dependencydependencygroupIdlog4j/groupIdartifactIdlog4j/artifactIdversion1.2.14/version/dependencydependencygroupIdcom.alibaba/groupIdartifactIddubbo/artifactIdversion${dubbo.version}/versionexclusionsexclusiongroupIdorg.springframework/groupIdartifactIdspring/artifactId/exclusion/exclusions/dependencydependencygroupIdcom.github.sgroschupf/groupIdartifactIdzkclient/artifactIdversion${zkclient.version}/version/dependencydependencygroupIdorg.apache.zookeeper/groupIdartifactIdzookeeper/artifactIdversion${zookeeper.version}/versionexclusionsexclusionartifactIdjmxtools/artifactIdgroupIdcom.sun.jdmk/groupId/exclusionexclusionartifactIdjmxri/artifactIdgroupIdcom.sun.jmx/groupId/exclusionexclusionartifactIdjms/artifactIdgroupIdjavax.jms/groupId/exclusion/exclusions/dependencydependencygroupIdorg.apache.cxf/groupIdartifactIdcxf-rt-frontend-simple/artifactIdversion2.6.1/version/dependencydependencygroupIdorg.apache.cxf/groupIdartifactIdcxf-rt-transports-http/artifactIdversion2.6.1/version/dependencydependencygroupIdorg.apache.cxf/groupIdartifactIdcxf-rt-frontend-jaxws/artifactIdversion2.6.1/versionexclusionsexclusiongroupIdorg.apache.cxf/groupIdartifactIdcxf-rt-ws-addr/artifactId/exclusion/exclusions/dependency/dependenciesbuildpluginsplugin groupIdorg.apache.maven.plugins/groupId artifactIdmaven-surefire-plugin/artifactId version2.16/version configuration skipTeststrue/skipTests /configuration /plugin /plugins/build
/project C. src/main/java目录下新建包 接下来我们在src/main/java源码目录下新建相关的包路径根据包中类的功能大致可以三类 1. 工具辅组类例如常量类以及常用工具类的封装类 2. 请求参数封装类 3.服务端接口封装及调用类 2.服务端地址配置 首先在resources目录下新建xml配置文件采用spring的IOC来管理各服务提供的接口地址。 例一dubbo协议的服务地址配置文件 ?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:dubbohttp://code.alibabatech.com/schema/dubboxmlns:contexthttp://www.springframework.org/schema/contextxmlns:txhttp://www.springframework.org/schema/txxsi:schemaLocationhttp://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.xsdcontext:annotation-config/context:component-scan base-packagecom.*/ !-- 消费方应用名用于计算依赖关系不是匹配条件不要与提供方一样 --dubbo:application nameapp_opentravel /!-- 使用zookeeper注册中心暴露服务地址 --!-- dubbo:registry addressredis://172.0.0.1:6380 / --!-- 生成远程服务代理可以像使用本地bean一样使用demoService --dubbo:reference idiOneService interfacecom.lglan.demo.service.IOneService timeout5000 urldubbo://172.0.0.1:20880/ dubbo:reference idiTwoService interfacecom.lglan.demo.service.ITwoService timeout5000 urldubbo://172.0.0.1:20880/
/beans 例二jaxws形式的远程调用接口地址配置 ?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:contexthttp://www.springframework.org/schema/contextxmlns:txhttp://www.springframework.org/schema/txxmlns:jaxwshttp://cxf.apache.org/jaxwsxsi:schemaLocationhttp://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.xsdcontext:annotation-config/context:component-scan base-packagecom.ceair.travel.handlers/jaxws:client idOneService serviceClasscom.lglan.demo.service.IOneService addresshttp://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