RestAssured接口自动化学习第一天
1:RestAssured环境搭建:
<!-- https://mvnrepository.com/artifact/io.rest-assured/rest-assured -->
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>4.3.0</version>
<scope>test</scope>
</dependency>
<!-- 解析json文件 -->
<!-- https://mvnrepository.com/artifact/io.rest-assured/json-path -->
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>json-path</artifactId>
<version>4.3.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.rest-assured/json-schema-validator -->
<!-- 验证json -->
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>json-schema-validator</artifactId>
<version>4.3.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.rest-assured/xml-path -->
<!--读取xml文档 -->
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>xml-path</artifactId>
<version>4.3.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hamcrest/java-hamcrest -->
<!-- 匹配响应断言 -->
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>java-hamcrest</artifactId>
<version>2.0.0.0</version>
<scope>test</scope>
</dependency>
2:基本入门学习:
引入静态方法:
import static io.restassured.RestAssured.given;
import static org.hamcrest.CoreMatchers.equalTo;
注意如果不引入静态方法,是不可以直接使用give()和equalTo()的
基本语法:
given().log().all().
when().
get("http://www.baidu.com").
then().
statusCode(200);
give()得到一个RequestSpecification
构建测试io.restassured.specification的请求部分,源码方法为:
public static RequestSpecification given() {
return createTestSpecification().getRequestSpecification();
}
given和with的区别在于语法上
with使用:
RequestSpecification with();
with().parameters("firstName", "John", "lastName", "Doe").when().post("/greetXML").then().assertThat().body("greeting.firstName", equalTo("John"));
with().parameters("firstName", "John", "lastName", "Doe").when().post("/greetXML").then().assertThat().body("greeting.firstName", equalTo("John")).and().body("greeting.lastName", equalTo("Doe"));
given使用:
given().parameters("firstName", "John", "lastName", "Doe").when().post("/greetXML").then().assertThat().body(hasXPath("/greeting/firstName", containsString("Jo")));
given().parameters("firstName", "John", "lastName", "Doe").when().post("/greetXML").then().assertThat().body(hasXPath("/greeting/firstName", containsString("Jo")));
given().request().param("name", "John").then().expect().body(containsString("OK")).when().get("/something");
given().
get("http://jsonplaceholder.typicode.com/posts/3").
then().
body("id", equalTo(3)).log().all();
关于log().all()根据放置位置不同,所要打印内容也是不一样的
then()后面主要是用来判断响应的
2.1equalTo用法:
如果判断多个数据,可以使用and连接或者1,直接写在一起
given().get("http://jsonplaceholder.typicode.com/posts/3").then().body("id", equalTo(3)).and().body("title",
equalTo("ea molestias quasi exercitationem repellat qui ipsa sit aut")).log().all()
直接写在一起:
given().get("http://jsonplaceholder.typicode.com/posts/3").then().body("id", equalTo(3), "title",
equalTo("ea molestias quasi exercitationem repellat qui ipsa sit aut")).log().all();
相关文章
- 只会postman单接口测试?这些高级功能你必须掌握
- 详解C#泛型(二) 获取C#中方法的执行时间及其代码注入 详解C#泛型(一) 详解C#委托和事件(二) 详解C#特性和反射(四) 记一次.net core调用SOAP接口遇到的问题 C# WebRequest.Create 锚点“#”字符问题 根据内容来产生一个二维码
- 关于wcf,webservice,webapi或者其他服务或者接口有什么区别 WCF、WebAPI、WebService之间的区别 【转载】HTTP和SOAP完全就是两个不同的协议 WebService学习总结(一)——WebService的相关概念
- 装饰者模式的学习(c#) EF SaveChanges() 报错(转载) C# 四舍五入 保留两位小数(转载) DataGridView样式生成器使用说明 MSSQL如何将查询结果拼接成字符串 快递查询 C# 通过smtp直接发送邮件 C# 带参访问接口,WebClient方式 C# 发送手机短信 文件 日志 写入 与读取
- 利器 | 接口自动化测试框架 RESTAssured 实践(三):对 Response 结果导出
- Pb协议的接口测试
- c#接口interface学习
- axios实战学习——调用城市天气api接口数据实现天气查询案例
- postman实现接口测试详细教程
- 接口测试面试题,等你来看
- Java学习笔记:03面向对象-接口_多态
- PHP 开发 APP 接口 学习笔记与总结 - APP 接口实例 [4] 首页 APP 接口开发方案 ③ 定时读取缓存方式
- 基于ADS1299的可穿戴设备调试之接口含义简析
- 代码审核及考虑代码合并接口
- H3C 用display interface命令显示接口信息
- SVNKit学习——使用低级别的API(ISVNEditor接口)直接操作Repository的目录和文件(五)
- 【紫光同创PGL22G学习】五、以SD卡为例,上手SPI接口使用
- 【紫光同创PGL22G学习】四、以24LC04为例,上手I2C接口使用(附加学习Opencores开源网站使用 和 Fabric Debugger在线调试)
- 字符串查找和替换接口
- 怎样设计接口?
- 记录python接口自动化测试--简单总结一下学习过程(第十目)
- C#显示接口实现和隐式接口实现
- SpringBoot项目使用hutool工具进行HttpClient接口调用的处理(文件上传)