接口自动化测试利器,使用Rest Assured进行REST API测试

1298 篇文章 7 订阅
215 篇文章 2 订阅

在这里插入图片描述2024软件测试面试刷题,这个小程序(永久刷题),靠它快速找到工作了!(刷题APP的天花板)

我们在做接口测试时,一般在代码中会使用HttpClient,但是HttpClient相对来讲还是比较麻烦的,代码量也相对较多,对于新手而言上手会比较难一点,今天我们来看下另一个接口测试工具包REST Assured

REST Assured是一个流行的Java库,用于测试RESTful Web服务。它提供了简单但强大的DSL(域特定语言)来验证REST服务的行为。

它完全支持所有REST方法,如GET、PUT、POST、PATCH等,可以说是接口自动化测试的利器。

引入 Rest Assured 依赖

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

编写Rest API测试

1.引入rest-assured依赖

2.编写代码

import io.restassured.RestAssured;
import io.restassured.http.Method;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;

/**
 * @author 作者:测试工程师成长之路
 * @version 创建时间:2023/12/24
 * 类说明:Rest Assured 示例代码
 */
public class RestAssuredAPITest {
    @Test
    public void GetBooksDetails() {
        // 指定请求的url 
        RestAssured.baseURI = "https://127.0.0.1/TesterRoad/Books";

        // 获取要发送到服务器的请求的RequestSpecification 
        RequestSpecification httpRequest = RestAssured.given();

        // 指定请求类型和请求报文
        Response response = httpRequest.request(Method.GET, "");
        // 输出接口响应报文和状态
        System.out.println("Status received => " + response.getStatusLine());
        System.out.println("Response=>" + response.prettyPrint());
    }
}

相关 API 说明

RestAssured.baseURI = "https://127.0.0.1/TesterRoad/Books";

上面的代码使用RestAssured类来设置一个基本URI。

在本例中,基本URI是https://127.0.0.1/TesterRoad/Books。

RequestSpecification httpRequest = RestAssured.given();

获取要发送到服务器的请求的RequestSpecification,Rest Assured库为此提供了一个名为RequestSpecification的接口。

变量httpRequest存储请求,以便我们可以在需要时修改它,例如添加身份验证,添加头等。

Response response = httpRequest.request(Method.GET, "");

调用服务器来使用RequestSpecification对象获取资源,上面的代码使用request方法向服务器发送对资源的请求。

request方法有两个参数,第一个是HTTP请求方法,第二个是字符串。字符串参数用于指定要与基URI一起发送的参数。在本例中,因为不需要任何参数,因此为空字符串。

请求方法的返回类型是Response对象,这意味着请求方法从服务器获取响应。

System.out.println("Status received => " + response.getStatusLine()); 
System.out.println("Response=>" + response.prettyPrint());

在上面的代码中,我们只是将响应作为字符串读取并将其打印到控制台。我们使用Response接口的getBody方法来返回响应的实际主体,然后将其打印到控制台。

我们还可以使用Rest Assured提供的简写方法来重构上述测试代码。

import io.restassured.RestAssured;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
import org.junit.Test;


/**
 * @author 作者:测试工程师成长之路
 * @version 创建时间:2023/12/24
 * 类说明:Rest Assured 示例代码
 */
public class RestAssuredAPITest {

    @Test
    public void GetBooksDetails() {

        // 指定请求的url
        RestAssured.baseURI = "https://demoqa.com/BookStore/v1/Books";
        // 获取要发送到服务器的请求的RequestSpecification
        RequestSpecification httpRequest = RestAssured.given();
        // 指定请求类型和请求报文
        Response response = httpRequest.get("");
        // 输出接口响应报文和状态
        System.out.println("Response Body is =>  " + response.asString());
    }
}

断言响应状态码和获取响应头信息

1.验证HTTP响应状态码

int statusCode = response.getStatusCode();
System.out.println("statusCode => "+statusCode);
// 断言HTTP响应状态
Assert.assertEquals(statusCode , 200);

上述代码将返回值statusCode与预期值(即200)进行断言

在这里插入图片描述

2.获取头信息

// 获取HTTP响应头
Headers allHeaders = response.headers();
for(Header header : allHeaders) {
    System.out.println("Key: " + header.getName() + ",Value: " + header.getValue());
}

在上面的代码中,我们访问了所有的头信息,然后通过遍历Headers集合来提取单个头。

图片

3.获取指定的头信息

// 获取指定的头信息,如 Content-Type
String contentType = response.header("Content-Type");
System.out.println("Content-Type => " + contentType);
// 输出
Content-Type => application/json; charset=utf-8

上述代码使用header(String arg0)方法来获取特定的header。

使用JsonPath处理响应报文

图片

上图为接口响应报文

// books节点是一个数组
// 使用 JsonPath 获取指定字段,此处用于获取第一个pages
JsonPath jsonPath = response.jsonPath();
System.out.println("books第一个节点的pages => "+jsonPath.get("books[0].pages"));
// 输出
books第一个节点的pages => 234

全部代码

1.pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>rest-assured</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>io.rest-assured</groupId>
            <artifactId>rest-assured</artifactId>
            <version>5.4.0</version>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/junit/junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
            <scope>test</scope>
        </dependency>

    </dependencies>
</project>

2.示例代码

import io.restassured.RestAssured;
import io.restassured.http.Header;
import io.restassured.http.Headers;
import io.restassured.path.json.JsonPath;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
import org.junit.Assert;
import org.junit.Test;

/**
 * @author 作者:测试工程师成长之路
 * @version 创建时间:2023/12/24
 * 类说明:Rest Assured 示例代码
 */
public class RestAssuredAPITest {

    @Test
    public void GetBooksDetails() {

        // 指定请求的url
        RestAssured.baseURI = "https://127.0.0.1/TesterRoad/Books";
        // 获取要发送到服务器的请求的RequestSpecification
        RequestSpecification httpRequest = RestAssured.given();
        // 指定请求类型和请求报文
        Response response = httpRequest.get("");
        // 输出接口响应报文和状态
        System.out.println("Response Body =>  " + response.asString());
        int statusCode = response.getStatusCode();
        System.out.println("statusCode => "+statusCode);
        // 断言HTTP响应状态
        Assert.assertEquals(statusCode , 200);
        // 获取HTTP响应头
        Headers allHeaders = response.headers();
        for(Header header : allHeaders) {
            System.out.println("Key: " + header.getName() + ",Value: " + header.getValue());
        }
        // 获取指定的头信息,如 Content-Type
        String contentType = response.header("Content-Type");
        System.out.println("Content-Type => " + contentType);
        // books节点是一个数组
        // 使用 JsonPath 获取指定字段,此处用于获取第一个pages
        JsonPath jsonPath = response.jsonPath();
        System.out.println("books第一个节点的pages => "+jsonPath.get("books[0].pages"));
    }
}

行动吧,在路上总比一直观望的要好,未来的你肯定会感谢现在拼搏的自己!如果想学习提升找不到资料,没人答疑解惑时,请及时加入群: 786229024,里面有各种测试开发资料和技术可以一起交流哦。

最后: 下方这份完整的软件测试视频教程已经整理上传完成,需要的朋友们可以自行领取 【保证100%免费】

在这里插入图片描述

软件测试面试文档

我们学习必然是为了找到高薪的工作,下面这些面试题是来自阿里、腾讯、字节等一线互联网大厂最新的面试资料,并且有字节大佬给出了权威的解答,刷完这一套面试资料相信大家都能找到满意的工作。在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

  • 19
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值