栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

REST Assured 64 - How To Pass Value From One API To Another API Using TestNG – ITestContext

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

REST Assured 64 - How To Pass Value From One API To Another API Using TestNG – ITestContext

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 依赖库


    org.testng
    testng
    7.3.0
    test

添加 rest assured 依赖库


    io.rest-assured
    rest-assured
    4.3.3
    test

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("{rn" + 
				"    "firstname" : "Jim",rn" + 
				"    "lastname" : "Brown",rn" + 
				"    "totalprice" : 111,rn" + 
				"    "depositpaid" : true,rn" + 
				"    "bookingdates" : {rn" + 
				"        "checkin" : "2018-01-01",rn" + 
				"        "checkout" : "2019-01-01"rn" + 
				"    },rn" + 
				"    "additionalneeds" : "Breakfast"rn" + 
				"}")
		.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("{rn" + 
				"    "firstname" : "Amod",rn" + 
				"    "lastname" : "Mahajan",rn" + 
				"    "totalprice" : 222,rn" + 
				"    "depositpaid" : true,rn" + 
				"    "bookingdates" : {rn" + 
				"        "checkin" : "2022-01-01",rn" + 
				"        "checkout" : "2022-01-01"rn" + 
				"    },rn" + 
				"    "additionalneeds" : "Breakfast"rn" + 
				"}")
		.when()
			.put()
		.then()
			.log()
			.all();
			
	}
	
	
 
}

输出:

[RemoteTestNG] detected TestNG version 7.0.1
Request method:	POST
Request URI:	https://restful-booker.herokuapp.com/booking
Proxy:			
Request params:	
Query params:	
Form params:	
Path params:	
Headers:		Accept=**
				Content-Type=application/json; charset=UTF-8
cookies:		
Multiparts:		
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
===============================================
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/290691.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号