代理网站推荐,wordpress 采集小说,网站开发工程师累不累,域名注册阿里云我们在做接口测试时#xff0c;一般在代码中会使用HttpClient#xff0c;但是HttpClient相对来讲还是比较麻烦的#xff0c;代码量也相对较多#xff0c;对于新手而言上手会比较难一点#xff0c;今天我们来看下另一个接口测试工具包REST Assured
REST Assured是一个流行…我们在做接口测试时一般在代码中会使用HttpClient但是HttpClient相对来讲还是比较麻烦的代码量也相对较多对于新手而言上手会比较难一点今天我们来看下另一个接口测试工具包REST Assured
REST Assured是一个流行的Java库用于测试RESTful Web服务。它提供了简单但强大的DSL(域特定语言)来验证REST服务的行为。
它完全支持所有REST方法如GET、PUT、POST、PATCH等可以说是接口自动化测试的利器。
引入 Rest Assured 依赖
dependencygroupIdio.rest-assured/groupIdartifactIdrest-assured/artifactIdversion5.4.0/versionscopetest/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 {Testpublic 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();获取要发送到服务器的请求的RequestSpecificationRest 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 {Testpublic void GetBooksDetails() {// 指定请求的urlRestAssured.baseURI https://demoqa.com/BookStore/v1/Books;// 获取要发送到服务器的请求的RequestSpecificationRequestSpecification 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; charsetutf-8上述代码使用headerString 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 version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersiongroupIdorg.example/groupIdartifactIdrest-assured/artifactIdversion1.0-SNAPSHOT/versionpropertiesmaven.compiler.source8/maven.compiler.sourcemaven.compiler.target8/maven.compiler.target/propertiesdependenciesdependencygroupIdio.rest-assured/groupIdartifactIdrest-assured/artifactIdversion5.4.0/versionscopetest/scope/dependency!-- https://mvnrepository.com/artifact/junit/junit --dependencygroupIdjunit/groupIdartifactIdjunit/artifactIdversion4.13.2/versionscopetest/scope/dependency/dependencies
/project2.示例代码
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 {Testpublic void GetBooksDetails() {// 指定请求的urlRestAssured.baseURI https://127.0.0.1/TesterRoad/Books;// 获取要发送到服务器的请求的RequestSpecificationRequestSpecification 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-TypeString contentType response.header(Content-Type);System.out.println(Content-Type contentType);// books节点是一个数组// 使用 JsonPath 获取指定字段,此处用于获取第一个pagesJsonPath jsonPath response.jsonPath();System.out.println(books第一个节点的pages jsonPath.get(books[0].pages));}
}总结
感谢每一个认真阅读我文章的人
作为一位过来人也是希望大家少走一些弯路如果你不想再体验一次学习时找不到资料没人解答问题坚持几天便放弃的感受的话在这里我给大家分享一些自动化测试的学习资源希望能给你前进的路上带来帮助。 软件测试面试文档
我们学习必然是为了找到高薪的工作下面这些面试题是来自阿里、腾讯、字节等一线互联网大厂最新的面试资料并且有字节大佬给出了权威的解答刷完这一套面试资料相信大家都能找到满意的工作。 视频文档获取方式 这份文档和视频资料对于想从事【软件测试】的朋友来说应该是最全面最完整的备战仓库这个仓库也陪伴我走过了最艰难的路程希望也能帮助到你以上均可以分享点下方小卡片即可自行领取。