目录
一、开发环境
- 开发工具:idea 2021
- Maven
- Rest-Assured
- testng
- jdk1.8
- RestAssured接口文档地址:https://www.javadoc.io/doc/io.rest-assured/rest-assured/latest/index.html
- Hamcrest:hamcrest 是一款用于校验的 Java 的单元测试框架,可以组合创建灵活的表达的匹配器进行断言。
二、创建第一个RestAssured接口测试
1.1创建Maven项目
在idea上创建好Maven项目
2.2解决Maven相关依赖
pom.xml文件
<dependencies>
<!-- REST Assured -->
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>5.2.0</version>
<scope>test</scope>
</dependency>
<!-- json-path -->
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>json-path</artifactId>
<version>5.2.0</version>
<scope>test</scope>
</dependency>
<!-- xml-path -->
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>xml-path</artifactId>
<version>5.2.0</version>
<scope>test</scope>
</dependency>
<!-- json-schema-validator -->
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>json-schema-validator</artifactId>
<version>5.2.0</version>
<scope>test</scope>
</dependency>
<!--Spring Mock Mvc-->
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>spring-mock-mvc</artifactId>
<version>5.2.0</version>
<scope>test</scope>
</dependency>
<!--testng-->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.14.3</version>
<scope>test</scope>
</dependency>
<!--hamcrest-->
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>java-hamcrest</artifactId>
<version>2.0.0.0</version>
<scope>test</scope>
</dependency>
</dependencies>
2.3创建第一个接口例子
- 在src/test/java下创建demo包,新建一个BasicFeatures类
package demo;
import org.testng.annotations.Test;
import static io.restassured.RestAssured.given;
public class BasicFeatures {
@Test
public void testStatusCode(){
given().
get("https://www.baidu.com").
then().
statusCode(200);
}
}
执行测试方法,结果:
- given()是RestAssured类下一个方法,所以官方文档强烈推荐使用静态导入。
- get()方法,用来发送请求
- then()又是一个方法,其实given()前面还有一个with()方法,暂时不用去计较什么意思
- statusCode()用来判断响应状态码是否等于200
三、打印响应内容和body解析
1. 打印全部响应内容到控制台
/**
* 验证状态码,并将响应内容全部打印到控制台
*/
@Test
public void testStatusCode(){
given().
get("https://www.baidu.com").
then().
statusCode(200).
log().all();
}
2. body解析
- 通过equalTo()方法来断言验证字段对应的值是否正确
- 使用body()方法来获取响应正文的内容
/**
* body解析
* 通过equalTo()方法来断言验证字段对应的值是否正确
*/
@Test
public void testEqualToMethod(){
given().
get("http://jsonplaceholder.typicode.com/posts/3").
then().
body("id", equalTo(3)).
log().all();
}
3. 多字段条件断言写法
/**
* 多个字段断言
*/
@Test
public void testMultiCondition(){
String url = "https://jsonplaceholder.typicode.com/posts/1";
given().
get(url).
then().
body("id", equalTo(1)).and().body("title", equalTo("delectus aut autem1")).
log().all();
}
- 单字段多值断言
使用方法:body(“key”,hasItems(“value1”,“value2”,“value3”))
/**
* 单个字段多值断言
*/
@Test
public void testMultiCondition2(){
String url = "https://jsonplaceholder.typicode.com/posts";
given().
get(url).
then().
body("id", hasItems(1, 2, 3)).
log().all();
}
四、请求参数和请求头
1.请求中带一个请求参数
/**
* 请求中带有参数
*/
@Test
public void testRequestParam(){
given().
param("userId",1).
when().
get(url).
then().
statusCode(200).log().all();
}
2.请求中带有多个请求参数
/**
* 请求中带有多个参数
*/
@Test
public void testRequestParam2(){
//构造一个Map对象,用来存多个参数和值
Map<String, String> params = new HashMap<>();
params.put("userId", "2");
params.put("id", "14");
given().
params(params).
when().
get(url).
then().
statusCode(200).log().all();
}
3. 请求中带有响应头
/**
* 请求中带有请求头信息
*/
@Test
public void testHeaders(){
//构造Map对象,用来存header的值
Map<String, String> headers = new HashMap<>();
headers.put("Content-Type", "application/json; charset=utf-8");
headers.put("accept-encoding", "gzip,deflate");
headers.put("accept-language", "zh-CN");
given().
headers(headers).
when().log().all(). //打印请求相关参数
get(url).
then().
statusCode(200).log().all(); //打印返回相关参数
}
一个多或个参数、一个或多个请求头字段、多个参数多个请求字段的场景,基本上涵盖了HTTP请求的每个场景。
五、Post请求和获取不同的响应内容格式
1.post请求
/**
* post请求
*/
@Test
public void testPost(){
String url = "https://reqres.in/api/users";
Map<String, String> params = new HashMap<>();
params.put("name","Antony123");
params.put("job", "tester");
Map<String, String> headers = new HashMap<>();
headers.put("Content-Type", "text/html");
given().
params(params).
headers(headers).
when().log().all().
post(url).
then().
statusCode(201).
log().all();
}
2.获取不同格式的响应内容
- 以字符串类型获取响应内容
/**
* 以字符串类型获取响应内容
*/
@Test
public void testRespAsString(){
String respString = get("https://reqres.in/api/users/2").asString();
System.out.println(respString);
}