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中文手册

知道、想到、做到、得到
原文地址:https://www.cnblogs.com/Durant0420/p/14862115.html