在 REST Assured 的官网 GitHub 上有这样一句简短的形容: Java DSL for easy testing of REST services 简洁的 REST 服务测试 Java DSL

REST Assured 官网的 README 第一句话对进行了一个长处的概述,总的意思表白的就是简略好用。那么 REST Assured 有哪些长处,又该如何应用呢?

用 Java 做接口自动化测试首选 REST Assured,具体起因如下:

  • 开源
  • 简洁的接口测试 DSL
  • 反对 xml json 的结构化解析
  • 反对 xpath jsonpath gpath 等多种解析形式
  • 对 spring 的反对比拟全面

增加 maven 依赖

<dependency>   <groupId>io.rest-assured</groupId>    <artifactId>rest-assured</artifactId>    <version>4.0.0</version>    <scope>test</scope></dependency>

咱们对接口进行测试个别由三步曲:传参、发申请、响应后果断言,REST Assured给咱们提供了清晰的三步曲,以given、when、then的构造来实现,根本写法如下:

//应用参数given().    param("key1", "value1").    param("key2", "value2").when().    post("/somewhere").then().    body(containsString("OK"))//应用X-Path (XML only) given().    params("firstName", "John", "lastName", "Doe").when().    post("/greetMe").then().    body(hasXPath("/greeting/firstName[text()='John']"))

申请体body如下

{  "password": "elcrD28ZSLLtR0VLs/jERA\u003d\u003d\n",  "grant_type": "password",  "scope": "server",  "userType": 1,  "username": "xxx"}

Request Header 如下:

Headers:    Authorization=Basic c3lzdGVtxxxRlbQ==        Host=47.103.xxx.133        Accept=*/*        Content-Type=application/json; charset=ISO-8859-1

咱们发送申请常常须要带有参数,应用 given() 就能够实现,过后当咱们应用 given() 的时候发现其中有很多传参办法如下:

没错,在传参的办法中蕴含了 param、pathParam、queryParam 和 formParam,上面来钻研下这几个传参办法的区别

  • param
  • 通常咱们都会应用 given().param 办法来传参,REST Assured 会依据 HTTP 办法主动尝试确定哪种参数类型(即查问或表单参数),如果是 GET,则查问参数将主动应用,如果应用 POST,则将应用表单参数;
  • queryParam 和 formParam
  • 有时候在 PUT 或 POST 申请中,须要辨别查问参数和表单参数时,就须要应用queryParam 和 formParam 办法了,具体写法如下:
given().       formParam("formParamName", "value1").       queryParam("queryParamName", "value2").when().       post("/something")
  • pathParam
    应用given时指定申请门路的参数,这个办法很少用到,或者说我自己简直没用到过(可能我的修行还不够,踩坑还太少~);具体写法如下:
given().        pathParam("OAuth", "oauth").        pathParam("accessToken", "token").when().         post("/auth/{OAuth}/{accessToken}").then().         ..
  • header/headers
    常常还须要在申请头中带入参数,这个时候就能够应用header或headers办法,写法如下:
given()       .header("Authorization","Basic c3lzdGVtOxxxbQ==")       .header("Host","47.xxx.xxx.133")
  • 或者用headers将多个参数写在一起:
  • given()     .headers("Authorization","Basic c3lzdGVtxxx3RlbQ==","Host","47.xxx.xxx.133")
  • cookie
    有时候须要在申请中带入cookie,restassured提供了cookie办法来实现:

    given()    .cookie("c_a","aaaaaa")    .cookie("c_b","bbbbbb"). ..
  • contentType
    常常还会设置contentType,最常见的就是application/json了,写法如下:
given().contentType("application/json"). ..//或者given().contentType(ContentType.JSON). ..
  • body
    在POST, PUT 或 DELETE申请中,咱们常常还须要带上申请体body,写法如下:

    given().body("{\n" +              "\t\"password\": \"elcrD28xxxR0VLs/jERA\\u003d\\u003d\\n\",\n" +              "\t\"grant_type\": \"password\",\n" +              "\t\"scope\": \"server\",\n" +              "\t\"userType\": 1,\n" +              "\t\"username\": \"xxx\"\n" +              "}")

也能够用request更为明确的指出是申请body:

given().request().body("{\n" +                "\t\"password\": \"elcrD28xxxR0VLs/jERA\\u003d\\u003d\\n\",\n" +                "\t\"grant_type\": \"password\",\n" +                "\t\"scope\": \"server\",\n" +                "\t\"userType\": 1,\n" +                "\t\"username\": \"xxx\"\n" +                "}")
  • 没有参数
    如果咱们没有参数须要传递,也能够省略掉given():
get("/lotto").then().assertThat().body("lotto.lottoId", equalTo(5));
  • proxy
    有时候咱们须要进行接口的调试,抓包是最罕用的一种形式,rest-assured 提供了 proxy 办法,能够设置代理,写法如下:
given().proxy("127.0.0.1",8888). ..

理论运行后果:

  • when次要用来触发申请,在when前面接着申请URL:
given().when().post("http://47.103.xxx.133/auth/oauth/token"). ..
  • 后面在 given 中咱们设置了很多申请参数,在 when 中也能够设置,只不过要留神的是在申请之前设置;这也比拟好了解,如果再申请之后的话,参数都设置怎么发申请呢?
  given()    .when()        .contentType(ContentType.JSON)        .headers("Authorization","Basic c3lzxxx3RlbQ==","Host","47.xxx.xxx.133")        .request().body("{\n" +            "\t\"password\": \"elcrD28ZSLLtR0VLs/jERA\\u003d\\u003d\\n\",\n" +            "\t\"grant_type\": \"password\",\n" +            "\t\"scope\": \"server\",\n" +            "\t\"userType\": 1,\n" +            "\t\"username\": \"qinzhen\"\n" +            "}")        .post("http://47.xxx.xxx.133/auth/oauth/token")     . ..
  • 断言-then().body()
    then().body() 能够对响应后果进行断言,在 body 中写入断言:

    .. post("http://47.xxx.xxx.133/auth/oauth/token") .then().statusCode(200).body("code",equalTo(1));

其中statusCode(200)是对状态码的断言,判断状态码是否为200; body("code",equalTo(1))是对返回体中的 code 进行断言,要求返回 code值为1 。

实操演示:
咱们将上述的 given、when、then 联合起来看一下理论运行成果,这里在运行之前再提一个性能,咱们能够在 when 和 then 前面加上.log().all(),这样在运行过程中就能够把申请和响应的信息都打印进去:


  • 获取响应-then().extract().body().path("code")
    咱们能够在 then 前面利用 .extract().body() 来获取咱们想要 body 的返回值,它们也能够间接接在断言前面,写法如下:
  • .. .then()      .log().all().statusCode(200).body("code",equalTo(1))      .extract().body().path("code");

实操演示:
演示前再来看一个新的性能,下面咱们再写申请体 body 时时这样的:

body("{\n" +    "\t\"password\": \"elcrD28ZxxxVLs/jERA\\u003d\\u003d\\n\",\n" +    "\t\"grant_type\": \"password\",\n" +    "\t\"scope\": \"server\",\n" +    "\t\"userType\": 1,\n" +    "\t\"username\": \"qinzhen\"\n" +    "}")

看起来有点丑,革新一下;rest-assured 为咱们提供了一个利用 HashMap 来创立json 文件的办法,先把要传的字段放入 hashmap 中,而后用 contentType 指明JSON 就能够了,具体写法如下:

HashMap map = new HashMap();map.put("password","elcrD28ZSLLtR0VLs/jERA\u003d\u003d\n");map.put("grant_type","password");map.put("scope","server");map.put("userType",1);map.put("username","xxx");given()      .headers("Authorization","Basic c3lzdGVtxxxlbQ==","Host","47.xxx.xxx.133")      .contentType(JSON)      .body(map). ..

当初进行残缺的申请,获取返回值 code 并打印:

HashMap map = new HashMap();map.put("password","elcrD28ZSLLtR0VLs/jERA\u003d\u003d\n");map.put("grant_type","password");map.put("scope","server");map.put("userType",1);map.put("username","xxx");Integer code = given()    .headers("Authorization","Basic c3lzdGVtxxxlbQ==","Host","47.xxx.xxx.133")    .contentType(JSON)    .body(map).when()    .log().all().post("http://47.xxx.xxx.133/auth/oauth/token").then()    .log().all().statusCode(200).body("code",equalTo(1))    .extract().body().path("code");System.out.println("返回code的值是:"+code);

运行后果:

对于REST Assured,这里仅仅算是初步意识。意识它的语法结构和性能,对于更多丰盛的用法还须要缓缓摸索钻研,特地是断言的局部,是测试工程师最罕用最终要的性能之一。REST Assured提供的残缺断言伎俩,在后续文章中咱们一起探讨。