2016-10-04 86 views
1

我有一个测试用例,我想在其中放置断言。断言带有失败原因的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/> 

那么有没有什么办法的,我可以在任何常规或XPath使用断言产生在JUnit输出我的自定义原因。

+0

看起来你是从你的描述中在'Xpath Assertion'中添加'expectcting [false]'而不是'true'? – Rao

+0

不,测试用例失败的原因应该是xml故障标记中提及的原因。但是这里的原因是不同的。有没有什么办法可以通过使用xpath或groovy来声明自定义错误信息。 – Sarvesh

+0

嗯,不清楚。你想用例子添加更多的细节吗? – Rao

回答

1

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

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

脚本断言

/** 
* 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}\"" 

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