如何将 CSV 文件中的变量与 JSON 响应进行比较以确定 JMETER 中的断言案例

How to compare a variable from CSV file with the JSON response to determine an assert case in JMETER

我是 jmeter 的初学者,需要一些帮助。我从端点收到了 JSON 响应,该响应包含一个用户集合。我只想循环访问用户集合以匹配一个值。如果匹配则通过断言案例。

回复:

{
"company": {
    "data" : {
        "key": "SSL-121-TEST"
        "userResult": [
          {
            "id": "1001",
            "consent": "Yes",
            "status": [
              {
                "code":"1",
                "reason": "Not Submitted"
              }
            ]
          },
          {
            "id": "1002",
            "consent": "Yes",
            "status": [
              {
                "code":"2",
                "reason": "No Result Found"
              }
            ]
          },
          {
            "id": "1003",
            "consent": "Yes",
            "status": [
              {
                "code":"1",
                "reason": "Not Submitted"
              }
            ]
          }
      ]
    }
}

我正在从 csv 文件中读取 原因 & 想与响应的每个 userResult 进行比较。如果任何原因匹配,则 assert case 应该通过。

  1. JSR223 Assertion 添加为 returns 上述 JSON

    请求的子项
  2. 将以下代码放入“脚本”区域:

    if (!new groovy.json.JsonSlurper().parse(prev.getResponseData()).company.data.userResult.status.reason.collect { it -> it.get(0) }.contains(vars.get('reason'))) {
        AssertionResult.setFailure(true)
        AssertionResult.setFailureMessage('Reason ' + vars.get('reason') + ' was not found in the response')
    }
    
  3. 就是这样,如果在响应的“原因”中找不到 reason 变量的值 - 请求将被标记为失败。

更多信息: