Java基于Rest?Assured自動化測試接口詳解
前言
不知道大家的項目是否都有對接口API進行自動化測試,反正像我們這種小公司是沒有的。由于最近一直被吐槽項目質(zhì)量糟糕,只能研發(fā)自己看看有什么接口測試方案。那么在本文中,我將探索如何使用 Rest Assured 自動化 API 測試,Rest Assured 是一個基于 Java 的流行的用于測試 RESTful API 的庫。
什么是Rest Assured
Rest Assured 是一個基于 Java 的開源庫,主要用于測試 RESTful API。它為編寫測試用例提供了一種簡單直觀的 DSL(領域特定語言),這使得開發(fā)人員可以輕松編寫和維護自動化測試。Rest Assured 支持 GET、POST、PUT、DELETE、PATCH 等各種 HTTP 方法,并且可以輕松與流行的測試框架(如 TestNG 和 JUnit)集成。
github地址:https://github.com/rest-assured/rest-assured
安裝Rest Assured
在maven中引入相關依賴
<dependency> <groupId>io.rest-assured</groupId> <artifactId>rest-assured</artifactId> <version>5.3.0</version> <scope>test</scope> </dependency>
Rest Assured結(jié)構(gòu)
Rest Assured代碼的整體結(jié)構(gòu)分為 3 個主要部分:
1.Given
Given是 API 測試的先決條件,可以在其中設置測試所需的一切,例如URL、請求頭或參數(shù),或任何需要滿足的先決條件。- 可以在“
Given”中設置的內(nèi)容:URL、請求頭、請求參數(shù)和請求正文。
2.When
When是實際向服務器發(fā)送 HTTP 請求并獲得響應的時間??梢栽?code>When中定義請求方法,如 GET、POST、PUT 等。
3.Then
Then是您檢查從服務器獲得的響應并確保它符合您的預期的地方。在這您可以檢查狀態(tài)代碼、響應正文、標頭或任何其他對您的測試很重要的內(nèi)容。
Show Me Code
我們現(xiàn)在通過一個例子來演示下如何使用Rest Assured,首先我們看下postman的例子:
1.請求參數(shù)

2.請求頭

3.請求體

現(xiàn)在我們用Rest Assured這個框架來測試下上面postman的這個接口。
import io.restassured.builder.RequestSpecBuilder;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
import org.testng.annotations.Test;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.lessThan;
public class TestRestAssured {
@Test
public void testMyApi() {
String jsonBody = "{"email":"dhadiprasetyo@gmail.com","uid":"Jzr0sOORprar10kay6CweZ5FNYP2"}";
Response response = given().baseUri("http://127.0.0.1:8000")
.queryParam("version", "1.0")
.header("Authorization", "yourauthhere")
.header("Signature", "yoursignaturehere")
.body(jsonBody)
.when().post("/getuserdata/")
.then().assertThat().statusCode(200)
.header("Content-Type", "application/json")
.header("Cache-Control", "max-age=3600")
.body("name", equalTo("Darmawan Hadiprasetyo"))
.time(lessThan(5000L))
.extract().response();
}
}1.首先我們在 given() 中設置前置條件
given().baseUri("http://127.0.0.1:8000")
.queryParam("version", "1.0")
.header("Authorization", "yourauthhere")
.header("Signature", "yoursignaturehere")
.body(jsonBody)
2.然后在when()中定義請求方法,本例中為POST
.when().post("/getuserdata/")
3.然后我們從我們的請求中斷言狀態(tài)代碼、標頭、正文和響應時間
.then().assertThat().statusCode(200)
.header("Content-Type", "application/json")
.header("Cache-Control", "max-age=3600")
.body("name", equalTo("Darmawan Hadiprasetyo"))
.time(lessThan(5000L))
.extract().response();
如何提取響應體?
例如,這將是我們對之前請求的回應:
{
"name": "alvin",
"role": "SDET"
}以下是我們?nèi)绾翁崛∵@些數(shù)據(jù):
JsonPath responseBody = response.jsonPath();
String fullName = responseBody.getString("name");
String role = responseBody.getString("role");
統(tǒng)一抽象封裝
在大多數(shù)情況下,需要測試許多 API,但前提條件相同,例如 BaseURL、參數(shù)和請求頭等,為了消除代碼中的冗余,我們可以統(tǒng)一抽象封裝一個 RequestSpecification 類作為我們的規(guī)范構(gòu)建器,并在我們的其他測試中重用它,如下所示:
import io.restassured.builder.RequestSpecBuilder;
import io.restassured.path.json.JsonPath;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
import org.testng.annotations.Test;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.lessThan;
public class TestRestAssured {
public static RequestSpecification requestSpecification() {
return new RequestSpecBuilder().setBaseUri("http://127.0.0.1:8000")
.addQueryParam("version", "1.0")
.addHeader("Authorization", "yourauthhere")
.addHeader("Signature", "yoursignaturehere")
.build();
}
@Test
public void testMyApi() {
String jsonBody = "{"email":"dhadiprasetyo@gmail.com","uid":"Jzr0sOORprar10kay6CweZ5FNYP2"}";
Response response = given().spec(requestSpecification())
.body(jsonBody)
.when().post("/getuserdata/")
.then().assertThat().statusCode(200)
.header("Content-Type", "application/json")
.header("Cache-Control", "max-age=3600")
.body("name", equalTo("Darmawan Hadiprasetyo"))
.time(lessThan(5000L))
.extract().response();
JsonPath responseBody = response.jsonPath();
String fullName = responseBody.getString("name");
String linkedIn = responseBody.getString("linkedin");
String role = responseBody.getString("role");
}
}現(xiàn)在,您可以在具有相同前提條件的任何其他需要的測試中重用 requestSpecification() 方法。查看與我們之前代碼的區(qū)別:
// previous
Response response = given().baseUri("http://127.0.0.1:8000")
.queryParam("version", "1.0")
.header("Authorization", "yourauthhere")
.header("Signature", "yoursignaturehere")
.body(jsonBody)
.when().post("/getuserdata/")
// then
Response response = given().spec(requestSpecification())
.body(jsonBody)
.when().post("/getuserdata/")
通過使用 given().spec(),我們的代碼現(xiàn)在變得簡單多了。
結(jié)論
本文簡單介紹了Rest Assured這個開源的接口測試框架是干嘛的,以及如何使用的,希望對大家有幫助。
以上就是Java基于Rest Assured自動化測試接口詳解的詳細內(nèi)容,更多關于Java Rest Assured自動化測試接口的資料請關注腳本之家其它相關文章!
相關文章
java數(shù)據(jù)結(jié)構(gòu)與算法之馬踏棋盤
這篇文章主要為大家詳細介紹了java數(shù)據(jù)結(jié)構(gòu)與算法之馬踏棋盤,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-02-02
javacv開發(fā)詳解之調(diào)用本機攝像頭視頻
這篇文章主要介紹了javacv開發(fā)詳解之調(diào)用本機攝像頭視頻,對javacv感興趣的同學,可以參考下2021-04-04
Java 注冊時發(fā)送激活郵件和激活的實現(xiàn)示例
這篇文章主要介紹了Java 注冊時發(fā)送激活郵件和激活的實現(xiàn)示例的相關資料,需要的朋友可以參考下2017-07-07
SpringBoot 自定義+動態(tài)切換數(shù)據(jù)源教程
這篇文章主要介紹了SpringBoot 自定義+動態(tài)切換數(shù)據(jù)源教程,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12
springboot?+mybatis?使用PageHelper實現(xiàn)分頁并帶條件模糊查詢功能
這篇文章主要介紹了springboot?+mybatis?使用PageHelper實現(xiàn)分頁并帶條件模糊查詢功能,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-02-02

