Ross Ross - 1 month ago 20
Groovy Question

SoapUI: Customize assertion log text when assertion passes or fails

I have a Script Assertion that compares value A from the XML response to value B, C, and D. If Value A is equal to any of value B, C, or D then the assertion passes, else it fails.

// 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*"))


At the moment for a passed assertion I get this string in the log:

Step 1 [TestStep_0001] OK: took 2296 ms


And a failed assertion I get this string in the log:

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


As you can see it is a bit messy and I'm trying neaten up the test suite logs.

Is there a way to set customized response in the logs?

Answer

I'm not sure that this is what you're looking for, but you can customize your assert passing a string as a message which will be displayed or logged when assert fails.

Something like:

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

This way when you execute the TestCase if the assert fail in the log you will see this instead:

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

UPDATE BASED ON COMMENT:

You don't have a method to remove a part of the assert message, if as shown in the previous solution the custom message using assert expression, 'custom fail message' it's not enough for your case (since seems that you also want to remove Expression: part in the result message); a possible solution to customize totally your message is to catch the java.lang.AssertionError exception thrown by the assert and print in the log the desired message:

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