【问题标题】:How to verify only a part of JSON Response value in jmeter如何仅验证 jmeter 中 JSON 响应值的一部分
【发布时间】:2025-12-05 10:05:01
【问题描述】:

响应 JSON 包含以下字段: "scheduledTimestamp":"2017-07-23T18:02:37.000Z"

我需要做的是断言类似:

Verify $.data.scheduledTimestamp contains "2017-07-23T18:02:37"

(忽略毫秒值)

我尝试响应断言为:

Text Response - checked
Contains - checked
Pattern To Test -  "scheduledTimestamp":"${con_res_date}(.+?)"

con_res_date = 2017-07-23T18:02:37

但断言仍然失败。

【问题讨论】:

  • 你试过 scheduleTimestamp":"${con_res_date} 吗?

标签: json jmeter assertion


【解决方案1】:

根据How to Use JMeter Assertions in Three Easy Steps

模式可以是:

  • “等于”或“子字符串”子句的“字符串”
  • “包含”或“匹配”子句的“Perl5 样式”正则表达式

所以你需要修改你的Response Assertion配置如下:

  • 模式匹配规则:Substring
  • 要测试的模式:scheduledTimestamp":"${con_res_date}

【讨论】:

  • 我需要验证时间表时间戳是这样的,同时忽略其中的毫秒值
  • 我通过下面提到的步骤做到了这一点: 1. 添加一个 JSR223 断言而不是响应断言 2. 在 JSR223 断言中,提供这些代码行'import groovy.json.*; def response = prev.getResponseDataAsString(); def json = new JsonSlurper().parseText(response);断言 json.data.scheduledTimestamp.startsWith("${con_res_date}")'
  • 永远不要将 JMeter 变量内联到 groovy 脚本中,因为这可能会导致编译错误。此外,它还破坏了 Groovy 脚本 compilation cache 功能。此外assert 关键字不会让您的采样器失败,您需要使用类似:if (!json.data.scheduledTimestamp.startsWith(vars.get('con_res_date')) { AssertionResult.setFailure(true)}。有关更多信息,请参阅Groovy is the New Black 指南。
最近更新 更多