【问题标题】:SoapUI: Customize assertion log text when assertion passes or failsSoapUI:当断言通过或失败时自定义断言日志文本
【发布时间】:2016-10-13 07:40:02
【问题描述】:

我有一个脚本断言,它将 XML 响应中的值 A 与值 B、C 和 D 进行比较。如果值 A 等于值 B、C 或 D 中的任何一个,则断言通过,否则失败。

// get the xml response                                             
def response = messageExchange.getResponseContent()                                                 
// parse it                                             
def xml = new XmlSlurper().parseText(response)                                              
// find your node by name                                               
def node = xml.'**'.find { it.name() == 'total-premium' }                                               
// assert                                               
(assert node.toString().matches("(36.476|38.395|40.315)\\d*"))

目前,对于通过的断言,我在日志中得到了这个字符串:

Step 1 [TestStep_0001] OK: took 2296 ms

一个失败的断言我在日志中得到了这个字符串:

 -> [Script Assertion] assert node.toString().matches((36.476|38.395|40.315)\\d*")           |    |          |           |    37.6033658 false           37.6033658

如您所见,它有点混乱,我正在尝试整理测试套件日志。

有没有办法在日志中设置自定义响应?

【问题讨论】:

    标签: xml groovy soapui assertions


    【解决方案1】:

    我不确定这是否是您要查找的内容,但您可以自定义您的assert,将字符串作为消息传递,当assert 失败时将显示或记录该消息。

    类似:

    assert node.toString().matches("(36.476|38.395|40.315)\\d*"), 'Assert fail custom message'
    

    这样,当您执行 TestCase 时,如果 assert 在日志中失败,您将看到:

    [Script Assertion] Assert fail custom message. Expression: node.toString().matches((36.476|38.395|40.315)\d*)
    

    根据评论更新:

    您没有删除assert 消息的一部分的方法,如果如上一个解决方案中所示,使用assert expression, 'custom fail message' 的自定义消息对于您的情况来说还不够(因为您似乎还想删除Expression: 部分在结果消息中);完全自定义消息的一种可能解决方案是捕获assert 引发的java.lang.AssertionError 异常并在日志中打印所需的消息:

    def node = 3
    try {
        assert node.toString().matches("(36.476|38.395|40.315)\\d*")
    } catch (AssertionError e) {
        log.info "Your custom message without expression"
    }
    

    【讨论】:

    • 有没有办法隐藏表达式部分?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-10
    • 2011-05-27
    • 2021-12-31
    相关资源
    最近更新 更多