java版接口测试框架 rest-assured的简单示例
生活随笔
收集整理的這篇文章主要介紹了
java版接口测试框架 rest-assured的简单示例
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、前言
REST Assured 是一種 Java DSL,用于簡化構建在 HTTP Builder 之上的基于 REST 的服務的測試。它支持 POST、GET、PUT、DELETE、OPTIONS、PATCH 和 HEAD 請求,可用于驗證和驗證這些請求的響應。
二、java語言選rest-assured的首選原因
開源
簡約的接口測試DSL
支持xml json的結構化解析
支持xpath jsonpath gpath等多種解析方式
對spring的支持比較全面
三、添加依賴
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
rest-assured中文手冊
知道、想到、做到、得到
總結
以上是生活随笔為你收集整理的java版接口测试框架 rest-assured的简单示例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 特洛伊木马脚本linux,手动查杀特洛伊
- 下一篇: linux常见分区格式,linux 分区