接口测试框架rest-assured入门

rest-assured

什么是rest-assured

REST Assured is a Java DSL for simplifying testing of REST based services built on top of HTTP Builder.
It supports POST, GET, PUT, DELETE, OPTIONS, PATCH and HEAD requests and can be used to validate and verify the response of these requests.

翻译一下定义:

  1. 是一个Java DSL
  2. 目的是简化REST服务的测试
  3. 基于HTTP协议

官网:http://rest-assured.io/
GitHub:https://github.com/rest-assured/rest-assured

延伸:什么是DSL

DSL - Domain Specific Language 领域特定语言,通常被定义为一种特别针对某类特殊问题的计算机语言,它不打算解决其领域外的问题。其基本思想是“求专不求全”,不像通用目的语言那样目标范围涵盖一切软件问题,而是专门针对某一特定问题的计算机语言。DSL的首要目的,是使程序尽可能地接近业务领域中的问题,从而消除不必要的间接性和复杂性。合理且恰当地选择语法形式永远是构造DSL的重中之重。

特性

总结一下,就是快速、简便。包括:

  • 构造http请求
  • 认证登录
  • 参数封装
  • JSON解析
  • 结果验证

把手弄脏

  1. 发送GET请求 + 验证JSON
    @Test
    public void testDoubanGet() {
        String url = "https://api.douban.com/v2/book/1220562";
        ValidatableResponse response =
                given().
                when().
                    get(url).
                then().
                    assertThat().
                        body("author[0]",equalTo("[日] 片山恭一")).
                        and().
                        statusCode(200);
    }
  1. POST登录
    @Test
    public void testLogin(){
        HashMap paramMap = new HashMap();
        paramMap.put("identifier","admin");
        paramMap.put("password","123456");
        paramMap.put("next","");

        Response response =
                given().
                    params(paramMap).
                when().
                    post("http://test-web-site/auth/");
        System.out.println(response.getHeaders().toString());
    }

再延伸:Hamcrest

接口测试框架rest-assured入门_第1张图片
image.png

Hamcrest is a framework for writing matcher objects allowing 'match' rules to be defined declaratively. There are a number of situations where matchers are invaluble, such as UI validation, or data filtering, but it is in the area of writing flexible tests that matchers are most commonly used. This tutorial shows you how to use Hamcrest for unit testing.

http://www.infoq.com/cn/articles/dsl-discussion
https://techbeacon.com/how-perform-api-testing-rest-assured
https://developers.douban.com/wiki/?title=guide
https://testerhome.com/topics/7060
https://testerhome.com/topics/6451
https://testerhome.com/topics/8743

你可能感兴趣的:(接口测试框架rest-assured入门)