在 REST Assured 的官方 GitHub 上有这样一句简短的描述: Java DSL for easy testing of REST services 简约的 REST 服务测试 Java DSL
REST Assured 官方的 README 第一句话对进行了一个优点的概述,总的意思表达的就是简单好用。那么 REST Assured 有哪些优点,又该如何使用呢?
用 Java 做接口自动化测试首选 REST Assured,具体原因如下:
添加 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,下面来研究下这几个传参方法的区别
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). ..
复制代码
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" + "}")
复制代码
get("/lotto").then().assertThat().body("lotto.lottoId", equalTo(5));
复制代码
given().proxy("127.0.0.1",8888). ..
复制代码
实际运行结果:
given().when().post("http://47.103.xxx.133/auth/oauth/token"). ..
复制代码
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") . ..
复制代码
.. 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() .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 提供的完整断言手段,在后续文章中我们一起探讨。
更多学习资料戳下方!!!
https://qrcode.ceba.ceshiren.com/link?name=article&project_id=qrcode&from=infoQ×tamp=1662366626&author=xueqi
评论