本文阅读量 次
1. Rest Assured 背景¶
1.1 发起请求¶
1.1.1 如何制定 Query 参数¶
通过 param()
指定 Query 参数
@Test
public void test_Md5CheckSumForTest_ShouldBe098f6bcd4621d373cade4e832627b4f6() {
String originalText = "test";
String expectedMd5CheckSum = "098f6bcd4621d373cade4e832627b4f6";
given().
param("text",originalText).
when().
get("http://md5.jsontest.com").
then().
assertThat().
body("md5",equalTo(expectedMd5CheckSum));
}
1.1.2 如果指定 Path 参数¶
通过 pathParam()
指定路径参数
路径中需要有 placeholder
@Test
public void test_NumberOfCircuits_ShouldBe20_Parameterized() {
String season = "2017";
int numberOfRaces = 20;
given().
pathParam("raceSeason",season).
when().
get("http://ergast.com/api/f1/{raceSeason}/circuits.json").
then().
assertThat().
body("MRData.CircuitTable.Circuits.circuitId",hasSize(numberOfRaces));
}
1.1.3 如何指定 Header 参数¶
通过 given().headers()
或 given().header()
指定
1.1.4 如果发起表单请求¶
1.2 响应的验证和处理¶
1.2.1 如何验证 header 和 cookie¶
@Test
public void whenCallingWelcomeEndpoint_thenCorrect() {
get(uri + "/welcome").then()
.assertThat()
.header("sessionId", notNullValue())
.cookie("token", notNullValue());
}
Response response = get(uri + "/welcome");
String headerName = response.getHeader("sessionId");
String cookieValue = response.getCookie("token");
assertThat(headerName).isNotBlank();
assertThat(cookieValue).isNotBlank();
1.2.2 如果从响应中抽取返回值¶
使用 extract()
方法
Movie result = get(uri + "/movie/" + testMovie.getId()).then()
.assertThat()
.statusCode(HttpStatus.OK.value())
.extract()
.as(Movie.class);
assertThat(result).isEqualTo(testMovie);
// 将整个响应当作字符串进行抽取
String responseString = get(uri + "/movie/" + testMovie.getId()).then()
.assertThat()
.statusCode(HttpStatus.OK.value())
.extract()
.asString();
assertThat(responseString).isNotEmpty();
1.2.2.1 如何从响应中抽取多个返回值¶
1.2.3 如果手动指定响应返回值¶
使用 thenReturn()
方法
Set<Movie> movieSet = new HashSet<>();
movieSet.add(new Movie(1, "movie1", "summary1"));
movieSet.add(new Movie(2, "movie2", "summary2"));
when(appService.getAll()).thenReturn(movieSet);
get(uri + "/movies").then()
.statusCode(HttpStatus.OK.value())
.assertThat()
.body("size()", is(2));
}
1.2.4¶
1.3 认证¶
1.3.1 Basic 认证¶
@Test
public void test_APIWithBasicAuthentication_ShouldBeGivenAccess() {
given().
auth().
preemptive().
basic("username", "password").
when().
get("http://path.to/basic/secured/api").
then().
assertThat().
statusCode(200);
}
1.4 日志方面¶
1.4.1 如何 log 请求部分¶
1.4.2 如何 log 响应部分¶
1.5 其他¶
1.5.1 如果使用 Cookie¶
1.5.2 如何在多个Test间共享参数¶
@Test
public void test_ScenarioRetrieveFirstCircuitFor2017SeasonAndGetCountry_ShouldBeAustralia() {
// First, retrieve the circuit ID for the first circuit of the 2017 season
String circuitId = given().
when().
get("http://ergast.com/api/f1/2017/circuits.json").
then().
extract().
path("MRData.CircuitTable.Circuits.circuitId[0]");
// Then, retrieve the information known for that circuit and verify it is located in Australia
given().
pathParam("circuitId",circuitId).
when().
get("http://ergast.com/api/f1/circuits/{circuitId}.json").
then().
assertThat().
body("MRData.CircuitTable.Circuits.Location[0].country",equalTo("Australia"));
}
1.5.3 使用 ResponseSpecBuilder 复用检查项¶
ResponseSpecification checkStatusCodeAndContentType =
new ResponseSpecBuilder().
expectStatusCode(200).
expectContentType(ContentType.JSON).
build();
@Test
public void test_NumberOfCircuits_ShouldBe20_UsingResponseSpec() {
given().
when().
get("http://ergast.com/api/f1/2017/circuits.json").
then().
assertThat().
spec(checkStatusCodeAndContentType).
and().
body("MRData.CircuitTable.Circuits.circuitId",hasSize(20));
}
1.6 特别备注¶
log()的位置
log() 可以标识在 given() 之后,也可以标识在 when() / then() 之后
1.7 参考¶
- https://techbeacon.com/app-dev-testing/how-perform-api-testing-rest-assured ⧉
- https://github.com/search?p=2&q=FormAuthConfig&type=Code ⧉
- https://chercher.tech/rest-assured/logging-rest-assured ⧉
- http://makeseleniumeasy.com/2020/09/20/rest-assured-tutorial-48-how-to-pass-headers-in-rest-assured-requests/ ⧉
- https://www.baeldung.com/rest-assured-response ⧉
- https://www.baeldung.com/rest-assured-tutorial ⧉
- https://dev.to/eliasnogueira/the-best-way-to-add-a-request-body-to-a-post-request-using-rest-assured-3onb ⧉
- https://howtodoinjava.com/java/library/rest-assured-http-post-and-put-examples/ ⧉
- https://devqa.io/rest-assured-post-request/ ⧉
- https://www.baeldung.com/rest-assured-authentication ⧉
- https://www.baeldung.com/rest-assured-header-cookie-parameter ⧉
- https://plagov.github.io/posts/2018-09-13-running-multiple-parallel-requests-with-restassured-and-testng/ ⧉