1.簡介
RestAssured是一個java接口自動化測試框架,可以發送POST,GET,PUT,DELETE,OPTIONS,PATCH和HEAD請求,並且可以用來驗證和校對這些請求的響應信息。從名稱上來看,它可以很好的支對restful風格的接口進行測試。
github地址:https://github.com/rest-assured/rest-assured
Rest Assured是在HTTP Builder(一個軟件項目)基礎上開發出的一個工具,這個工具能夠採用
Java DSL(領域特定語言,例如groovy)進行基於REST風格的服務的測試,使得這種測試變得簡單。
2 Rest Assured 支持POST, GET, PUT, DELETE, HEAD, PATCH and OPTIONS這幾種類型的請求和響應的驗證。沒有TRACE類型哈。
1)支持JSON數據解析和驗證
2)支持XML數據解析和驗證
3)支持cookies添加
4)支持Header字段設置
5)支持Content Type設置
6)正文Body的設置
7)支持Cookies的設置
8)Status狀態碼相關設置
9)authentication認證設置
2.環境搭建及示例
創建一個maven項目,在pom.xml中加入以下依賴
<dependencies>
<dependency>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
<version>5.0.1</version>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>4.0.0</version>
<scope>test</scope>
</dependency>
<!-- use to parse json document -->
<!-- https://mvnrepository.com/artifact/io.rest-assured/json-path -->
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>json-path</artifactId>
<version>4.0.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.rest-assured/json-schema-validator -->
<!-- validate a json response conforms to a Json schema -->
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>json-schema-validator</artifactId>
<version>4.0.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.rest-assured/xml-path -->
<!-- use to parse xml document -->
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>xml-path</artifactId>
<version>4.0.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.14.3</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hamcrest/java-hamcrest -->
<!-- match rules in assertion -->
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>java-hamcrest</artifactId>
<version>2.0.0.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.14.3</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>4.0.0</version>
<scope>compile</scope>
</dependency>
</dependencies>
簡單例子
import org.testng.annotations.Test;
import static io.restassured.RestAssured.*;
import static org.hamcrest.Matchers.containsString;
/**
* @description:
* @author: ljx
* @time: 2020/6/24 16:56
*/
public class FirstRestAssured {
@Test
public void test() {
//given()這是一個請求對象,get(url)這個是做GET類型請求的發送操作,then()這個是驗證相關的對象,前面get()執行完了就能拿到response對
// 象,然後response對象下有一些驗證相關的類,then()就是驗證類的一個方法
//能夠打印出響應中一切內容(響應頭,cookie,body),而且是比較美觀的格式打印,不管響應內容是json還是xml,還是html。
String url = "https://www.baidu.com";
given().get(url).then().statusCode(200).log().all();
}
}
上邊的代碼完成了請求百度,然後判斷狀態碼並且打印響應信息的操作,可以看出來這種函數式編程十分的便捷,代碼量少
given()這是一個請求對象,get(url)這個是做GET類型請求的發送操作,then()這個是驗證相關的對象,前面get()執行完了就能拿到response對象,然後response對象下有一些驗證相關的類,then()就是驗證類的一個方法。
如果使用常規java編程如下:
/**
* Java原本風格的寫法
*/
@Test
public void testStatusCodeJavaStyle() {
//1. 創建一個RestAssured對象
RestAssured ra = new RestAssured();
//2. 創建一個請求對象
RequestSpecification rs = ra.given();
//3. 發送請求,拿到響應對象
Response res = rs.get("https://www.baidu.com");
//4. 判斷響應狀態碼是不是200
assert res.getStatusCode() == 200;
}
代碼行數比較多。
3.功能詳解
1.請求
2.斷言
3.