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

我们在做接口测试时,一般在代码中会使用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"));

}

}

最后感谢每一个认真阅读我文章的人,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走:【文末小卡片领取】

这些资料,对于【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴上万个测试工程师们走过最艰难的路程,希望也能帮助到你!

  • 12
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值