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

时间:2016-10-13 07:40:02

标签: xml groovy soapui assertions

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

正如您所看到的那样,它有点混乱,我正在尝试连接测试套件日志。

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

1 个答案:

答案 0 :(得分: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:部分;完全自定义消息的可能解决方案是捕获java.lang.AssertionError抛出的assert异常,并在日志中打印所需的消息:

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"
}
相关问题