Groovy 如何在运行测试步骤时运行脚本断言?

Groovy 如何在运行测试步骤时运行脚本断言?,groovy,soapui,Groovy,Soapui,我不熟悉groovy脚本和soap UI。我在测试步骤中添加了脚本断言。然而,当我运行测试用例时,脚本断言并没有运行。我必须手动运行它来验证我的响应是否正确。 有人能帮我吗? 我的groovy脚本测试断言: import groovy.json.JsonSlurper //grab response def response = messageExchange.response.responseContent //Convert to JsonSluper to access data de

我不熟悉groovy脚本和soap UI。我在测试步骤中添加了脚本断言。然而,当我运行测试用例时,脚本断言并没有运行。我必须手动运行它来验证我的响应是否正确。 有人能帮我吗? 我的groovy脚本测试断言:

import groovy.json.JsonSlurper

//grab response
def response = messageExchange.response.responseContent

//Convert to JsonSluper to access data
def list = new JsonSlurper().parseText(response)


//check delegates are in one session per timeslot
for (i = 0; i < list.size(); i++) {

    // find all items for this delegate in this timeslot
    def itemsForThisDelegateInThisTimeslot = list.findAll {element -> element.delegateId == list[i].delegateId && element.agendaItemId== list[i].agendaItemId}

    log.info(list[i].delegateId)

    // there should not be more than 1
    if(itemsForThisDelegateInThisTimeslot.size() > 1) {
        log.info(list[i].delegateId + "Delegate already assigned to a workshop at same time");

     //Assert fail in execution
       throw new Error ('Fail')
    }
}
import groovy.json.JsonSlurper
//抓取响应
def response=messageExchange.response.responseContent
//转换为JsonSluper以访问数据
def list=new JsonSlurper().parseText(响应)
//检查学员是否在每个时间段的一个会话中
对于(i=0;ielement.delegateId==list[i]。delegateId&&element.agendaItemId==list[i]。agendaItemId}
log.info(列表[i].delegateId)
//不应超过1个
if(ItemsForThisDelegateIntHistTimeSlot.size()>1){
log.info(列表[i].delegateId+“已同时分配到车间的代表”);
//断言执行失败
抛出新错误('失败')
}
}

首先,此脚本断言中没有断言。查找Groovy断言

如果您正在“断言”脚本是通过还是失败,您需要类似于

assert (response.contains('Something from the response'));

如果通过,则台阶变为绿色。如果失败,它会变红

嗯,看起来你在抛出一个异常,当这个步骤失败的时候。我不会以这种方式使用异常。有一个异常用于捕获和报告代码问题。不是测试失败

关于异常,请查阅Groovy Try and Catch

至于这个运行(或不运行)的测试用例。我怀疑它正在运行,但由于您没有断言任何内容,因此无法看到结果


有没有注意到屏幕底部的脚本日志选项卡?当测试步骤运行时,所有log.info语句都将出现在此处。我建议清除此日志(在脚本日志窗口中单击鼠标右键…),然后再次运行测试用例,并查看脚本日志中的一些日志消息。

谢谢Chris,我将尝试添加与上述类似的断言。运行测试时,我看不到日志信息语句。我必须手动运行脚本以查看其通过或失败。您应该可以看到任何日志记录。您已经找到脚本日志选项卡了,是吗?我仍在试图理解为什么在整体运行测试时脚本断言没有运行。您可以通过向测试步骤添加第二个脚本断言来强制您的步骤失败,并放置类似于“assert(false==true);”的内容,该内容应始终失败。嗨,Chris,很抱歉回复太晚,是的,我知道日志选项卡在哪里,我可以看到在手动运行该步骤的脚本断言时创建的日志。嗨,当您作为一个整体运行测试脚本时,应该能够看到相同的情况。
assert (someBooleanVar == true);