【问题标题】:How to print all assertion failure messages into HTML report of SoapUI如何将所有断言失败消息打印到 SoapUI 的 HTML 报告中
【发布时间】:2017-03-17 18:51:05
【问题描述】:

我正在使用 groovy 运行测试用例并断言数据。我想将每条失败的消息打印到html junit generate report

示例代码

    import groovy.json.JsonSlurper

    def ResponseMessage = messageExchange.response.responseContent
    def jsonString = new JsonSlurper().parseText(ResponseMessage)

    assert !(jsonString.isEmpty())
    assert jsonString.code == 200
    assert jsonString.status == "success"

    def accountInfo = jsonString.data
    assert !(accountInfo.isEmpty())

    def inc=0

    //CHECKING LANGUAGES IN RESPONSE
    if(accountInfo.languages.id!=null)
    {

           log.info("Language added successfully")
    }
    else
    {

         log.info("Language NOT added.") //want to display this in html report
         inc++

    }

    if(accountInfo.educations!=null)
      {

       log.info("Educations added successfully")
      }
    else
     {

     log.info("Educations NOT added.") //want to display this in html report
     inc++

     } 

assert inc<=0,"API unable to return all parameters, Please check logs"

场景

我在这里做的是,如果测试条件不匹配并转到 ELSE,我将变量 inc 增加 1。所以最后如果失败如果inc>0,我的测试。

举报

在 junit 样式的 html 生成报告中,如果测试失败,它只显示一条名为 API unable to return all parameters, Please check logs 的消息

但我想要的是将每个 IF 条件消息显示到 HTML 报告中,如果任何条件进入 ELSE 部分。

【问题讨论】:

    标签: groovy soapui


    【解决方案1】:

    几个指针:

    • 断言在第一次失败时停止,并且只有此失败消息是 junit 报告的一部分。
    • 话虽如此,用户将不知道当前响应是否有任何进一步的验证失败。
    • 属于if..else 的消息不属于junit report
    • 为了实现这一点,需要收集所有这些消息并最终显示收集到的错误消息。
    • 下面的解决方案使用变量messages 并附加每个失败,以便在最后显示它们。这样,如果 OP 要求,所有故障都可以显示在报告中。
    • 除了assert 语句之外,用户还可以使用以下语句在报告中显示消息

      if (messages) throw new Error(messages.toString())

    脚本断言

    import groovy.json.JsonSlurper
    
    
    //check if the response is empty
    assert context.response, 'Response is empty or null'
    
    def jsonString = new JsonSlurper().parseText(context.response)
    
    def messages = new StringBuffer()
    jsonString.code == 200 ?: messages.append("Code does not match: actual[${jsonString.code}], expected[200]\n")
    jsonString.status == "success" ?: messages.append("Status does not match: actual[${jsonString.status}], expected[success]\n")
    
    def accountInfo = jsonString.data
    accountInfo ?: messages.append('AccountInfo is empty or null\n')
    
    def inc=0
    
    //CHECKING LANGUAGES IN RESPONSE
    if(accountInfo.languages.id) {
       log.info('Language added successfully')
    } else {
        log.error('Language NOT added.') //want to display this in html report
        messages.append('Language not added.\n')
        inc++
    }
    
    if(accountInfo.educations) {
        log.info('Educations added successfully')
    } else {
        log.error('Educations NOT added.') //want to display this in html report
        messages.append('Educations NOT added.\n')
        inc++
    } 
    
    //inc<=0 ?: messages.append('API unable to return all parameters, Please check logs.')
    //if(messages.toString()) throw new Error(messages.toString())
    assert inc<=0, messages.append('API unable to return all parameters, Please check logs.').toString()
    

    【讨论】:

    • 您给定的解决方案对我来说很好。只有一个问题,即使断言失败,我的测试用例也不会失败。直到断言一切正常。但是当我查看 junit html 报告时。它显示状态=通过。我想要的是当断言失败时报告应该失败并在报告中打印所有断言消息以查看哪个失败。
    • 你用过吗?它是否显示了您想要的所有信息?你有没有尝试使用 SoapUI 本身?
    • 刚刚在答案中将if (messages) 更改为if (messages.toString())。你能试试这个更新的答案吗?
    猜你喜欢
    • 2016-10-30
    • 1970-01-01
    • 1970-01-01
    • 2011-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多