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();

参考文档:https://testerhome.com/topics/7060

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员路同学

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值