2016-12-28 44 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