【问题标题】:How to run script assertion while running the test step?如何在运行测试步骤时运行脚本断言?
【发布时间】:2019-04-29 16:32:38
【问题描述】:

我是 groovy 脚本和肥皂 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')
    }
}

【问题讨论】:

    标签: groovy soapui


    【解决方案1】:

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

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

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

    assert (someBooleanVar == true);
    

    如果通过,则步骤变为绿色。如果失败,它将变为红色。

    恕我直言,当步骤失败时,您似乎正在引发异常。我不会以这种方式使用异常。有一个例外来捕获和报告代码问题。不是测试失败。

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

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

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

    【讨论】:

    • 谢谢克里斯,我会尝试像你上面提到的那样添加断言。运行测试时我看不到我的日志信息语句。我必须手动运行脚本才能看到它通过或失败。
    • 您应该会看到任何日志记录。您找到了脚本日志选项卡,不是吗?我仍在尝试理解为什么在整个运行测试时脚本断言没有运行。您可以通过向测试步骤添加第二个脚本断言并输入类似“assert (false == true);”之类的内容来强制您的步骤失败,这应该总是失败。
    • 嗨,克里斯,抱歉回复晚了,是的,我知道日志选项卡在哪里,当我手动运行该步骤的脚本断言时,我可以看到我的日志已创建。
    • 您好,当您将测试脚本作为一个整体运行时,您应该能够看到相同的结果。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-29
    • 1970-01-01
    • 2021-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多