我在Google和Stackoverflow中搜索,没有找到任何有用的信息,所以我决定发布问题。
我正在从JSON中的API获得响应。
{
"CouponCode": [{
"id": 56,
"name": "BlackFriday"
}, {
"id": 58,
"name": "ThanksGiving"
}, {
"id": 62,
"name": "New Year"
}]}我需要添加断言,这将计算出总共有3个id和3个名称。
所有ID和名称都不是空的。我们不想发送空属性值。
我正在使用SOAP开源。请提供准确的代码或确切的参考。
确切地说,断言需要
如果Id为3,Id为3,则值为three..if,则JSON与本例类似,断言将失败。
这
{
"CouponCode": [{
"id": 56,
"name": "BlackFriday"
}, {
"id": 58,
"name": "ThanksGiving"
}, {
"id": "",
"name": "New Year"
}]}发布于 2016-03-02 16:55:47
下面的groovy script使用json方法检查预期的结果。
在测试用例中的groovy script步骤之后添加rest request步骤。
提取同样的数独代码。
json文本。如果不想使用固定响应,请从前一步响应中读取它。创建对象。id计数并使用期望值进行检查,并在发生故障时显示错误消息。//for testing using fixed response, you may aslo assign dynamic response.
def jsonText = '''
{
"CouponCode": [{
"id": 56,
"name": "BlackFriday"
}, {
"id": 58,
"name": "ThanksGiving"
}, {
"id": 62,
"name": "New Year"
}]}'''
def coupons = new groovy.json.JsonSlurper().parseText(jsonText).CouponCode
//You may also read these values from test case properties
def expectedIdCount = 3
def expectedNameCount = 3
//assert the expected id count with find all coupon ids count of json response
assert expectedIdCount == coupons.findAll{it.id}.size(), "Coupon id count does not match"
//assert the expected name count with find all coupon names count of json response
assert expectedNameCount == coupons.findAll{it.name}.size(), "Coupon name count does not match"对于rest步骤,也可以使用script assertion实现同样的目的,这将避免额外的groovy脚本步骤。但是,它可能需要对脚本进行很少的修改,如下所示。
如何动态读取json响应?
来自脚本断言的
使用下面的线,并从上面移除固定的jsonText。
def jsonText = messageExchange.response.responseContent
从Groovy脚本步骤//替换def jsonText = context.expand('${ReplaceStepName#Response}')下面的rest请求步骤名
如何读取预期结果的测试用例级属性,而不是脚本中的硬编码值?
为id定义一个测试用例级别的属性,比如EXPECTED_ID_COUNT,并像您提到的那样提供值3,同样,也为name定义。
//read in script these properties
def expectedIdCount = context.testCase.getPropertyValue('EXPECTED_ID_COUNT') as Integer
发布于 2016-03-02 16:28:04
https://stackoverflow.com/questions/35752235
复制相似问题