【问题标题】:JMeter can JSON path assertion plugin match to an array?JMeter可以JSON路径断言插件匹配数组吗?
【发布时间】:2016-02-22 14:07:19
【问题描述】:

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

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

使用 JSON 路径断言插件,我可以单独检查每个值。

$.array[0]

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

【问题讨论】:

    标签: json jmeter jmeter-plugins


    【解决方案1】:

    目前无法查看Can't check an array with JSONPath Assertion. Update JSONPath to 2.1?

    您可以使用Response Assertion 来解决此问题,如下所示:

    1. 将 JSON 路径提取器添加为返回上述 JSON 的请求的子项,并将其配置如下:

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

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

    示例测试计划:

    <?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 指南。

    【讨论】:

    • 这不起作用。当我运行测试时,我收到以下错误:测试失败:变量(数组)预期匹配 /["value1","value2","value3"]/ @Dmitri T
    • 是的,你是对的。但是,您评论中的第一句话是不必要的。 @Dmitri T
    【解决方案2】:

    我可以通过使用.. 语法来实现它。在您的情况下,它将是 $..array ,它将返回

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

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

    【讨论】:

      猜你喜欢
      • 2014-03-27
      • 1970-01-01
      • 2021-02-28
      • 1970-01-01
      • 2013-08-23
      • 1970-01-01
      • 1970-01-01
      • 2018-05-28
      • 1970-01-01
      相关资源
      最近更新 更多