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

2.請求頭

3.請求體

現(xiàn)在我們用Rest Assured這個(gè)框架來測試下上面postman的這個(gè)接口。
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() 中設(shè)置前置條件
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)代碼、標(biāo)頭、正文和響應(yīng)時(shí)間
.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();
如何提取響應(yīng)體?
例如,這將是我們對之前請求的回應(yīng):
{
"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)一抽象封裝一個(gè) 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這個(gè)開源的接口測試框架是干嘛的,以及如何使用的,希望對大家有幫助。
以上就是Java基于Rest Assured自動化測試接口詳解的詳細(xì)內(nèi)容,更多關(guān)于Java Rest Assured自動化測試接口的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
java數(shù)據(jù)結(jié)構(gòu)與算法之馬踏棋盤
這篇文章主要為大家詳細(xì)介紹了java數(shù)據(jù)結(jié)構(gòu)與算法之馬踏棋盤,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02
javacv開發(fā)詳解之調(diào)用本機(jī)攝像頭視頻
這篇文章主要介紹了javacv開發(fā)詳解之調(diào)用本機(jī)攝像頭視頻,對javacv感興趣的同學(xué),可以參考下2021-04-04
Java 注冊時(shí)發(fā)送激活郵件和激活的實(shí)現(xiàn)示例
這篇文章主要介紹了Java 注冊時(shí)發(fā)送激活郵件和激活的實(shí)現(xiàn)示例的相關(guān)資料,需要的朋友可以參考下2017-07-07
SpringBoot 自定義+動態(tài)切換數(shù)據(jù)源教程
這篇文章主要介紹了SpringBoot 自定義+動態(tài)切換數(shù)據(jù)源教程,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12
springboot?+mybatis?使用PageHelper實(shí)現(xiàn)分頁并帶條件模糊查詢功能
這篇文章主要介紹了springboot?+mybatis?使用PageHelper實(shí)現(xiàn)分頁并帶條件模糊查詢功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-02-02
mybatis項(xiàng)目兼容mybatis-plus問題
這篇文章主要介紹了mybatis項(xiàng)目兼容mybatis-plus問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-02-02

