首页 > 解决方案 > SoapUI (Pro) > DataDriven > 消息内容断言 - 如何处理十进制值

问题描述

我正在使用 ReadyAPI(SoapUI Pro) 来测试 RestAPI。作为使用 DataSource (DataDriven) 的测试数据,我从 excel 中提供输入(API 输入)。为了测试 API 响应,我正在比较 Excel 的输出(预期结果已经在 excel 中)。为此,我正在使用消息内容断言(因为它很容易而且我没有太多技术性)。现在的问题是,在 excel 中,预期输出是 10.0,但是当我在 SoapUI(Pro)中使用这个 excel 字段进行断言时,它读取 10.0 > 10。但是从 API 响应值是 10.0。所以断言失败了。我试图在 excel 中处理它,但没有运气。

那么在 SoapUI 级别我们可以在 Assertion 中处理它吗?如果无法使用消息内容断言,那么是否可以使用 JsonPath 存在匹配(我对 Json 了解不多)

标签: soapuiassertionready-apidata-driven

解决方案


我相信消息内容断言总是寻找字符串。所以你的“10.0”将被视为一个字符串。输入文件中的内容可能不会以同样的方式处理。

该问题的另一个变体可能是您的输入文件将“10.0”作为预期值,但您从响应中收到“10,0”。

您可能可以通过多种方式解决此问题。我假设您的消息内容断言包含一个变量,它引用您的数据源,例如 ${DataSource#ColumnName} ?小心点!如果你得到一个很长的回应,你的断言将是肯定的,如果在你的回应中的任何地方都可以找到这个值。您最终可能会得到误报。10.0 可能是时间戳之类的一部分,这在 Web 服务响应中是一种常见的数据类型。

我想我会选择使用脚本断言更改您的消息内容断言,并插入如下内容:

// This should be adjusted to match your 
// DataSource name and correct column name
def value1 = context.expand( '${DataSource#value}' )

// Now we need to make sure, that no matter if you 
// receive a number in the format 10.0 or 10,0
// we convert it to one and same thing.
value1 = value1.replace(",",".")

// And in case the 10.0 is being returned as 10
// we need to manually add the .0
if (value1.indexOf(".")==-1) {
    value1 = value1.concat(".0")
}

// Now retrieve the value from the response
// You can replace this line, by rightclicking
// inside the script, and choose Get Data
def value2 = context.expand( '${TestStepName#Response#value}' )

assert value1==value2

现在您将只检查该特定 XML 实体是否具有预期值。这将显着降低误报的风险。

需要进行一些调整,以使其适合您的需求。

如果发生像命名空间变化这样的小变化,这种比较方式将受到破坏。您可以使用 XmlSlurper 对其进行改进。但我暂时不考虑这个。一旦你到达那里,就回去问问。:-)


推荐阅读