SOAP UI 断言帮助:验证总属性和总值

SOAP UI Assertion Help : To validate Total attribute and Total Value

我在 Google 和 Whosebug 中进行了搜索,但没有找到任何有用的信息,因此决定 post 提问。

我在 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"
}]}

有几种可能solutions。最简单的是使用 XPath 断言;请记住,在内部,SoapUI 会将所有内容转换为 XML(如果可以)。

count(//*:id)

预期结果:

3

同样适用于您的 name

下面groovy script使用json的方式来检查预期的结果。

在测试用例的 rest request 步骤之后添加 groovy script 步骤。

用于提取相同内容的 Sudo 代码。

  1. 阅读 json 文本。如果您不想使用固定响应,请从上一步响应中读取它。创建对象。
  2. 确保您的 ID 和名称的计数符合预期。您也可以在测试用例自定义属性中定义它们,以防不想使用固定值并在脚本中每次都更改。
  3. 找到所有 id 计数并检查预期值,并在失败时显示错误消息。
  4. 与第 3 步类似,对名称进行断言。
//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