1、初識REST Assured
1.1 描述
在REST Assured的官方GitHub上有這樣一句簡短的描述:
Java DSL for easy testing of REST services
簡約的REST服務測試Java DSL
1.2 優點
官方的README第一句話對REST Assured進行了一個優點的概述,總的意思表達的就是簡單好用
用Java做接口自動化測試首選REST Assured
,具體原因如下:
- 開源
- 簡約的接口測試DSL
- 支持
xml
json
的結構化解析 - 支持
xpath
jsonpath
gpath
等多種解析方式 - 對
spring
的支持比較全面
功能很齊全,部分我自己也還沒有具體用到,瞭解到了方向,需要時隨時查找學習
2、如何使用
- 添加maven依賴
<dependency> <groupId>io.rest-assured</groupId> <artifactId>rest-assured</artifactId> <version>4.0.0</version> <scope>test</scope> </dependency>
2.1 基本三步曲
我們對接口進行測試一般由三步曲:傳參
、發請求
、響應結果斷言
,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']"))
2.2 分步拆解
前提:現有一個
post
請求的登錄接口http://47.103.xxx.133/auth/oauth/token
,
請求體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
2.2.1 given
我們發送請求經常需要帶有參數,使用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). ..
2.2.2 when
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") . ..
2.2.3 then
then後面可以跟斷言,也可以獲取響應值
-
斷言-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.注:這裏的
equalTo
使用的是hamcrest
斷言,不瞭解的小夥伴可參考另外一篇文章:
Junit原生斷言和hamcrest斷言的區別及使用實操演示:
我們將上述的given
、when
、then
結合起來看一下實際運行效果,這裏在運行之前再提一個功能,我們可以在when
和then
後面加上.log().all()
,這樣在運行過程中就可以把請求和響應的信息都打印出來:
-
獲取響應-then().extract().body().path(“code”)
我們可以在then後面利用.extract().body()
來獲取我們想要body的返回值,它們也可以直接接在斷言後面,寫法如下:注意這裏的
body()
不要和請求體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);
運行結果:
3、寫在最後
關於REST Assured
,這裏僅僅算是初步認識,認識它的語法結構和功能,對於更多豐富的用法還需要慢慢探索研究,特別是斷言的部分,是測試工程師最常用最終要的功能之一了,REST Assured
提供了完整的斷言手段,可參考下篇文章:
02-REST Assured的斷言實現
參考鏈接:
REST Assured官方GitHub:https://github.com/rest-assured/rest-assured/wiki/Usage