JMeter学习-007-JMeter 断言实例之一 - 响应断言(转载)

转载:https://www.cnblogs.com/fengpingfan/p/4755308.html

JMeter学习-007-JMeter 断言实例之一 - 响应断言

 

之前的文章中已经对如何录制 web 的请求进行了详细的描述,敬请参阅:JMeter学习-004-WEB脚本入门实战

同时,我们的手机应用(例如:京东、天猫、唯品会、携程、易迅 等等 App)所发出的请求,也可进行录制,只需要将手机连接的无线网络代理到本机(以 iPhone 为例,依次点击 【设置】/【无线局域网】/【连接网络信息,即网络右侧详细信息符号】/【HTTP代理】/手动】,填写 服务器IP、端口 对应填写 JMeter 机器的 IP、JMeter HTTP 代理端口 即可),并将对应的端口设置为 JMeter 中设置的端口后,即可对相应的操作进行录制操作。非常简单,在此不再赘述。

当我们执行脚本的时候,需要依据一定的条件去判断脚本是否执行成功,例如响应信息(响应头、主体、响应数据、响应代码、大小等)、响应时间。JMeter 中也响应的给我们提供了相应的断言,大多数通用的断言 JMeter 均已给出,例如:Response Assertion、Size Assertion、MD5Hex Assertion等等。JMeter 提供的断言列表如下所示:

以下就以易迅网App的一个简单的类目搜索,对其添加响应断言和大小断言,来简单的演示一下 JMeter 中的断言。具体的操作步骤如下:

  1. 获取类目 手机数码-手机通讯-苹果 的 HTTP 请求(通过 JMeter HTTP代理服务器 录制即可获得,在此略)
  2. 添加如下所示 Response Assertion(前四)、Size Assertion(后一):
    1. 01 - 判断类目搜索结果状态码
    2. 02 - 判断类目搜索结果响应数据包含苹果
    3. 03 - 判断类目搜索结果中不包含信息
    4. 04 - 判断类目搜索结果响应头信息中包含 Server: nginx
    5. 05 - 判断响应结果大小(body)
  3. 添加查看结果树

添加响应断言:

右键单击 HTTP请求(手机数码-手机通讯-苹果),依次选择:【添加/断言/响应断言】,如下图所示:

响应断言添加页面如下所示:

要测试的响应字段:取样区域,也可简单理解为取样的数据源。单选 与 复选按钮可同时选择。

模式匹配规则:判断的关系依据。单选 与 复选按钮可同时选择,选择了复选框后,为前面关系条件的否。例如前面选择了 包括,同时勾选了 否,则实际判断关系依据为:不包括。

要测试的模式:可简单理解为判断的值。

PS:一个断言仅可判断一种类型的断言,无法混杂判断,即 要测试的响应字段、模式匹配规则、要测试的模式 在此断言中都是相同,若模式匹配规则不同,则需新增响应断言。

 

最终的脚本样式如下所示:

脚本源码为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<?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="接口自动化测试用例 - 非登录态" 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="用户定义的变量" enabled="true">
        <collectionProp name="Arguments.arguments"/>
      </elementProp>
      <stringProp name="TestPlan.user_define_classpath"></stringProp>
    </TestPlan>
    <hashTree>
      <ThreadGroup guiclass="ThreadGroupGui" testclass="ThreadGroup" testname="001-类目搜索验证" enabled="true">
        <stringProp name="ThreadGroup.on_sample_error">continue</stringProp>
        <elementProp name="ThreadGroup.main_controller" elementType="LoopController" guiclass="LoopControlPanel" testclass="LoopController" testname="循环控制器" 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">1419564228000</longProp>
        <longProp name="ThreadGroup.end_time">1419564228000</longProp>
        <boolProp name="ThreadGroup.scheduler">false</boolProp>
        <stringProp name="ThreadGroup.duration"></stringProp>
        <stringProp name="ThreadGroup.delay"></stringProp>
      </ThreadGroup>
      <hashTree>
        <HTTPSamplerProxy guiclass="HttpTestSampleGui" testclass="HTTPSamplerProxy" testname="手机数码-手机通讯-苹果" enabled="true">
          <elementProp name="HTTPsampler.Arguments" elementType="Arguments" guiclass="HTTPArgumentsPanel" testclass="Arguments" enabled="true">
            <collectionProp name="Arguments.arguments">
              <elementProp name="districtId" elementType="HTTPArgument">
                <boolProp name="HTTPArgument.always_encode">false</boolProp>
                <stringProp name="Argument.name">districtId</stringProp>
                <stringProp name="Argument.value">29357</stringProp>
                <stringProp name="Argument.metadata">=</stringProp>
                <boolProp name="HTTPArgument.use_equals">true</boolProp>
              </elementProp>
              <elementProp name="exAppTag" elementType="HTTPArgument">
                <boolProp name="HTTPArgument.always_encode">false</boolProp>
                <stringProp name="Argument.name">exAppTag</stringProp>
                <stringProp name="Argument.value">2045191607</stringProp>
                <stringProp name="Argument.metadata">=</stringProp>
                <boolProp name="HTTPArgument.use_equals">true</boolProp>
              </elementProp>
            </collectionProp>
          </elementProp>
          <stringProp name="HTTPSampler.domain">mb.51buy.com</stringProp>
          <stringProp name="HTTPSampler.port"></stringProp>
          <stringProp name="HTTPSampler.connect_timeout"></stringProp>
          <stringProp name="HTTPSampler.response_timeout"></stringProp>
          <stringProp name="HTTPSampler.protocol">http</stringProp>
          <stringProp name="HTTPSampler.contentEncoding">GB2312</stringProp>
          <stringProp name="HTTPSampler.path">/json.php?mod=Search&act=page&p=1&path=706188t706189&districtId=29357&areacode=1&dtype=list%7Cpage%7Cclasses&appSource=android&appVersion=45</stringProp>
          <stringProp name="HTTPSampler.method">POST</stringProp>
          <boolProp name="HTTPSampler.follow_redirects">true</boolProp>
          <boolProp name="HTTPSampler.auto_redirects">false</boolProp>
          <boolProp name="HTTPSampler.use_keepalive">true</boolProp>
          <boolProp name="HTTPSampler.DO_MULTIPART_POST">false</boolProp>
          <boolProp name="HTTPSampler.monitor">false</boolProp>
          <stringProp name="HTTPSampler.embedded_url_re"></stringProp>
        </HTTPSamplerProxy>
        <hashTree>
          <HeaderManager guiclass="HeaderPanel" testclass="HeaderManager" testname="HTTP信息头管理器" enabled="true">
            <collectionProp name="HeaderManager.headers">
              <elementProp name="Charset" elementType="Header">
                <stringProp name="Header.name">Charset</stringProp>
                <stringProp name="Header.value">UTF-8</stringProp>
              </elementProp>
              <elementProp name="Content-Type" elementType="Header">
                <stringProp name="Header.name">Content-Type</stringProp>
                <stringProp name="Header.value">application/x-www-form-urlencoded</stringProp>
              </elementProp>
              <elementProp name="Accept-Encoding" elementType="Header">
                <stringProp name="Header.name">Accept-Encoding</stringProp>
                <stringProp name="Header.value">gzip</stringProp>
              </elementProp>
              <elementProp name="User-Agent" elementType="Header">
                <stringProp name="Header.name">User-Agent</stringProp>
                <stringProp name="Header.value">Dalvik/1.6.0 (Linux; U; Android 4.4.2; GT-I9502 Build/KOT49H)</stringProp>
              </elementProp>
            </collectionProp>
          </HeaderManager>
          <hashTree/>
          <ResponseAssertion guiclass="AssertionGui" testclass="ResponseAssertion" testname="01 - 判断类目搜索结果状态码" enabled="true">
            <collectionProp name="Asserion.test_strings">
              <stringProp name="71131476">"errno":0</stringProp>
            </collectionProp>
            <stringProp name="Assertion.test_field">Assertion.response_data</stringProp>
            <boolProp name="Assertion.assume_success">false</boolProp>
            <intProp name="Assertion.test_type">2</intProp>
          </ResponseAssertion>
          <hashTree/>
          <ResponseAssertion guiclass="AssertionGui" testclass="ResponseAssertion" testname="02 - 判断类目搜索结果响应数据包含苹果" enabled="true">
            <collectionProp name="Asserion.test_strings">
              <stringProp name="1065923">苹果</stringProp>
            </collectionProp>
            <stringProp name="Assertion.test_field">Assertion.response_data</stringProp>
            <boolProp name="Assertion.assume_success">false</boolProp>
            <intProp name="Assertion.test_type">2</intProp>
          </ResponseAssertion>
          <hashTree/>
          <ResponseAssertion guiclass="AssertionGui" testclass="ResponseAssertion" testname="03 - 判断类目搜索结果中不包含信息" enabled="true">
            <collectionProp name="Asserion.test_strings">
              <stringProp name="1425729940">我的博客地址: http://www.cnblogs.com/fengpingfan/</stringProp>
            </collectionProp>
            <stringProp name="Assertion.test_field">Assertion.response_headers</stringProp>
            <boolProp name="Assertion.assume_success">false</boolProp>
            <intProp name="Assertion.test_type">6</intProp>
          </ResponseAssertion>
          <hashTree/>
          <ResponseAssertion guiclass="AssertionGui" testclass="ResponseAssertion" testname="04 - 判断类目搜索结果响应头信息中包含 Server: nginx" enabled="true">
            <collectionProp name="Asserion.test_strings">
              <stringProp name="1066132337">Server: nginx</stringProp>
            </collectionProp>
            <stringProp name="Assertion.test_field">Assertion.response_headers</stringProp>
            <boolProp name="Assertion.assume_success">false</boolProp>
            <intProp name="Assertion.test_type">2</intProp>
          </ResponseAssertion>
          <hashTree/>
          <SizeAssertion guiclass="SizeAssertionGui" testclass="SizeAssertion" testname="05 - 判断响应结果大小(body)" enabled="true">
            <stringProp name="Assertion.test_field">SizeAssertion.response_data</stringProp>
            <stringProp name="SizeAssertion.size">4000</stringProp>
            <intProp name="SizeAssertion.operator">3</intProp>
          </SizeAssertion>
          <hashTree/>
        </hashTree>
      </hashTree>
      <ResultCollector guiclass="AssertionVisualizer" testclass="ResultCollector" testname="断言结果" enabled="true">
        <boolProp name="ResultCollector.error_logging">false</boolProp>
        <objProp>
          <name>saveConfig</name>
          <value class="SampleSaveConfiguration">
            <time>true</time>
            <latency>true</latency>
            <timestamp>true</timestamp>
            <success>true</success>
            <label>true</label>
            <code>true</code>
            <message>true</message>
            <threadName>true</threadName>
            <dataType>true</dataType>
            <encoding>false</encoding>
            <assertions>true</assertions>
            <subresults>true</subresults>
            <responseData>false</responseData>
            <samplerData>false</samplerData>
            <xml>false</xml>
            <fieldNames>false</fieldNames>
            <responseHeaders>false</responseHeaders>
            <requestHeaders>false</requestHeaders>
            <responseDataOnError>false</responseDataOnError>
            <saveAssertionResultsFailureMessage>false</saveAssertionResultsFailureMessage>
            <assertionsResultsToSave>0</assertionsResultsToSave>
            <bytes>true</bytes>
            <threadCounts>true</threadCounts>
          </value>
        </objProp>
        <stringProp name="filename"></stringProp>
      </ResultCollector>
      <hashTree/>
      <ResultCollector guiclass="ViewResultsFullVisualizer" testclass="ResultCollector" testname="察看结果树" enabled="true">
        <boolProp name="ResultCollector.error_logging">false</boolProp>
        <objProp>
          <name>saveConfig</name>
          <value class="SampleSaveConfiguration">
            <time>true</time>
            <latency>true</latency>
            <timestamp>true</timestamp>
            <success>true</success>
            <label>true</label>
            <code>true</code>
            <message>true</message>
            <threadName>true</threadName>
            <dataType>true</dataType>
            <encoding>false</encoding>
            <assertions>true</assertions>
            <subresults>true</subresults>
            <responseData>false</responseData>
            <samplerData>false</samplerData>
            <xml>false</xml>
            <fieldNames>false</fieldNames>
            <responseHeaders>false</responseHeaders>
            <requestHeaders>false</requestHeaders>
            <responseDataOnError>false</responseDataOnError>
            <saveAssertionResultsFailureMessage>false</saveAssertionResultsFailureMessage>
            <assertionsResultsToSave>0</assertionsResultsToSave>
            <bytes>true</bytes>
            <threadCounts>true</threadCounts>
          </value>
        </objProp>
        <stringProp name="filename"></stringProp>
      </ResultCollector>
      <hashTree/>
    </hashTree>
  </hashTree>
</jmeterTestPlan>

脚本执行结果如下所示:

那么当断言失败的时候是什么样子呢?我们依据下两图进行相应的修改:

在此执行的结果如下图所示,通过点击对应的断言,可查看断言的详细信息:

当然您也可以添加断言结果(右键单击测试计划、线程组、HTTP请求,以此选择【添加/监听器/断言结果】添加即可,注意断言结果的作用域,避免出现干扰),通过断言结果更加直观的查看所有的失败断言汇总信息,如下所示:

 

我们可以利用 JMeter 提供的各种断言,进行请求响应结果的判定,其他在本文中没有用到的断言,各位小主们可先自行研究探索。同时,在实际的自动化应用过程中,JMeter 的断言难免出现不满足我们测试场景的时候,那么就需要我们针对不同的场景进行扩展了,通过已有断言 BeanShell 和 BSF 去进行扩展,也许可以解决我们的问题。另外,JMeter 是用 Java 语言编写的开源测试工具,当然也具有 Java 良好的扩展性,我们可以通过开发 JMeter 插件对其进行符合我们当前业务的定制扩展,以大大提升适应性、易用性。(PS:相关的简单实战示例,后续陆续更新,敬请期待!)

 

至此, JMeter学习-006-JMeter 断言实例之一 - 响应断言 顺利完结,希望此文能够给初学 JMeter 的您一份参考。

最后,非常感谢亲的驻足,希望此文能对亲有所帮助。热烈欢迎亲一起探讨,共同进步。非常感谢! ^_^

 

posted on 2019-07-23 17:14  lovely&lucy  阅读(243)  评论(0编辑  收藏  举报

导航