8-5接口测试用例设计与编写2 rest-assured

rest-assured

  • 简约的接口测试DSL
  • 支持xml json的结构化解析
  • 支持xpath jsonpath gpath等多种解析方式
  • 对Spring的支持比较前面
  • 底层是httpclient
    加入访问http://localhost:8080/lotto/{id},返回结果如下
{
   "lotto":{
      "lottoId":5,
      "winning-numbers":[2,45,34,23,7,5,3],
      "winners":[
         {
            "winnerId":23,
            "numbers":[2,45,34,23,3,5]
         },
         {
            "winnerId":54,
            "numbers":[52,3,12,11,18,22]
         }
      ]
   }
}

github地址
编写测试用例

public class StartRestAssured {
    @Test
    public void
    lotto_resource_returns_200_with_expected_id_and_winners() {
        when().
                get("/lotto/{id}", 5).//发起一次请求,地址是"/lotto/5"
                then().
                statusCode(200).//返回码是200
                body("lotto.lottoId", equalTo(5),//判断lotto.lottoId是5
                        "lotto.winners.winnerId", hasItems(23, 54));//lotto.winnners.winnerId包含23,54

    }
}

先添加依赖

<!-- https://mvnrepository.com/artifact/io.rest-assured/rest-assured -->
<dependency>
    <groupId>io.rest-assured</groupId>
    <artifactId>rest-assured</artifactId>
    <version>4.0.0</version>
    <scope>test</scope>
</dependency>
posted on 2019-10-12 14:24  singleSpace  阅读(203)  评论(0编辑  收藏  举报