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

本贴最后更新于 1226 天前,其中的信息可能已经事过景迁

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借鉴了这一套描述可以使得语法更加简洁:

所以我们很容易想到这条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"));
//equalTo是hamcrest所带断言,hamcrest有提供非常丰富的断言方式

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

2 操作
shakebabe 在 2020-11-19 15:50:07 更新了该帖
shakebabe 在 2020-08-07 16:37:21 更新了该帖
6 回帖
请输入回帖内容 ...