当前位置:   article > 正文

对 Jenkins+ANT+Jmeter 接口测试的实践_elvis 测试框架

elvis 测试框架

1、前言

最近感觉大家都在讲Jenkins+jmeter+ant或maven的使用,但没有说到具体怎么投入到项目使用,只有比较基本的流程说说,所以我今天要说的就是我是怎么将这个方案投入到实际中使用的,jmeter和ant,之前做性能测试是用loadrunner,接口测试使用RobotFramework+Request来做的,loadrunner用来做简单接口测试太笨重而且成效不高,RobotFramework+Request虽然可以使用数据驱动模式,但测试报告就会显示很鸡肋,因为基本是一个测试用例在循环,所以后面使用了Jenkins+jmeter+ant的方案

2、框架与数据准备

在做接口测试框架之前,我研究了一下jmeter工具特征和本身项目接口的特征,jmeter是以请求为单位,不是以脚本或测试用例为单位,既然这样,我就可以每次循环获取接口或对应数据进行测试了,加上之前在testerhome上看到一些大神写的接口测试框架,其实框架是死的,思想是活的,利用这个活的特点,后面就想了一套比较简单的框架,可以看下图

是的,我的测试数据是用Excel管理的,Excel的测试数据可看下图

从数据格式可以看出,做了用例的执行标识,选中为YES时才执行,NO时不执行,这是一个用例选择执行的简单设计,还有请求的方法也是可以选择的,目前项目中的主要用POST和GET,除此之外之后要获取的还有用例名,调用的接口和具体的测试请求数据,这就是使用excel管理测试用例的模块
数据准备好以后做一些全局或初始的配置,一般就是改jmeter.properties里面对应的配置

是的,里面这行本来是默认指定为user.properties,但有自己个性化的需求,例如服务器和测试账号

配置基本完成,之后就是项目的目录架构,conf就是放配置文件的,如myuser.properties,jmx就是对应的脚本,report是输出的测试报告路径,res是一些上传文件所用的测试资源等,test_data是为后期性能测试作准备的,现在先忽略,test_suite就是放excel表格的,build.xml就不多说了,就是ant的执行文件

3、脚本设计

好吧,接下来就是jmx脚本的设计,如下图,这是一些变量的设置

还有上文提到用myuser.properties配置的,这些在jmeter中作为变量来使用

然后对于读取数据,我是用了beanshell写了简单的java读取excel的脚本构建起与测试数据的连接,首先是先获取测试用例数,用于设置执行循环请求的次数,这里是用前置处理器的方式获取数据的,前置处理器需要sampler紧接着在其后使用,不然是不会执行的,所有我把它放在了登录的请求里面

具体的代码:

  1. <code><span class="n">String</span> <span class="n">filename</span><span class="o">=</span><span class="n">vars</span><span class="o">.</span><span class="na">get</span><span class="o">(</span><span class="s">"test_data"</span><span class="o">);</span>
  2. <span class="n">String</span> <span class="o">[]</span> <span class="n">datalist</span><span class="o">=</span><span class="k">new</span> <span class="n">String</span><span class="o">[</span><span class="mi">20</span><span class="o">];</span>
  3. <span class="n">String</span> <span class="n">content</span><span class="o">;</span>
  4. <span class="n">InputStream</span> <span class="n">openfile</span> <span class="o">=</span> <span class="k">new</span> <span class="n">FileInputStream</span><span class="o">(</span><span class="n">filename</span><span class="o">);</span>
  5. <span class="n">Workbook</span> <span class="n">wb</span> <span class="o">=</span> <span class="n">Workbook</span><span class="o">.</span><span class="na">getWorkbook</span><span class="o">(</span><span class="n">openfile</span><span class="o">);</span>
  6. <span class="n">Sheet</span> <span class="n">FirstSheet</span> <span class="o">=</span> <span class="n">wb</span><span class="o">.</span><span class="na">getSheet</span><span class="o">(</span><span class="mi">0</span><span class="o">);</span>
  7. <span class="kt">int</span> <span class="n">totalRows</span><span class="o">=</span><span class="n">FirstSheet</span><span class="o">.</span><span class="na">getRows</span><span class="o">();</span>
  8. <span class="kt">int</span> <span class="n">rows</span><span class="o">=</span><span class="n">totalRows</span><span class="o">-</span><span class="mi">1</span><span class="o">;</span> <span class="c1">//减1是因为我标题行不取,为了后面从第二行开始取</span>
  9. <span class="n">String</span> <span class="n">data</span><span class="o">=</span><span class="n">rows</span><span class="o">.</span><span class="na">toString</span><span class="o">();</span>
  10. <span class="n">vars</span><span class="o">.</span><span class="na">put</span><span class="o">(</span><span class="s">"row"</span><span class="o">,</span><span class="n">data</span><span class="o">);</span> <span class="c1">//用例行数</span>
  11. <span class="n">wb</span><span class="o">.</span><span class="na">close</span><span class="o">();</span></code>
然后获取正常的测试数据是用beanshell sampler,这个有个大坑,就是用sampler来获取数据的话到时在测试报告会显示出来,但实际上其不是测试用例,后面会说我目前是怎么破的,反正取数据的操作最好就是不要显示在测试报告的统计上,这里就是取到刚才所说的测试用到的值,执行标识load,用例名称testcase,调用接口inter,请求方法method,请求数据request以及预期结果断言response,之前就将他们作为变量传到脚本当中

具体的代码(补充说明一下,取excel我是用jxl.jar去读的,但有部分小伙伴喜欢用POI去读,反正原理都是一样的,像在平时编写代码那样导入包就好了):

  1. <code> <span class="kt">int</span> <span class="n">i</span><span class="o">;</span>
  2. <span class="kt">int</span> <span class="n">k</span><span class="o">=</span><span class="mi">0</span><span class="o">;</span>
  3. <span class="n">String</span> <span class="o">[]</span> <span class="n">datalist</span><span class="o">=</span><span class="k">new</span> <span class="n">String</span><span class="o">[</span><span class="mi">20</span><span class="o">];</span>
  4. <span class="n">String</span> <span class="n">content</span><span class="o">;</span>
  5. <span class="n">String</span> <span class="n">filename</span><span class="o">=</span><span class="n">vars</span><span class="o">.</span><span class="na">get</span><span class="o">(</span><span class="s">"test_data"</span><span class="o">);</span>
  6. <span class="n">String</span> <span class="n">str</span><span class="o">=</span><span class="n">vars</span><span class="o">.</span><span class="na">get</span><span class="o">(</span><span class="s">"rownum"</span><span class="o">);</span>
  7. <span class="kt">int</span> <span class="n">row</span> <span class="o">=</span> <span class="n">Integer</span><span class="o">.</span><span class="na">parseInt</span><span class="o">(</span><span class="n">str</span><span class="o">);</span>
  8. <span class="n">InputStream</span> <span class="n">openfile</span> <span class="o">=</span> <span class="k">new</span> <span class="n">FileInputStream</span><span class="o">(</span><span class="n">filename</span><span class="o">);</span>
  9. <span class="n">Workbook</span> <span class="n">wb</span> <span class="o">=</span> <span class="n">Workbook</span><span class="o">.</span><span class="na">getWorkbook</span><span class="o">(</span><span class="n">openfile</span><span class="o">);</span>
  10. <span class="n">Sheet</span> <span class="n">FirstSheet</span> <span class="o">=</span> <span class="n">wb</span><span class="o">.</span><span class="na">getSheet</span><span class="o">(</span><span class="mi">0</span><span class="o">);</span>
  11. <span class="k">for</span><span class="o">(</span><span class="n">i</span><span class="o">=</span><span class="mi">1</span><span class="o">;</span><span class="n">i</span><span class="o"><</span><span class="mi">9</span><span class="o">;</span><span class="n">i</span><span class="o">++){</span> <span class="c1">//9是因为我表格就是9列的</span>
  12. <span class="k">if</span> <span class="o">(</span><span class="n">i</span><span class="o">!=</span><span class="mi">5</span><span class="o">&&</span><span class="n">i</span><span class="o">!=</span><span class="mi">6</span><span class="o">){</span> <span class="c1">//表格中白色的两行不取</span>
  13. <span class="n">Cell</span> <span class="n">oCell</span><span class="o">=</span> <span class="n">FirstSheet</span><span class="o">.</span><span class="na">getCell</span><span class="o">(</span><span class="n">i</span><span class="o">,</span><span class="n">row</span><span class="o">);</span>
  14. <span class="n">content</span><span class="o">=</span><span class="n">oCell</span><span class="o">.</span><span class="na">getContents</span><span class="o">();</span>
  15. <span class="n">datalist</span><span class="o">[</span><span class="n">k</span><span class="o">]=</span><span class="n">content</span><span class="o">;</span>
  16. <span class="n">k</span><span class="o">++;</span>
  17. <span class="o">}</span>
  18. <span class="o">}</span>
  19. <span class="n">vars</span><span class="o">.</span><span class="na">put</span><span class="o">(</span><span class="s">"load"</span><span class="o">,</span><span class="n">datalist</span><span class="o">[</span><span class="mi">0</span><span class="o">]);</span> <span class="c1">//标识</span>
  20. <span class="n">vars</span><span class="o">.</span><span class="na">put</span><span class="o">(</span><span class="s">"testcase"</span><span class="o">,</span><span class="n">datalist</span><span class="o">[</span><span class="mi">1</span><span class="o">]);</span> <span class="c1">//用例名称</span>
  21. <span class="n">vars</span><span class="o">.</span><span class="na">put</span><span class="o">(</span><span class="s">"inter"</span><span class="o">,</span><span class="n">datalist</span><span class="o">[</span><span class="mi">2</span><span class="o">]);</span> <span class="c1">//接口</span>
  22. <span class="n">vars</span><span class="o">.</span><span class="na">put</span><span class="o">(</span><span class="s">"method"</span><span class="o">,</span><span class="n">datalist</span><span class="o">[</span><span class="mi">3</span><span class="o">]);</span> <span class="c1">//请求方法</span>
  23. <span class="n">vars</span><span class="o">.</span><span class="na">put</span><span class="o">(</span><span class="s">"request"</span><span class="o">,</span><span class="n">datalist</span><span class="o">[</span><span class="mi">4</span><span class="o">]);</span> <span class="c1">//请求数据</span>
  24. <span class="n">vars</span><span class="o">.</span><span class="na">put</span><span class="o">(</span><span class="s">"response"</span><span class="o">,</span><span class="n">datalist</span><span class="o">[</span><span class="mi">5</span><span class="o">]);</span> <span class="c1">///断言校验</span>
  25. <span class="k">return</span> <span class="n">content</span><span class="o">;</span></code>
执行的效果如下图,开始在登录完之后拿到测试用例的数量,作为循环的次数,做个计数器用来excel行的遍历,使用if控制器判断执行方法,如果是YES的话才执行请求,如果method是POST就是执行POST请求的http sampler,GET就执行GET请求的http sampler,然后输出的请求结果会有对应的用例名称,最后在响应断言中填上response变量,基本的脚本设计就完成了

具体http请求也是很简单的

4、整理测试报告

脚本设计完以后,用ant去执行jmx就可以了,至于怎么用ant去执行我就不多说了,去网上搜一下大把的教程,我这里是同时输出了两份测试报告,一份是作简单的统计用,就是jmeter-results-detail-report_21.xsl那份

还有是查看测试数据详情用的,就是jmeter-results-shanhe-me.xsl,两份报告之间做了一个比较简单的跳转,方便查看

其实在这里我是用了比较取巧的方法把之前取数据的sampler过滤掉了,不输出到html测试报告,打开输出的jtl看一下你就懂了

所以两份测试报告我也做了一些手脚,但这个不是明智的方法,只不过是没有办法中的办法,就是去改上文的两个xsl
统计的

查看数据的

其实这些也是我之后要优化的地方,只是目前太急要出个方案罢了

说了那么多,还没说到jenkins,至于怎么使用,https://testerhome.com/topics/5099,大同小异,就是这样用就好

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/416752
推荐阅读
相关标签
  

闽ICP备14008679号