JMeter可以将JSON路径断言插件与数组匹配吗?

时间:2016-02-22 14:07:19

标签: json jmeter jmeter-plugins

在我的一个测试中,我需要检查数组中的数据是否与预期结果匹配。 我发送API调用并收到以下JSON响应:

{
"sting": "value",
"another string": "value",
"array": ["value1","value2","value3"]
}

使用JSON Path Assertion插件,我可以单独检查每个值。

$.array[0]

有没有办法在一个断言中评估所有数组的值?

2 个答案:

答案 0 :(得分:2)

目前正在研究Can't check an array with JSONPath Assertion. Update JSONPath to 2.1?是不可能的。

您可以使用Response Assertion进行操作,如下所示:

  1. 将JSON Path Extractor添加为请求的子级,该请求返回JSON以上并按如下方式对其进行配置:

    • 目标变量名称:任何有意义的内容,即array
    • JSON路径表达式:$.array
  2. 在 JSON路径提取器之后添加响应断言,并按如下方式对其进行配置:

    • 申请 - > JMeter变量 - > array
    • 模式匹配规则:Equals
    • 要测试的模式:["value1","value2","value3"] - 确保没有尾随空格,新行等。

      Response Assertion

  3. 示例测试计划:

    <?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">{
    &quot;sting&quot;: &quot;value&quot;,
    &quot;another string&quot;: &quot;value&quot;,
    &quot;array&quot;: [&quot;value1&quot;,&quot;value2&quot;,&quot;value3&quot;]
    }</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">[&quot;value1&quot;,&quot;value2&quot;,&quot;value3&quot;]</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指南。

答案 1 :(得分:0)

我能够通过使用 .. 语法来实现这一点。在您的情况下,它会返回 $..array

[
    "value1",
    "value2",
    "value3"
]

然后,例如,您可以选中“附加断言值”框和“作为正则表达式匹配”,然后执行 \w+ 作为预期值,断言那里有一个词。