1、背景
最近在技术群里面讨论到使用 python 操作 jmeter 的话题
有人提到了 subprocess 启动命令行来操作 jmeter,也有人提到了 jmeterapi,另外也有人提到了 pymeter
除了这些,实际上我之前也基于 docker+subprocess 来操作 jmeter 的项目
jmeterapi 这个库年久失修,就不想搞它了,下面跟我一起试试 pymeter 吧


2、环境准备
直接参考项目的说明即可

3、Jmeter 测试
3.1 写测试用例
使用方法比较简单,设置 GUI 类似,只是前后顺序不相同:
- 创建线程组
- 把 sample,断言,抽取等配置加入线程组
- 创建报告收集器
- 把线程组和报告收集器加入测试计划
- 运行测试计划

3.2 运行和收集结果
3.2.1 pycharm 启动
如果你使用的 PyCharm,会自动识别

3.2.2 命令行启动

3.2.3 查看报告
报告会自动生成在 output 文件夹下面,一个 test 函数对应一份报告
使用浏览器打开 index.html 就能看到完整的报告


4、总结
- python 结合 jmeter-dsl 的特性,使用起来很方便
- 常用的部分功能(线程组,部分断言,报告)已经支持
- 目前整体完成度还是偏低,插件,复杂的断言,各种 sample 还未支持
