集成测试是软件测试的一个阶段,在软件测试中,单独软件模块做为一组进行组合和测试,而不是单独测试每一个类。这能够经过使用用于后端代码的JUnit和用于UI的Selenium来轻松实现。这两个测试均可以做为构建/CI系统的一部分,来查看报告和构建/CI系统失败或经过。git
因为咱们如今都在写或维护RESTful微服务,这些服务或api都暴露在web上,还分布在不一样的网络上,所以它们极易有风险和安全威胁,这些威胁会影响到基于它们的进程。所以为了确保它们正确地执行,测试变成必要。为了测试这些API,不依靠人工测试来自动化REST API测试用例是很是重要的。本文关注的是基本原则、机制和测试REST API的几种方法。为了简单起见,这里将使用GitHub REST API。github
能获取的技术和工具备不少,其中包括Apache HTTP client,rest-assured,soapUI,Postman等等。我将介绍Apache HTTP client,rest-assured和soapUI。web
这类测试一般会在持续集成过程当中做为较晚的步骤运行,在它以后运行的REST API已经部署完毕。json
当测试REST APIs时,咱们要关注如下几点:后端
1.用Apache HTTP Client写测试用例api
Http Client提供高效的、最新的和多功能的包,它实现了最新的HTTP标准和推荐的客户端。安全
HTTP响应代码网络
public void validStatusCode() throws IOException { HttpUriRequest request = new HttpGet( "https://api.github.com/events" ); HttpResponse httpResponse = HttpClientBuilder.create().build().execute( request ); Assert.assertThat(httpResponse.getStatusLine().getStatusCode(), equalTo(HttpStatus.OK)); }
响应主体和头部app
public void responseBody() IOException { String jsonMimeType = "application/json"; HttpUriRequest request = new HttpGet( "https://api.github.com/events" ); HttpResponse response = HttpClientBuilder.create().build().execute( request ); String mimeType = ContentType.getOrDefault(response.getEntity()).getMimeType(); Event[] events = new ObjectMapper().readValue(response.getEntity(). getContent(), Event[].class); Assert.assertEquals( jsonMimeType, mimeType ); // more assert starments can be added here } @JsonIgnoreProperties(ignoreUnknown = true)// this is added since new ObjectMapper().readValue(response.getEntity().getContent(), Event[].class); //throw will exception if don't have all properties(part of the response) present in this class class Event { private String type; private long id; private Repo repo; // setters and getters for all properties goes here } @JsonIgnoreProperties(ignoreUnknown = true) class Repo { private long id; private String name; // setters and getters for all properties goes here }
2.经过rest-assured写测试用例maven
REST-assured是Java DSL(领域专用语言)用于简化构建在HTTP Builder顶部基于服务的REST测试。它支持 POST,GET,PUT,DELETE,OPTIONS,PATCH和 HEAD 请求,并能够用于确认和验证这些请求的响应。
HTTP响应代码、响应主体和头部
@Test public void getStatusWithRestAssured() { Event[] events = RestAssured.get("https://api.github.com/events").then() .statusCode(200).assertThat().contentType(ContentType.JSON) .body("", CoreMatchers.notNullValue()) .extract().as(Event[].class); // more assert statement goes here. }
经过rest-assured,能够经过简单的方法覆盖各类测试场景。更多关于rest-assured的细节能够点击阅读原文。
3.经过SoapUI写测试用例
SoapUI是一款开源、跨平台的测试工具。它能够自动化SOAP和REST web服务的功能、回归、依从性和负载测试。它提供了一个易用的图形界面,并支持业界领先的技术和标准来模拟好激发web service的行为。
下面是设置它所需的步骤。
一旦咱们完成上述步骤,经过在pom中增长插件,建立一个maven项目。下面的代码假设项目描述符文件的名称是project.xml。
<plugin> <groupId>com.smartbear.soapui</groupId> <artifactId>soapui-maven-plugin</artifactId> <version>5.2.1</version> <configuration> <projectFile>${basedir}/project.xml</projectFile> </configuration> <executions> <execution> <id>soapui-test</id> <phase>integration-test</phase> <goals> <goal>test</goal> </goals> </execution> </executions> </plugin>
若是在默认的Maven repo下不可用,则须要添加如下资源库:
<pluginRepositories> <pluginRepository> <id>smartbear-sweden-plugin-repository</id> <url>http://www.soapui.org/repository/maven2</url> </pluginRepository> </pluginRepositories>
运行接下来的Maven命令来运行你全部的测试:
mvn clean integration-test mvn clean integration-test