2016-12-28 49 views
0

我目前有一个断言脚本,匹配从设置值的响应值。见下文:SoapUI断言匹配任何一方0.05内的字段值

// get the xml response            
def response = messageExchange.getResponseContent()             
// parse it            
def xml = new XmlSlurper().parseText(response)            
// find your node by name            
def node = xml.'**'.find { it.name() == 'total-premium' }            
// assert            
assert node.toString().matches("(0|27.11|0)\\d*"), 'Expected Result: 0 or 27.11 or 0 Actual Result: ' + node  

我想要做的是匹配0.05以下和以上的值之间的值。所以对于这个特定的脚本,如果total-premium的值在27.06到27.16之间,我需要断言是真的。

此刻,断言代码将字段total-premium中的数值与matches("(0|27.11|0)\\d*")中的三个值相匹配。

但是,代替我输入11个值total-premium可能是我希望行assert node.toString().matches("(0|27.11|0)\\d*"), 'Expected Result: 0 or 27.11 or 0 Actual Result: ' + node通过即使字段中的值是0.05加上或减去我手动输入到此脚本中的值。对于这个例子是27.11

有关简要概述,我有〜1000个测试用例,我使用Excel为每个测试用例创建代码和断言,然后导入到SoapUI中。所以我根据Excel算法输入的值自动匹配脚本。

+0

你能显示什么是你的断言的输入和发生了什么?目前尚不清楚。 – Rao

回答

1

您可以使用JUnitpublic static void assertEquals(double expected, double actual, double delta)

import org.junit.Assert 
// ... your code goes here ... 
// new assert 
if(node.toDouble() != 0.0) 
    Assert.assertEquals(27.11, node.toDouble(), 0.05) 
+0

添加了你的代码并得到了这个错误'没有方法的签名:static java.lang.Double.parseDouble()适用于参数类型:(groovy.util.slurpersupport.NodeChild)values:[35.7]可能的解决方案:parseDouble(java .lang.String)' – Ross

+0

@Ross看起来像只能用'.toDouble()' - 看我的改正。 – SiKing

+0

非常感谢 – Ross