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

时间:2016-03-02 16:01:52

标签: json groovy automated-tests soapui assertions

我在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和名称
  • 查找总ID和名称大小。

如果Id是3和3 Ids值是三..如果JSON就像在这种情况下断言将失败。

这个

{
"CouponCode": [{
    "id": 56,
    "name": "BlackFriday"
}, {
    "id": 58,
    "name": "ThanksGiving"
}, {
    "id": "",
    "name": "New Year"
}]}

2 个答案:

答案 0 :(得分:1)

以下groovy script使用json方式检查预期结果。

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

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

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

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

count(//*:id)

预期结果:

3

同样适用于您的name