2016-10-04 9 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レポート生成後:また

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") 
} 

が、運今回時刻:

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を進めます。 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'に' true'の代わりに 'expecting [false]'を追加したようです。 – Rao

+0

いいえ、テストケースに失敗した理由は、XMLのfaultタグに記述された理由になります。しかし、ここで理由は異なるものです。 xpathかgroovyのどちらかを使って、assertでカスタムエラーメッセージを記述できる方法はありますか? – Sarvesh

+0

Hmm。、はっきりしていません。あなたはいくつかの詳細を例文とともに追加しますか? – 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}\"" 

あなたはすぐにerrorCodeが一致しない場合、それがどのように動作するか確認するために、直接hereからそれをテストすることがあります。

関連する問題