日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

java版接口测试框架 rest-assured的简单示例

發(fā)布時間:2023/12/13 综合教程 25 生活家
生活随笔 收集整理的這篇文章主要介紹了 java版接口测试框架 rest-assured的简单示例 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

一、前言

  REST Assured 是一種 Java DSL,用于簡化構(gòu)建在 HTTP Builder 之上的基于 REST 的服務(wù)的測試。它支持 POST、GET、PUT、DELETE、OPTIONS、PATCH 和 HEAD 請求,可用于驗證和驗證這些請求的響應(yīng)。

二、java語言選rest-assured的首選原因

開源
簡約的接口測試DSL
支持xml json的結(jié)構(gòu)化解析
支持xpath jsonpath gpath等多種解析方式
對spring的支持比較全面

三、添加依賴

  maven方式

    <dependencies>

        <!--jUnit5相關(guān)的依賴-->
        <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相關(guān)的包-->
        <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的話,則則是如下導(dǎo)入方式

<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,官網(wǎng)推薦Static imports如下幾個包  

import static io.restassured.RestAssured.given;
import static io.restassured.matcher.RestAssuredMatchers.*;
import static org.hamcrest.Matchers.*;

  假如還想使用JsonSchemaValidator的話,也推薦靜態(tài)導(dǎo)入

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 設(shè)置測試預(yù)設(shè)(包括請求頭、請求參數(shù)、請求體、cookies 等等)
when 所要執(zhí)行的操作(GET/POST 請求)
then 解析結(jié)果、斷言

rest-assured官方文檔:https://github.com/rest-assured/rest-assured/wiki/GettingStarted

rest-assured中文手冊

知道、想到、做到、得到

總結(jié)

以上是生活随笔為你收集整理的java版接口测试框架 rest-assured的简单示例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。