JMeter 可以 JSON 路径断言插件匹配数组吗?
JMeter can JSON path assertion plugin match to an array?
在我的一项测试中,我需要检查数组中的数据是否与预期结果匹配。 我发送 API 呼叫并收到以下 JSON 响应:
{
"sting": "value",
"another string": "value",
"array": ["value1","value2","value3"]
}
使用 JSON 路径断言插件,我可以单独检查每个值。
$.array[0]
有没有办法在一个断言中评估数组的所有值?
目前无法查看 Can't check an array with JSONPath Assertion. Update JSONPath to 2.1?。
您可以使用 Response Assertion 解决此问题,如下所示:
添加 JSON 路径提取器作为 JSON 以上 returns 请求的子项,并按如下方式配置:
- 目标变量名称:任何有意义的,即
array
- JSON 路径表达式:
$.array
- 目标变量名称:任何有意义的,即
在 JSON 路径提取器 之后添加响应断言 并配置如下:
- 应用于 -> JMeter 变量 ->
array
- 模式匹配规则:
Equals
要测试的模式:
["value1","value2","value3"]
- 确保没有尾随空格、换行等。
- 应用于 -> JMeter 变量 ->
示例测试计划:
<?xml version="1.0" encoding="UTF-8"?>
<jmeterTestPlan version="1.2" properties="2.8" jmeter="2.13 r1665067">
<hashTree>
<TestPlan guiclass="TestPlanGui" testclass="TestPlan" testname="Test Plan" enabled="true">
<stringProp name="TestPlan.comments"></stringProp>
<boolProp name="TestPlan.functional_mode">false</boolProp>
<boolProp name="TestPlan.serialize_threadgroups">false</boolProp>
<elementProp name="TestPlan.user_defined_variables" elementType="Arguments" guiclass="ArgumentsPanel" testclass="Arguments" testname="User Defined Variables" enabled="true">
<collectionProp name="Arguments.arguments"/>
</elementProp>
<stringProp name="TestPlan.user_define_classpath"></stringProp>
</TestPlan>
<hashTree>
<ThreadGroup guiclass="ThreadGroupGui" testclass="ThreadGroup" testname="Thread Group" enabled="true">
<stringProp name="ThreadGroup.on_sample_error">continue</stringProp>
<elementProp name="ThreadGroup.main_controller" elementType="LoopController" guiclass="LoopControlPanel" testclass="LoopController" testname="Loop Controller" enabled="true">
<boolProp name="LoopController.continue_forever">false</boolProp>
<stringProp name="LoopController.loops">1</stringProp>
</elementProp>
<stringProp name="ThreadGroup.num_threads">1</stringProp>
<stringProp name="ThreadGroup.ramp_time">1</stringProp>
<longProp name="ThreadGroup.start_time">1456157004000</longProp>
<longProp name="ThreadGroup.end_time">1456157004000</longProp>
<boolProp name="ThreadGroup.scheduler">false</boolProp>
<stringProp name="ThreadGroup.duration"></stringProp>
<stringProp name="ThreadGroup.delay"></stringProp>
</ThreadGroup>
<hashTree>
<kg.apc.jmeter.samplers.DummySampler guiclass="kg.apc.jmeter.samplers.DummySamplerGui" testclass="kg.apc.jmeter.samplers.DummySampler" testname="jp@gc - Dummy Sampler" enabled="true">
<boolProp name="WAITING">true</boolProp>
<boolProp name="SUCCESFULL">true</boolProp>
<stringProp name="RESPONSE_CODE">200</stringProp>
<stringProp name="RESPONSE_MESSAGE">OK</stringProp>
<stringProp name="REQUEST_DATA">Dummy Sampler used to simulate requests and responses
without actual network activity. This helps debugging tests.</stringProp>
<stringProp name="RESPONSE_DATA">{
"sting": "value",
"another string": "value",
"array": ["value1","value2","value3"]
}</stringProp>
<stringProp name="RESPONSE_TIME">${__Random(50,500)}</stringProp>
<stringProp name="LATENCY">${__Random(1,50)}</stringProp>
<stringProp name="CONNECT">${__Random(1,5)}</stringProp>
</kg.apc.jmeter.samplers.DummySampler>
<hashTree>
<com.atlantbh.jmeter.plugins.jsonutils.jsonpathextractor.JSONPathExtractor guiclass="com.atlantbh.jmeter.plugins.jsonutils.jsonpathextractor.gui.JSONPathExtractorGui" testclass="com.atlantbh.jmeter.plugins.jsonutils.jsonpathextractor.JSONPathExtractor" testname="jp@gc - JSON Path Extractor" enabled="true">
<stringProp name="VAR">array</stringProp>
<stringProp name="JSONPATH">$.array</stringProp>
<stringProp name="DEFAULT"></stringProp>
<stringProp name="VARIABLE"></stringProp>
<stringProp name="SUBJECT">BODY</stringProp>
</com.atlantbh.jmeter.plugins.jsonutils.jsonpathextractor.JSONPathExtractor>
<hashTree/>
<ResponseAssertion guiclass="AssertionGui" testclass="ResponseAssertion" testname="Response Assertion" enabled="true">
<collectionProp name="Asserion.test_strings">
<stringProp name="-1728402013">["value1","value2","value3"]</stringProp>
</collectionProp>
<stringProp name="Assertion.test_field">Assertion.response_data</stringProp>
<boolProp name="Assertion.assume_success">false</boolProp>
<intProp name="Assertion.test_type">8</intProp>
<stringProp name="Assertion.scope">variable</stringProp>
<stringProp name="Scope.variable">array</stringProp>
</ResponseAssertion>
<hashTree/>
</hashTree>
</hashTree>
</hashTree>
</hashTree>
</jmeterTestPlan>
如果您的响应可能有所不同,即数组成员顺序发生变化,您可能需要考虑基于脚本的断言,即 JSR223 Assertion 而不是
有关在测试中使用 JMeter 断言的全面信息,请参阅 How to Use JMeter Assertions in Three Easy Steps 指南。
我能够通过使用 ..
语法来实现它。在你的情况下,它将是 $..array
,这将是 return
[
"value1",
"value2",
"value3"
]
然后你可以,例如,检查“附加断言值”框和“作为正则表达式匹配”,然后做 \w+
作为预期值,断言那里有一个词。