JMeter实现接口自动化测试
JMeter实现接口自动化测试
一、jmeter的环境搭建
参考:https://www.cnblogs.com/qmfsun/p/4902534.html
二、jmeter的汉化
临时汉化方法:打开jmeter,options-->choose language-->选择语言
可以根据自己的需要选择简体中文或者繁体中文,如图:
![](https://upload-images.jianshu.io/upload_images/14987680-98120e40223296de.png?imageMogr2/auto-orient/strip|imageView2/2/w/964/format/webp)
但是这种办法是临时汉化,退出软件之后再次打开界面还是英文风格,需要永久汉化可以修改jmeter的配置文件jmeter.properties:
进入jmeter的安装目录,找到配置文件用npp打开,在 # language=en 下面加一行:language=zh_CN,即可实现永久汉化。
![](https://upload-images.jianshu.io/upload_images/14987680-0731a34de16b891a.png?imageMogr2/auto-orient/strip|imageView2/2/w/893/format/webp)
配置文件的路径:../jmeter/apache-jmeter-5.0/bin/jmeter.properties
npp查找字符小技巧:当前文件按Ctrl+f调出查找弹框,输入需要查找的字符,一直向后查找,直至需要查找的位置:
![](https://upload-images.jianshu.io/upload_images/14987680-9435821169261aad.png?imageMogr2/auto-orient/strip|imageView2/2/w/969/format/webp)
三、jmeter的接口请求
3.1、打开Jmeter:下载好Jmeter后,双击bin目录下的jmeter.bat文件:
![](https://upload-images.jianshu.io/upload_images/14987680-5c41b7c1a479cb47.png?imageMogr2/auto-orient/strip|imageView2/2/w/1200/format/webp)
3.2、添加线程组:在“测试计划”上点击鼠标右键-->添加-->threads(Users)-->线程组,添加测试场景设置组件,接口测试中一般设置为1个“线程数”,根据测试数据的个数设定“循环次数”。
![](https://upload-images.jianshu.io/upload_images/14987680-ba742cc413c22a6d.png?imageMogr2/auto-orient/strip|imageView2/2/w/1200/format/webp)
3.3、添加“HTTP信息头管理器”、“HTTP Cookie管理器”和“用户定义的变量”
![](https://upload-images.jianshu.io/upload_images/14987680-6ce64504e72eeedc.png?imageMogr2/auto-orient/strip|imageView2/2/w/1200/format/webp)
3.4、添加HTTP请求
![](https://upload-images.jianshu.io/upload_images/14987680-a7ed5706962a8896.png?imageMogr2/auto-orient/strip|imageView2/2/w/1200/format/webp)
3.5、添加信息:包括信息头管理器信息、自定义变量和HTTP请求信息
![](https://upload-images.jianshu.io/upload_images/14987680-0497b6d1c955322a.png?imageMogr2/auto-orient/strip|imageView2/2/w/1200/format/webp)
![](https://upload-images.jianshu.io/upload_images/14987680-b64f80fd01d9e37e.png?imageMogr2/auto-orient/strip|imageView2/2/w/1200/format/webp)
![](https://upload-images.jianshu.io/upload_images/14987680-c480e7d8ac5a82ce.png?imageMogr2/auto-orient/strip|imageView2/2/w/1200/format/webp)
3.6、添加查看结果树(监听器),并执行查看结果
![](https://upload-images.jianshu.io/upload_images/14987680-f4bdba061a4886e2.png?imageMogr2/auto-orient/strip|imageView2/2/w/1200/format/webp)
四、jmeter的参数化
应用场景:把接口需要的参数值保存在csv或者txt文档中,接口通过参数名在文档中获取参数值
4.1、添加csv组件
![](https://upload-images.jianshu.io/upload_images/14987680-c6a4a7961c3574ce.png?imageMogr2/auto-orient/strip|imageView2/2/w/1200/format/webp)
4.2、在csv文件中填写参数名和参数值
![](https://upload-images.jianshu.io/upload_images/14987680-06f8328069196fd7.png?imageMogr2/auto-orient/strip|imageView2/2/w/833/format/webp)
4.3、CSV 数据文件设置
![](https://upload-images.jianshu.io/upload_images/14987680-36e29481c9fab1d3.png?imageMogr2/auto-orient/strip|imageView2/2/w/1200/format/webp)
4.4、HTTP请求参数化
![](https://upload-images.jianshu.io/upload_images/14987680-9a63ff4209d2380f.png?imageMogr2/auto-orient/strip|imageView2/2/w/1092/format/webp)
4.5、查看测试结果
![](https://upload-images.jianshu.io/upload_images/14987680-938714de361aeacd.png?imageMogr2/auto-orient/strip|imageView2/2/w/1092/format/webp)
五、jmeter的json传参
应用场景:第一个接口响应完成后,获取第一个接口响应体中的某个参数值,作为第二个接口请求体中请求参数值,这个获取过的过程通常使用正则和json提取,这里讲一下json提取
5.1、从后置处理器添加json提取组件
![](https://upload-images.jianshu.io/upload_images/14987680-d2e2d1c665107c27.png?imageMogr2/auto-orient/strip|imageView2/2/w/1027/format/webp)
5.2、定位参数位置(推荐使用chrome插件“JSON-handle”)
![](https://upload-images.jianshu.io/upload_images/14987680-31f115518f918c25.png?imageMogr2/auto-orient/strip|imageView2/2/w/851/format/webp)
5.3、json提取器添加提取信息
![](https://upload-images.jianshu.io/upload_images/14987680-5e7963d560f1deae.png?imageMogr2/auto-orient/strip|imageView2/2/w/1092/format/webp)
5.4、新参数传给第二个接口
![](https://upload-images.jianshu.io/upload_images/14987680-9a073f03be61fde8.png?imageMogr2/auto-orient/strip|imageView2/2/w/1200/format/webp)
5.5、执行第二个接口
![](https://upload-images.jianshu.io/upload_images/14987680-a2931de527994d36.png?imageMogr2/auto-orient/strip|imageView2/2/w/1200/format/webp)
5.6、验证定位的json位置是否正确
![](https://upload-images.jianshu.io/upload_images/14987680-9f67cb07414852bc.png?imageMogr2/auto-orient/strip|imageView2/2/w/1042/format/webp)
六、jmeter的断言添加
6.1、添加“响应断言”和“断言结果”
![](https://upload-images.jianshu.io/upload_images/14987680-07578a4dc3d9dd29.png?imageMogr2/auto-orient/strip|imageView2/2/w/1200/format/webp)
![](https://upload-images.jianshu.io/upload_images/14987680-42ec3cc4ccd1114c.png?imageMogr2/auto-orient/strip|imageView2/2/w/1036/format/webp)
6.2、添加断言信息
![](https://upload-images.jianshu.io/upload_images/14987680-cb937f0365ef3a57.png?imageMogr2/auto-orient/strip|imageView2/2/w/1200/format/webp)
6.3、查看断言结果
![](https://upload-images.jianshu.io/upload_images/14987680-55235a53e7659ae6.png?imageMogr2/auto-orient/strip|imageView2/2/w/1200/format/webp)
![](https://upload-images.jianshu.io/upload_images/14987680-863838b3b9058eb4.png?imageMogr2/auto-orient/strip|imageView2/2/w/1200/format/webp)