REST-Assured,接口自动化的 "瑞士军刀"- 初识篇

500 阅读3分钟

REST-Assured 简介

REST-Assured 是一套基于 Java 语言实现的开源 REST API 测试框架,由作者 Johan Haleby 开发并维护,目前该项目在 GitHub 上已收获 4.9K star

image.png

从官方描述可以看到 REST-Assured 使得通过 Java 语言测试 REST API 变得更加简单和容易

REST-Assured 除了语法简洁之外,强大的解析功能(支持 XML,JSON)也是其成为如今企业首选的接口自动化框架原因之一。【软件测试资料合集】

REST-Assured 初体验

Step 1)安装 JDK

Step 2)安装 IDE,推荐 Intellij IDEA

Step 3)安装 Maven,设置 Maven 镜像

引入 REST-Assured 依赖

1.创建 Maven 工程

2.POM.xml 添加 REST-Assured 依赖坐标

  <dependency>
      <groupId>io.rest-assured</groupId>
      <artifactId>rest-assured</artifactId>
      <version>4.3.0</version>
      <scope>test</scope>
</dependency>

3.创建 Java Class,静态导入 REST-Assured 类路径(官方推荐使用,编写脚本时更加有效率)

  import static io.restassured.RestAssured.*;

4.第一个 get 请求

  given().
when().
    get("http://httpbin.org/get?phone=13323234545&password=123456").
then().
    log().body();

类似于行为驱动开发(Behaviour Driven Development-BDD)中的定义的结构 Given-When-Then,Given:在某场景下,When:发生什么事件,Then:产生了什么结果。而 REST-Assured 借鉴了这一套描述可以使得语法更加简洁:

  • given 设置测试预设(包括请求头、请求参数、请求体、cookies 等等)
  • when 所要执行的操作(GET/POST 请求)
  • then 解析结果、断言

所以我们很容易想到这条 case 的作用:发送 get 请求,log()表示输出响应结果信息,body()输出响应体内容。

如果要输出响应的所有信息,使用 log().all()即可。

param 参数设置

我们会注意到上面这条 case 参数和 URL 是拼接在一起的,REST-Assured 可以让每部分(URL,参数,请求头)分开来,确保我们的代码有更好的可读性,在 given 中配置 queryParam 查询参数:

  given().
    queryParam("mobilephone","13323234545").
    queryParam("password","123456").
when().
    get("http://httpbin.org/get").
then().
   log().body();

而且我们还能采用更加智能的方式:given 中指定 param,此时 REST-Assured 将会自动根据 Http 方法决定参数类型(GET 方法将会自动使用查询参数,POST 方法将会自动使用表单参数)

  //GET方法将会自动使用查询参数
given().
    param("mobilephone","13323234545").
    param("password","123456").
when().
    get("http://httpbin.org/get").
then().
    log().body();
//POST方法将会自动使用表单参数
given().
    param("mobilephone","13323234545").
    param("password","123456").
when().
    post("http://httpbin.org/post").
then().
    log().body();

Cookies 设置

如果想要在请求中携带 Cookies 信息,REST-Assured 给我们提供了非常方便的方式:

  given().
    cookie("cookieName","cookieValue").
when().
    post("http://httpbin.org/post").
then().
    log().body();

或者是指定多对 cookie:

  given().
    cookie("cookieName","cookieValue1","cookieValue2").
when().
    post("http://httpbin.org/post").
then().
    log().body();

Header 设置

  given().
    header("headerName","value").
when().
    post("http://httpbin.org/post").
then().
    log().body();

Content Type 设置

  given().
    contentType("application/json").
    //或者指定下面的形式
	//contentType(ContentType.JSON).
when().
    post("http://httpbin.org/post").
then().
    log().body();

Request Body 设置

  given().
    body("{\"mobilephone\":\"13323234545\",\"password\":\"123456\"}").
when().
    post("http://httpbin.org/post").
then().
    log().body();

REST-Assured 还支持可以将 Java 对象序列化为 JSON 或者 XML,比如:

1 ) 通过 contentType 指定为 JSON,将 HashMap 序列化为 JSON

  HashMap<String,String> hashMap= new HashMap<String,String>();
hashMap.put("firstName","jack");
hashMap.put("lastName","tom");
given().
    contentType(ContentType.JSON).
    body(hashMap).
when().
    post("http://httpbin.org/post").
then().
    log().body();

2 )通过 contentType 指定为 JSON,将 Message 对象序列化为 JSON

Message.java

  public class Message {
    private String message;
    public String getMessage() {
        return message;
    }
    public void setMessage(String message) {
        this.message = message;
    }
}


  Message message = new Message();
message.setMessage("tester");
given().
    contentType(ContentType.JSON).
    body(message).
when().
    post("http://httpbin.org/post").
then().
    log().body();

3 ) 通过 contentType 指定为 XML,将 Message 对象序列化为 XML

在类前面加注解 XmlRootElement:

  @XmlRootElement
public class Message {}


  Message message = new Message();
message.setMessage("tester");
given().
    contentType(ContentType.XML).
    body(message).
when().
    post("http://httpbin.org/post").
then().
    log().body();

校验响应数据

支持校验状态码, cookies, 响应头, content type 和响应体

  given().
when().
    post("http://httpbin.org/post").
then().
    assertThat().statusCode(200);
	//assertThat().cookie("cookieName","cookieValue");
    //assertThat().header("headName","headerValue");
    //assertThat().contentType(ContentType.JSON);
    //assertThat().body(equalTo("something"));
//equalTohamcrest所带断言,hamcrest有提供非常丰富的断言方式

本文带着大家了解 REST-Assured 的基本结构和语法,当然,REST-Assured 的功能远不止这些,比如其内置的 JsonPath 解析和 XmlPath 解析以及 hamcrest 断言都是十分强大的功能,后续再给大家详细介绍。【软件测试资料合集】