如何在SoapUI中失败脚本断言?

sch*_*omm 1 groovy soapui assertions http-headers

当一个变量有另一个值而不是定义时,我试图让脚本断言失败.我的目标:如果TestStep失败,将TestStep标记为红色.请注意,我正在使用TestStep的脚本断言 - 而不是单独的Groovy-Script TestStep.我的脚本看起来像这样:

httpResponseHeader = messageExchange.responseHeaders
contentType = httpResponseHeader["Content-Type"]
log.info("Content-Type: " + contentType)

if (contentType != "[image/jpeg]"){
    log.info("ERROR! Response is not an image.")
    //Script Assertion should fail.
    //TestStep should be marked red.
} else {
    log.info("OK! ResponseType is an image.")
}
Run Code Online (Sandbox Code Playgroud)

有什么办法,如何让脚本断言取决于属性?我试过使用getStatus()方法,但这只适用于testrunner对象.不幸的是,testRunner-object不能在关于这篇文章的脚本断言中使用:http://forum.soapui.org/viewtopic.php? f = 2&t = 2494#p9107

Rao*_*Rao 10

需要断言以便测试失败或根据条件自动传递.

def httpResponseHeader = messageExchange.responseHeaders
def contentType = httpResponseHeader["Content-Type"]
log.info("Content-Type: " + contentType)
assert contentType.get(0) == "image/jpeg","Content Type is not an image"
Run Code Online (Sandbox Code Playgroud)

编辑:早先注意如何抛出错误,假设你有其余的.现在根据输入改变了最后一行代码.

  • 好.你也可以用另一种方式使测试失败`if(contentType.get(0)!="image/jpeg"){throw new Error('Content Type不是图像')}`.但是这在不同的上下文中使用,例如,您需要进行多次检查而不停止第一次失败,并且在脚本结束时使其失败.但断言在第一次失败时停止,并没有看其余的.因此,如果条件而不是断言,则使用if,如果上述ifs中的任何一个失败,则最后抛出错误.希望这可以帮助. (2认同)