【问题标题】:SoapUI (Pro) > DataDriven > Message Content Assertion - How to handle decimal valuesSoapUI (Pro) > DataDriven > 消息内容断言 - 如何处理十进制值
【发布时间】:2019-05-08 20:13:27
【问题描述】:

我正在使用 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 了解不多)

【问题讨论】:

  • 嗨,你可能有数据类型的问题,即。来自 excel 的数据可能是字符串,而来自响应的数据可能是浮点数或双精度数。您可以尝试的是,当您在响应中添加断言时,使用“添加断言以使脚本存在”,并在您的脚本中通过执行 .getClass() 获取数据类型。您可能会看到不同之处。

标签: soapui assertion ready-api data-driven


【解决方案1】:

我相信消息内容断言总是寻找字符串。所以你的“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 对其进行改进。但我暂时不考虑这个。一旦你到达那里,就回去问问。 :-)

【讨论】:

    猜你喜欢
    • 2017-09-16
    • 1970-01-01
    • 2021-09-08
    • 1970-01-01
    • 2018-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多