集成测试是软件测试的一个阶段,在软件测试中,单独软件模块作为一组进行组合和测试,而不是单独测试每个类。这可以通过使用用于后端代码的JUnit和用于UI的Selenium来轻松实现。这两个测试都可以作为构建/CI系统的一部分,来查看报告和构建/CI系统失败或通过。

由于我们现在都在写或维护RESTful微服务,这些服务或api都暴露在web上,还分布在不同的网络上,因此它们极易有风险和安全威胁,这些威胁会影响到基于它们的进程。因此为了确保它们正确地执行,测试变成必要。为了测试这些API,不依靠人工测试来自动化REST API测试用例是非常重要的。本文关注的是基本原则、机制和测试REST API的几种方法。为了简单起见,这里将使用GitHub REST API。

能获取的技术和工具有很多,其中包括Apache HTTP client,rest-assured,soapUI,Postman等等。我将介绍Apache HTTP client,rest-assured和soapUI。

这类测试通常会在持续集成过程中作为较晚的步骤运行,在它之后运行的REST API已经部署完毕。

当测试REST APIs时,我们要关注以下几点:

  • HTTP响应代码
  • 响应主体 - JSON, XML
  • 在响应中的HTTP头部

1.用Apache HTTP Client写测试用例

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));

}

响应主体和头部

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写测试用例

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的行为。

下面是设置它所需的步骤。

  1. 创建soapUI测试项目。
  2. 定义端点。
  3. 测试用例和测试套件的创建。
  4. 为端点添加测试步骤。
  5. 项目描述符生成。

一旦我们完成上述步骤,通过在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


博云
104 声望16 粉丝

博云技术社区定期分享容器、微服务、DevOps等云原生技术干货和落地实践。