我们如何验证 jmeter 中的响应数据模式(仅列值)?

How can we verify response data schema(column values only) in jmeter?

我需要在 jmeter 中验证 API 模式。 我的 API 响应数据是

[{
    "snid": "1",
    "subject": "Automation",
    "state": null,
    "country": null,
    "contact_name": "John",
    "email": "John@gmail.com",
    "phone": "402-221-9999"
}, {
    "snid": "2",
    "subject": "Testing",
    "state": null,
    "country": null,
    "contact_name": "Smith",
    "email": "jmstauch@leoadaly.com",
    "phone": "402-111-2222"
}]

所以我想验证它是否包含

的响应消息
{
    "snid": "",
    "subject": "",
    "state": ,
    "country": ,
    "contact_name": "",
    "email": "",
    "phone": ""
}

我只需要验证字段的列名和顺序。 我使用了 response assertions contains 但它不验证顺序,还 JSON path assertion 验证对象值而不是参数(或者它验证完整的消息)但在我的情况下我需要验证确切的模式.

请帮忙

我建议使用 JSR223 Assertion and i.e. Groovy JSON Schema for this as currently there are no relevant bundled test elements and/or plugins to cover this functionality. Make sure you have the .jar in JMeter Classpath

有关在 JMeter 中使用 Groovy 脚本的详细信息,请参阅 Groovy Is the New Black 文章。

你可以使用map.keySet(),它会给你地图上的所有钥匙。

[ "snid",
  "subject",
  "state",
  "country",
  "contact_name",
  "email",
  "phone"
]

我找到了一个解决你的问题 首先将对象解析为 json slurper

def slurper = new groovy.json.JsonSlurper();

def response = slurper.parseText(prev.getResponseDataAsString());

def testdata = response[0].keySet() as List;

这会将测试数据保存为

["snid","subject","state","country","contact_name","email","phone"]