断言带有失败原因的 SoapUI 测试用例
Assert a SoapUI test case with reason of failure
我有一个测试用例,我想对其进行断言。
我需要提供断言失败的原因。
我的 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/')
并且我将 true 作为预期输出。
在生成 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 输出中生成我的自定义原因。
根据您的问题和评论,这里是 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
不匹配它的行为如何。
我有一个测试用例,我想对其进行断言。
我需要提供断言失败的原因。
我的 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/')
并且我将 true 作为预期输出。 在生成 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 输出中生成我的自定义原因。
根据您的问题和评论,这里是 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
不匹配它的行为如何。