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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)