java版介面測試框架 rest-assured的簡單示例
阿新 • • 發佈:2021-06-28
一、前言
REST Assured 是一種 Java DSL,用於簡化構建在 HTTP Builder 之上的基於 REST 的服務的測試。它支援 POST、GET、PUT、DELETE、OPTIONS、PATCH 和 HEAD 請求,可用於驗證和驗證這些請求的響應。
二、新增依賴
maven方式
<dependencies> <!--jUnit5相關的依賴--> <dependency> <groupId>org.junit.jupiter</groupId><artifactId>junit-jupiter-engine</artifactId> <version>5.6.2</version> </dependency> <dependency> <groupId>org.junit.platform</groupId> <artifactId>junit-platform-runner</artifactId><version>1.6.2</version> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>RELEASE</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.platform</groupId> <artifactId>junit-platform-launcher</artifactId> <version>1.6.2</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>5.6.2</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> <version>5.6.2</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-params</artifactId> <version>5.6.2</version> <scope>test</scope> </dependency> <!-- 對yaml序列化和反序列化的庫 --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.8</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.dataformat</groupId> <artifactId>jackson-dataformat-yaml</artifactId> <version>2.9.9</version> </dependency> <!-- allure報告的庫 --> <dependency> <groupId>io.qameta.allure</groupId> <artifactId>allure-junit5</artifactId> <version>RELEASE</version> <scope>test</scope> </dependency> <!--rest-assured相關的包--> <dependency> <groupId>io.rest-assured</groupId> <artifactId>rest-assured</artifactId> <version>4.4.0</version> <scope>test</scope> </dependency> <dependency> <groupId>io.rest-assured</groupId> <artifactId>json-path</artifactId> <version>4.4.0</version> <scope>test</scope> </dependency> <dependency> <groupId>io.rest-assured</groupId> <artifactId>xml-path</artifactId> <version>4.4.0</version> <scope>test</scope> </dependency> <dependency> <groupId>io.rest-assured</groupId> <artifactId>json-schema-validator</artifactId> <version>4.4.0</version> <scope>test</scope> </dependency> </dependencies>
假如jdk的版本是1.9的話,則則是如下匯入方式
<dependency> <groupId>io.rest-assured</groupId> <artifactId>rest-assured-all</artifactId> <version>4.4.0</version> <scope>test</scope> </dependency>
三、Demo演示
1、src/test/java下new一個包(restassureddemo),並且在下面new一個TestDemo.class
2、為了更好的使用restassured,官網推薦Static imports如下幾個包
import static io.restassured.RestAssured.given; import static io.restassured.matcher.RestAssuredMatchers.*; import static org.hamcrest.Matchers.*;
假如還想使用JsonSchemaValidator的話,也推薦靜態匯入
import static io.restassured.module.jsv.JsonSchemaValidator.*;
3、第一個get案例,請求百度
package restassureddemo; import org.junit.jupiter.api.Test; import static io.restassured.RestAssured.given; public class TestDemo { @Test void simpleTest(){ given() .when () .get ("https://www.baidu.com") .then () .log ().all (); } }
- given 設定測試預設(包括請求頭、請求引數、請求體、cookies 等等)
- when 所要執行的操作(GET/POST 請求)
- then 解析結果、斷言
rest-assured官方文件:https://github.com/rest-assured/rest-assured/wiki/GettingStarted