断言SoapUI测试用例的原因是失败

时间:2016-10-04 12:23:11

标签: groovy junit soapui

我有一个测试用例,我想把它放在断言上。

我需要提供断言失败的原因。

我的XML格式输出如下:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Body>
      <soapenv:Fault>
         <faultcode>soapenv:Server</faultcode>
         <faultstring>XYZ-001: input is wrong</faultstring>
         <detail>
            <con:fault xmlns:con="http://www.bea.com/wli/sb/context">
               <con:errorCode>XYZ-001</con:errorCode>
               <con:reason>input is wrong</con:reason>
               <con:location>
                  <con:node>PipelinePairNode1</con:node>
                  <con:pipeline>PipelinePairNode1_response</con:pipeline>
                  <con:stage>stage1</con:stage>
                  <con:path>response-pipeline</con:path>
               </con:location>
            </con:fault>
         </detail>
      </soapenv:Fault>
   </soapenv:Body>
</soapenv:Envelope>

我想要的结果应该是xml的 faultstring 节点。

为此,我尝试使用此代码进行xpath断言:

declare namespace soapenv='http://schemas.xmlsoap.org/soap/envelope/';
declare namespace con='http://www.bea.com/wli/sb/context';
boolean('/soapenv:Envelope/soapenv:Body/soapenv:Fault/')

我按预期输出为真。 在生成JUnit报告之后,它给出了一些其他原因:

Cancelling due to failed test step

<h3><b>Failure Failed</b></h3><pre>[XPath Match] XPathContains comparison failed for path [declare namespace soapenv='http://schemas.xmlsoap.org/soap/envelope/';
declare namespace con='http://www.bea.com/wli/sb/context';
boolean('/soapenv:Envelope/soapenv:Body/soapenv:Fault/')], expecting [false], actual was [true]
</pre><hr/>

然后我使用以下脚本继续使用Groovy:

def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
def requsetHolder = groovyUtils.getXmlHolder( messageExchange.requestContent )
def responseHolder = groovyUtils.getXmlHolder( messageExchange.responseContent )
def refNum = responseHolder.getNodeValue("soapenv:Envelope/soapenv:Body/soapenv:Fault/")
def testrunner = context.getTestRunner();
if (refNum != null){
    testrunner.fail("soapenv:Envelope/soapenv:Body/soapenv:Fault/faultstring")
}

但这次也没有运气。 junit失败的原因是:

Cancelling due to failed test step

<h3><b>Failure Failed</b></h3><pre>[Script Assertion] net.sf.saxon.trans.XPathException: XPath syntax error at char 46 on line 2 in {...pe/soapenv:Body/soapenv:Fau...}:
 Unexpected token "<eof>" in path expression
</pre><hr/>

所以有什么方法可以在groovy或xpath中使用断言在junit输出中生成我的自定义原因。

1 个答案:

答案 0 :(得分:1)

根据您的问题&amp;评论,这是Script Assertion

  • 该脚本包含如何在报告中显示自定义消息的不同方法。
  • 请按照内嵌评论了解详情。
  • 如果响应为errorCode,则会添加示例代码段,以便对特定Fault元素值进行额外检查。您也可以将其应用于其他元素。

脚本断言

/**
 * The below script should be used as Script Assertion
 * which checks if the response contains Fault, raise error otherwise
 * Once it has fault in it, then it checks for the specific "errorCode", raise error with
 * customized message
 */

//Get the response parsed
def envelope = new XmlSlurper().parseText(context.response)

//There are three approaches to check & and throw customized error message
// if the response does not have Fault. Use one of them
assert envelope.Body.Fault, "Response does not have soap fault"
assert !envelope.Body.Fault.isEmpty(), "Response does not have soap fault"
if (!envelope.Body.Fault) { throw new Error ("Response does not have soap fault") }

//Further check for specific errorCode in the soap fault
def expectedErrorCode = 'XYZ-001'
def actualErrorCode = envelope.'**'.find {it.name() == 'errorCode' } as String

log.info "Actual code is : $actualErrorCode"
assert expectedErrorCode == actualErrorCode, "Soap fault should not have \"${expectedErrorCode}\""

如果errorCode不匹配,您可以直接从here快速测试,看看它的行为方式。