我在Google和Stackoverflow中搜索过,但我没有找到任何有用的信息,并决定发帖提问。
我收到了JSON API的回复。
{
"CouponCode": [{
"id": 56,
"name": "BlackFriday"
}, {
"id": 58,
"name": "ThanksGiving"
}, {
"id": 62,
"name": "New Year"
}]}
我需要添加一个断言,它会计算总共有3个id和3个名字。
所有ID和名称均不为空。我们不想发送空属性值。
我正在使用SOAP UI开源。请提供准确的代码或准确的参考。
完全断言需要
如果Id是3和3 Ids值是三..如果JSON就像在这种情况下断言将失败。
这个
{
"CouponCode": [{
"id": 56,
"name": "BlackFriday"
}, {
"id": 58,
"name": "ThanksGiving"
}, {
"id": "",
"name": "New Year"
}]}
答案 0 :(得分:1)
以下groovy script
使用json
方式检查预期结果。
在测试用例的groovy script
步骤之后添加rest request
步骤。
用于提取相同内容的Sudo代码。
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"
使用script assertion
进行休息步骤同样可以实现,这将避免额外的groovy脚本步骤。但它可能需要对脚本进行少量更改,如下所示。
如何动态阅读json
响应?
来自脚本断言
使用下面的行并从上面删除固定的jsonText
def jsonText = messageExchange.response.responseContent
从Groovy脚本步骤
//替换下面的其余请求步骤名称
def jsonText = context.expand('${ReplaceStepName#Response}')
如何在脚本中读取预期结果而不是硬编码值的测试用例级别属性?
为id
定义一个测试用例级别属性,比如说EXPECTED_ID_COUNT
,并像你提到的那样提供值3,同样也为name
定义。
//read in script these properties
def expectedIdCount = context.testCase.getPropertyValue('EXPECTED_ID_COUNT') as Integer
答案 1 :(得分:0)