如果请求在使用脚本断言的soapui中失败,如何停止测试用例执行?

时间:2018-03-30 03:03:39

标签: groovy soapui ready-api

如果soap请求失败意味着状态!=" HTTP / 1.1 200 OK" ,testCase应该停止,不再运行其他步骤

有一种方法可以在 groovy 中执行此操作,但我不希望在测试用例中添加额外的测试步骤

            def headers = testRunner.testCase.getTestStepByName("RequestName").httpRequest.response.responseHeaders['#status#']
            if (!headers.contains("HTTP/1.1 200 OK"))
            {
                testRunner.fail("" + headers + "Flow failed")
                testRunner.fail("No futher testSteps will be run inside the current case")
            }

请注意,由于其他一些常规代码限制,我无法更改以下设置

enter image description here

不更改上述选项的原因我有一个测试用例,其中有10个步骤。 1是请求和其他9个步骤验证各种事情。因此,如果我检查错误"中止错误"选项和步骤3失败。那么从4到10的步骤都没有运行。所以请提供一个解决方案,考虑不使用" abort "选项

那么请你为脚本断言提供一个解决方案,而无需勾选此选项。"出错时中止"

由于 testRunner.fail 脚本断言中不可用,并且正常断言(断言0 == 1)不会停止测试用例,除非我们勾选上面的设置。我坚持这个限制

2 个答案:

答案 0 :(得分:2)

您可以通过脚本断言中提供的testRunner变量访问context,所以为什么不能这样:

def httpResponseHeader = messageExchange.responseHeaders
def headers = httpResponseHeader["#status#"]
log.info("Status: " + headers)

if (!headers.contains("HTTP/1.1 200 OK")) { 
    context.testRunner.fail("" + headers + "Flow failed")
    context.testRunner.fail("No futher testSteps will be run inside the current case")
}

答案 1 :(得分:0)

谢谢@craigcaulifield,你的回答对我帮助很大。

很高兴知道testRunner即使在脚本断言中也很有用

现在使用脚本断言,如果请求失败,我们可以停止testCase。

但是,当我们单独运行请求而不是作为测试用例的一部分时,会出现错误

cannot invoke method fail() on null object

此错误是因为

context.testRunner.fail()

testRunner仅在测试用例执行期间可用,而不是单独的testStep执行

所以在这里要克服的是可以处理这两种情况的代码

def responseHeaders=messageExchange.responseHeaders
def status=responseHeaders["#status#"]

// Checking if status is successfully fetched i.e. Response is not empty
if(status!=null)
{
    if(!status.contains("HTTP/1.1 200 OK"))
   {
  // context.testRunner is only available when the request is run as a part of testCase and not individually 
    if(context.testRunner!=null)
    {
          // marking the case fail so further steps are not run
        context.testRunner.fail()
     }
    assert false, "Request did not returned successful status. Expected =  [HTTP/1.1 200 OK] but Actual =  " + status
   }
}
else
{
    if(context.testRunner!=null)
    {
    context.testRunner.fail()
    }
    assert false, "Request did not returned any response or empty response"

}