2016-10-04 128 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不匹配。