【问题标题】:Jmeter - JSON Extractor - issue with extracting the exact decimal valueJmeter - JSON Extractor - 提取精确十进制值的问题
【发布时间】:2023-03-07 04:58:01
【问题描述】:

我正在编写一个测试来断言 json 中的所有金额值在小数点后包含 2 位数字。 从 Json 读取十进制值时,JSON 提取器似乎忽略零。因此,例如 8.50 将是 8.5,因此确切的断言失败。

我添加到虚拟采样器中的 Json 示例

{
    "shop": {
        "book": [
            {
                "author": "Author 1",
                "title": "Title 1",
                "price": 8.50
            },
        ],
        }
    },
}

我的价格响应断言设置为 8.50

Assertion result

在结果中我得到一个失败,因为它表明提取的是 8.5 而不是 8.50

是否需要强制提取器完全按照 json 中的值读取值?

【问题讨论】:

    标签: json jmeter extract


    【解决方案1】:

    JMeter 的 JSON 断言依赖于 JayWay JsonPath,而这个底层库内部有一个错误。

    您可能想report this issue at their GitHub 并且如果/一旦它会得到解决 - create an enhancement request for JMeter 使用这个更新更好的库。

    与此同时,您可以使用JSR223 Assertion 作为解决方法,相关的 Groovy 代码将是:

    def expectedPrice = 8.50
    
    def actualPrice = new groovy.json.JsonSlurper().parse(prev.getResponseData()).shop.book[0].price
    
    if (expectedPrice != actualPrice) {
        AssertionResult.setFailure(true)
        AssertionResult.setFailureMessage('Price mismatch, expected: ' +  expectedPrice + ', got: ' + actualPrice)
    }
    

    更多信息:

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多