SoapUI:断言通过或失败时自定义断言日志文本

SoapUI: Customize assertion log text when assertion passes or fails

我有一个脚本断言,它将 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

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

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

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

类似于:

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

如果日志中 assert 失败,那么当您执行 TestCase 时,您将看到以下内容:

[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"
}