当前位置:   article > 正文

REST-assured框架【1】-基础操作_restassured pom

restassured pom

REST-assured是一套由 Java 实现的 REST API 测试框架,语法比较简洁。下面介绍下的基本操作。

一、环境准备

1、pom文件添加依赖

  1. <dependency>
  2. <groupId>io.rest-assured</groupId>
  3. <artifactId>rest-assured</artifactId>
  4. <version>4.4.0</version>
  5. <scope>compile</scope>
  6. </dependency>

2、手动引用静态包

import static io.restassured.RestAssured.*;

3、将RestAssured添加到classPath中

二、RestAssured的基本组成

given:设置场景。 设置测试预设(包括请求头、请求参数、请求体、cookies 等等)

when :执行的操作。所要执行的操作(GET/POST 请求)

注意:如果配置的路径中没有域名,只有相对路径,默认读取本地路径(http://localhost:8080/)

then :产生的结果。解析结果、断言

三、RestAssured样例

1、Get-不带参数的请求

  1. @Test
  2. public void getbase() {
  3. given().
  4. when().get("http://localhost:8080/getBase").
  5. then().log().body();
  6. }

2、​​​​​​​​​​​​​​​​​​​​​Get-带queries参数

  1. @Test
  2. public void getWithProps() {
  3. given().
  4. queryParam("name","Daisy").
  5. queryParam("age","10").
  6. when().get("http://localhost:8080/getWtihQueries").
  7. then().log().body();
  8. }

3、​​​​​​​​​​​​​​​​​​​​​Get-带cookies参数

  1. @Test
  2. public void getWithCookies() {
  3. given().
  4. cookie("login","true").
  5. when().get("/getWithCookies").
  6. then().log().body();
  7. }

4、​​​​​​​post-不带参数

  1. @Test
  2. public void postBase() {
  3. given().
  4. when().post("http://localhost:8080/postBase").
  5. then().log().body();
  6. }

5、​​​​​​​post-带form参数

  1. @Test
  2. public void postWithForms() {
  3. given().
  4. formParam("name","Daisy").
  5. formParam("age","12").
  6. when().post("http://localhost:8080/postWithForms").
  7. then().log().body();
  8. }

6、​​​​​​​post-带json参数

  1. @Test
  2. public void postWithJson() {
  3. String josndata = "{\"name\": \"Daisy\",\"age\": \"10\"}";
  4. given().
  5. body(josndata).contentType(ContentType.JSON)
  6. .when().post("http://localhost:8080/postWithJson")
  7. .then().log().body();
  8. }

7、​​​​​​​​​​​​​​post-提取响应数据

  1. @Test
  2. public void postGetJson() {
  3. String jsonData = "{\"name\": \"Daisy\",\"age\": \"10\"}";
  4. Response response =
  5. given().
  6. contentType(ContentType.JSON).
  7. body(jsonData).
  8. when().post("http://localhost:8080/postGetCookies").
  9. then().extract().response();
  10. System.out.println("响应时间:" + response.time()); //提取响应时间
  11. System.out.println("cookies:" + response.cookies()); //提取cookies
  12. String result = response.getBody().asString();//提取响应body
  13. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/869774
推荐阅读
相关标签
  

闽ICP备14008679号