Java基于Rest?Assured自動化測試接口詳解
前言
不知道大家的項(xiàng)目是否都有對接口API進(jìn)行自動化測試,反正像我們這種小公司是沒有的。由于最近一直被吐槽項(xiàng)目質(zhì)量糟糕,只能研發(fā)自己看看有什么接口測試方案。那么在本文中,我將探索如何使用 Rest Assured 自動化 API 測試,Rest Assured 是一個基于 Java 的流行的用于測試 RESTful API 的庫。
什么是Rest Assured
Rest Assured 是一個基于 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 個主要部分:
1.Given
Given是 API 測試的先決條件,可以在其中設(shè)置測試所需的一切,例如URL、請求頭或參數(shù),或任何需要滿足的先決條件。- 可以在“
Given”中設(shè)置的內(nèi)容:URL、請求頭、請求參數(shù)和請求正文。
2.When
When是實(shí)際向服務(wù)器發(fā)送 HTTP 請求并獲得響應(yīng)的時間。可以在When中定義請求方法,如 GET、POST、PUT 等。
3.Then
Then是您檢查從服務(wù)器獲得的響應(yīng)并確保它符合您的預(yù)期的地方。在這您可以檢查狀態(tài)代碼、響應(yīng)正文、標(biāo)頭或任何其他對您的測試很重要的內(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() 中設(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)時間
.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)一抽象封裝一個 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自動化測試接口詳解的詳細(xì)內(nèi)容,更多關(guān)于Java Rest Assured自動化測試接口的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
SpringBoot Pom文件依賴及Starter啟動器詳細(xì)介紹
這篇文章主要介紹了SpringBoot Pom文件的依賴與starter啟動器的作用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-09-09
spring Security的自定義用戶認(rèn)證過程詳解
這篇文章主要介紹了spring Security的自定義用戶認(rèn)證過程詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-09-09
兩種JAVA實(shí)現(xiàn)短網(wǎng)址服務(wù)算法
這篇文章介紹了兩種JAVA實(shí)現(xiàn)短網(wǎng)址服務(wù)算法,一種是基于MD5碼的,一種是基于自增序列的,需要的朋友可以參考下2015-07-07
JDBC用IDEA連接SQLServer數(shù)據(jù)庫的超實(shí)用教程
JDBC是Java連接數(shù)據(jù)庫的一種接口,它由各個數(shù)據(jù)庫廠商為開發(fā)者提供的接口,要使用它需要到相應(yīng)廠商下載對應(yīng)的jar包,下面這篇文章主要給大家介紹了關(guān)于JDBC用IDEA連接SQLServer數(shù)據(jù)庫的超實(shí)用教程,需要的朋友可以參考下2023-05-05
java面向?qū)ο蠡A(chǔ)_final詳細(xì)介紹
本文將詳細(xì)介紹java final 對象的使用,需要了解更多的朋友可以參考下2012-11-11

