REST Assured 系列汇总 之 REST Assured 64 - How To Pass Value From One API To Another API Using TestNG – ITestContext
介绍
有时我们需要将一个 API 的输出做为另外一个 API 的输入。例如:从 Create Booking API 中获取一个 booking id,如果我们需要获取,更新, 部分更新,删除这个 booking,那么就得传 booking id 到这些 APIs。
我们将用 TestNG 做为测试框架,就很容易方便用例之间的数据共享,我没有发现到 Junit 有类似的功能。
前提条件
添加 testNG 依赖库
<!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.3.0</version>
<scope>test</scope>
</dependency>
添加 rest assured 依赖库
<!-- https://mvnrepository.com/artifact/io.rest-assured/rest-assured -->
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>4.3.3</version>
<scope>test</scope>
</dependency>
Interface ITestContext
根据 TestNG 文档, ITestContext 是一个接口,定义一个测试上下文,包含所有的测试运行信息。这个上下文的实例被传递到测试监听者,可以查询到相关信息。
ITestContext 是一个非常强大的接口,它提供了许多有用的方法。本文我们了解一下 ITestContext 的两个重要方法,setAttribute(java.lang.String name, java.lang.Object value) 和 getAttribute(java.lang.String name)
setAttribute()
setAttribute(atttributeName, attributeValue) – 设置一个自定义的属性. 类似添加一个键值对的元素到一个 Map。属性值的类型可以是任意类型,这就是它接收 Object 类型为属性值的原因。
getAttribute()
getAttribute(attributeName) – 获取一个属性的值,返回值类型是一个 Object.
How to use ITestContext?
ITestContext 接口继承自 IAttributes 接口,ITestContext 引用一旦创建,就可以以一个参数传递给加了 @Test 注释标签的方法。首先要通过 setAttribute() 方法存储上下文,然后就可能通过 getAttribute() 方法来获取上下文内容。
我们将用 Restful – Booker APIs 作为例子,相关的 API 例子在之前的文章中也有涉及到。
代码:
import org.testng.ITestContext;
import org.testng.annotations.Test;
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
public class ShareDataUsingITestContext {
@Test
public void createBooking(ITestContext context)
{
int bookingId = RestAssured
.given()
.log()
.all()
.baseUri("https://restful-booker.herokuapp.com/")
.basePath("booking")
.contentType(ContentType.JSON)
.body("{\r\n" +
" \"firstname\" : \"Jim\",\r\n" +
" \"lastname\" : \"Brown\",\r\n" +
" \"totalprice\" : 111,\r\n" +
" \"depositpaid\" : true,\r\n" +
" \"bookingdates\" : {\r\n" +
" \"checkin\" : \"2018-01-01\",\r\n" +
" \"checkout\" : \"2019-01-01\"\r\n" +
" },\r\n" +
" \"additionalneeds\" : \"Breakfast\"\r\n" +
"}")
.when()
.post()
.then()
.log()
.all()
.extract()
.jsonPath()
.get("bookingid");
// Storing data in a context to use for other tests
context.setAttribute("bookingId", bookingId);
}
@Test
public void updateBooking(ITestContext context)
{
// Retrieving required data from context
int bookingId = (int) context.getAttribute("bookingId");
RestAssured
.given()
.log()
.all()
.baseUri("https://restful-booker.herokuapp.com/")
.basePath("booking/"+bookingId)
.header("Authorization","Basic YWRtaW46cGFzc3dvcmQxMjM=")
.contentType(ContentType.JSON)
.body("{\r\n" +
" \"firstname\" : \"Amod\",\r\n" +
" \"lastname\" : \"Mahajan\",\r\n" +
" \"totalprice\" : 222,\r\n" +
" \"depositpaid\" : true,\r\n" +
" \"bookingdates\" : {\r\n" +
" \"checkin\" : \"2022-01-01\",\r\n" +
" \"checkout\" : \"2022-01-01\"\r\n" +
" },\r\n" +
" \"additionalneeds\" : \"Breakfast\"\r\n" +
"}")
.when()
.put()
.then()
.log()
.all();
}
}
输出:
[RemoteTestNG] detected TestNG version 7.0.1
Request method: POST
Request URI: https://restful-booker.herokuapp.com/booking
Proxy: <none>
Request params: <none>
Query params: <none>
Form params: <none>
Path params: <none>
Headers: Accept=*/*
Content-Type=application/json; charset=UTF-8
Cookies: <none>
Multiparts: <none>
Body:
{
"firstname": "Jim",
"lastname": "Brown",
"totalprice": 111,
"depositpaid": true,
"bookingdates": {
"checkin": "2018-01-01",
"checkout": "2019-01-01"
},
"additionalneeds": "Breakfast"
}
HTTP/1.1 200 OK
Server: Cowboy
Connection: keep-alive
X-Powered-By: Express
Content-Type: application/json; charset=utf-8
Content-Length: 195
Etag: W/"c3-mr+ToVU3dK7bNqxVpsnhD1SC7cM"
Date: Wed, 27 Jan 2021 06:23:23 GMT
Via: 1.1 vegur
{
"bookingid": 11,
"booking": {
"firstname": "Jim",
"lastname": "Brown",
"totalprice": 111,
"depositpaid": true,
"bookingdates": {
"checkin": "2018-01-01",
"checkout": "2019-01-01"
},
"additionalneeds": "Breakfast"
}
}
Request method: PUT
Request URI: https://restful-booker.herokuapp.com/booking/11
Proxy: <none>
Request params: <none>
Query params: <none>
Form params: <none>
Path params: <none>
Headers: Authorization=Basic YWRtaW46cGFzc3dvcmQxMjM=
Accept=*/*
Content-Type=application/json; charset=UTF-8
Cookies: <none>
Multiparts: <none>
Body:
{
"firstname": "Amod",
"lastname": "Mahajan",
"totalprice": 222,
"depositpaid": true,
"bookingdates": {
"checkin": "2022-01-01",
"checkout": "2022-01-01"
},
"additionalneeds": "Breakfast"
}
HTTP/1.1 200 OK
Server: Cowboy
Connection: keep-alive
X-Powered-By: Express
Content-Type: application/json; charset=utf-8
Content-Length: 171
Etag: W/"ab-+iqLT0fOvVL3GfV0ed6NlH849m8"
Date: Wed, 27 Jan 2021 06:23:25 GMT
Via: 1.1 vegur
{
"firstname": "Amod",
"lastname": "Mahajan",
"totalprice": 222,
"depositpaid": true,
"bookingdates": {
"checkin": "2022-01-01",
"checkout": "2022-01-01"
},
"additionalneeds": "Breakfast"
}
PASSED: createBooking(org.testng.TestRunner@1990a65e)
PASSED: updateBooking(org.testng.TestRunner@1990a65e)
===============================================
Default test
Tests run: 2, Failures: 0, Skips: 0
===============================================
===============================================
Default suite
Total tests run: 2, Passes: 2, Failures: 0, Skips: 0
===============================================